Search code examples
salesforcevisualforceapex

How to put a comma between every value excepting the last valuein a map


So the problem statement is: I have an Object:Case_Article_Junction__c and two fields Case (which has the id of every case) and Case__r.CaseNumber (which stores the case number).

The challenge is to display the case number on a Visual Force page, a hyperlink, clicking which will take me to the corresponding case detail taking the case id as a parameter in the url configured.

The following apex controller code simply takes a map and queries the case id and the case number of particular article id.

Apex Class:

public class NewTestApexClass
{
 Map<Integer,Case_Article_Junction__c> mapToAccount = new 
 Map<Integer,Case_Article_Junction__c>();

public NewTestApexClass() {
    Integer i = 0;

    for (Case_Article_Junction__c a : [select Case__c, Case__r.CaseNumber from Case_Article_Junction__c where Article_shadow__c='a1Od0000000uAWK']) {

        mapToAccount.put(i, a);
        i++;

    } 
}

public Map<Integer,Case_Article_Junction__c> getMapToAccount() {
    return mapToAccount;
}
}

The following Visualforce Page is used to put an hyperlink to the case number and clicking which takes us to the corresponding case page with the id as parameter

<apex:page controller="NewTestApexClass" showheader="false" 
applyHtmlTag="false" standardStylesheets="false" >

       <apex:repeat value="{!mapToAccount}" var="accNum">
       <apex:outputLink value="https://cadence--dev2--c.cs13.visual.force.com/apex/case_CDSInfo_Page?id={!mapToAccount[accNum].Case__c}">{!mapToAccount[accNum].Case__r.CaseNumber}</apex:outputLink>
           <apex:outputText>,</apex:outputText>
   </apex:repeat>
</apex:page>

Now my question is,how can I put a comma between every case entered excepting the last case,that is to say the 'comma' must be present in between the case numbers only when there are multiple values and not the last value (when multiple case numbers are displayed) and also when there is a single case number, currently the repeat puts a comma after every value including the last value

Please help in providing a solution on the same


Solution

  • You don't need a Map to store your data, it could go perfectly well to a List<Case_Article_Junction__c>. This is good because Lists have a concept of index, last element... Maps are iterable but that used to be done in undetermined order (that changed with Java 8 if I recall correctly, adopted in SF ~2 years ago?)

    Anyway. One way to solve it would be to learn the Id of last element and then use rendered attribute:

    <apex:repeat value="{!linksList}" var="link">
        <apex:outputLink value="/apex/case_CDSInfo_Page?id={!link.Case__c}">{!link.Case__r.CaseNumber}</apex:outputLink>
        <apex:outputText rendered="{!link.Id != lastRecordId}">,</apex:outputText>
    </apex:repeat>
    

    You probably could even try doing that without a helper variable, experiment with {!linksList[linksList.size-1].Id}.

    Another way would be to use CSS for this. I didn't try it but How to style unordered lists in CSS as comma separated text looks promising. If you decide it's something worth looking into - remember that apex:datalist tag will build for you a list without manual looping

    There's also a way to use a helper <apex:variable> tag that will start at zero, increment at each iteration and you'd base your rendered attribute on comparing it with collection's size. Check https://salesforce.stackexchange.com/a/193204/799 for some inspiration. The docs say to not rely on this behaviour though so your call.