function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
self learning 8self learning 8 

Display list of values with comma separation but for the last value before need to display and instead comma

now following code displaying like a,b,c,d
But I want to display like a,b,c and d

Code is : 

String dear = 'Hello';
        
        boolean first = true;
        
        for(Account a : accs)
        {
            if(first)
            {
                dear += a.Salutation+ ' ' + a.lastname;
                first = false;
                continue;
            }
        
           // dear += ' and ' +  a.Salutation+ ' ' + a.lastname;
            dear += ' , ' +  a.Salutation+ ' ' + a.lastname;
        }
        dear += ',';
        
        Opp.customer__c = dear; 
        update opp;        
Best Answer chosen by self learning 8
Vikash GoyalVikash Goyal
In the above solution, I have used wrong variables or missed some variables to define but In my case the following code is working fine :
 
List<contact> lstContact = new List<contact>([SELECT firstname, lastname FROM Contact WHERE lastname != null limit 5]);
String dear = 'Hello';
boolean first = true;
for(Contact a : lstContact){
   if(first){
     dear += a.firstname+ ' ' + a.lastname;
     first = false;
     continue;
   }
   dear += ' , ' +  a.firstname+ ' ' + a.lastname;
}
if(!lstContact.isEmpty() && lstContact.size() > 1){
  dear = dear.substringBeforeLast(',') + ' and ' + dear.substringAfterLast(',');
}
system.debug('======='+dear);

 

All Answers

Vikash GoyalVikash Goyal
Hi, 

You can use any of the following approaches for this:

1.
String dear = 'Hello';
boolean first = true;
for(Account a : accs){
   if(first){
     dear += a.Salutation+ ' ' + a.lastname;
     first = false;
     continue;
   }
   dear += ' , ' +  a.Salutation+ ' ' + a.lastname;
}
if(!accs.isEmpty() && accs.size > 1){
  dear = dear.substringBeforeLast(',') + ' and ' + dear.substringAfterLast(',');
}
dear += ',';

2.
String dear = 'Hello';
boolean first = true;
for(Account a : accs){
   if(first){
     dear += a.Salutation+ ' ' + a.lastname;
     first = false;
     continue;
   }
   dear += ' , ' +  a.Salutation+ ' ' + a.lastname;
}
if(!accs.isEmpty() && accs.size > 1){
  dear = dear.reverse().replaceFirst(',', 'dna').reverse();
}
dear += ',';

3.
String dear = 'Hello';
boolean first = true;
Integer counter = 1;
for(Account a : accs){
   if(first){
    	dear += a.Salutation + '  '+ a.lastname;
        first = false;
    }else if(lst.size() > 1 && counter == lst.size()){
        dear += ' and ' +a.Salutation + '  '+ a.lastname;
    }else{
        dear += ' , ' + a.Salutation + '  '+ a.lastname;
    }
    counter++;
}
dear += ',';

Hope this will help you.

Thanks
 
self learning 8self learning 8
If I am displaying 2 accounts it should be like a and B
If I am displaying 3 account it should be like a,b and c
If I am displying 4 account it should be like a,b,c and d
self learning 8self learning 8
please help with this 
 
self learning 8self learning 8
Dear Vikash , I tried all the methods but no luck. All are displaying either comma or and not like following. If I am displaying 2 accounts it should be like a and B If I am displaying 3 account it should be like a,b and c If I am displying 4 account it should be like a,b,c and d Regards, Krushnaveni Pallam | Consultant
Vikash GoyalVikash Goyal
In the above solution, I have used wrong variables or missed some variables to define but In my case the following code is working fine :
 
List<contact> lstContact = new List<contact>([SELECT firstname, lastname FROM Contact WHERE lastname != null limit 5]);
String dear = 'Hello';
boolean first = true;
for(Contact a : lstContact){
   if(first){
     dear += a.firstname+ ' ' + a.lastname;
     first = false;
     continue;
   }
   dear += ' , ' +  a.firstname+ ' ' + a.lastname;
}
if(!lstContact.isEmpty() && lstContact.size() > 1){
  dear = dear.substringBeforeLast(',') + ' and ' + dear.substringAfterLast(',');
}
system.debug('======='+dear);

 
This was selected as the best answer
self learning 8self learning 8
Thank a lot for yoour help vikas, based on your code i figured out the solution