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
Lee.ax1423Lee.ax1423 

How to Convert List values to a comma separate values in single field

I want to display list values in visual force page but i want to display in single filed with comma separated values.

For example i have ten contacts in my contact object.I want to display these all contact names or another filed in a text box with comma separated.(ex: Lee,jeff,dogules).

 

Can any one help me how to achieve this.......

 

Help!!!!!!!!!!!!!

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi Lee,

 

Example-

 

// a list of string, you wanna display

List<String> tempLst = new List<String>{'Lee','Jeff','Dogules','Alok'};

 

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : tempList)

{

 commaSepratedList += str + ',' ;

}

 

// remove last additional comma from string

commaSepratedList = commaSepratedLis.subString(0,commaSepratedList.length());

system.debug('comma seprated list is----> '+commaSepratedList);

All Answers

@anilbathula@@anilbathula@

Hi Lee,

 

I had a sample code with same requirement.It may help you for reference.:-

 

 

public String getApprbyBanks(){
String bank='', tmpBank='';

for(Approved_by_Banks__c abb :[SELECT id, Name, Bank_Name__r.Name FROM
Approved_by_Banks__c
where Project__c=:ld.Project_lookup__c]){
bank = bank + abb.Bank_Name__r.Name +',';
}
if(bank.length()>0)
tmpBank = bank.substring(0,bank.length()-1);
return tmpBank;
}

/*Method to calss in vf page8*/

 

public void setProject(){
try{
Projects__c proj = [Select id,Name,Builder_Name__r.Name,Location__r.Name,Location__c,Builder_Property_Approved__c
from Projects__c
where
id=:ld.Project_lookup__c
];
ld.Project_Name__c=proj.Name;
ld.Prop_by_builder__c=proj.Builder_Name__r.Name;
ld.Project_appr_banks__c=getApprbyBanks();  //Assign all values as comma seperated values.
ld.Project_appr__c=proj.Builder_Property_Approved__c;
ld.Prop_Locations__c=proj.Location__c;
}
catch(Exception e){
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'No Lead Data');
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'No Lead Data'));
}
}

 

i have a lookup field onto project a custom object from lead .

for every project there will be mutiple banks(custom object)who approves that project.

if i select any project from lead through lookup all the related banks for that project comes and store in bank__c rich text field on lead.

 

 

Thanks

Anil.B

 

 

Alok_NagarroAlok_Nagarro

Hi Lee,

 

Example-

 

// a list of string, you wanna display

List<String> tempLst = new List<String>{'Lee','Jeff','Dogules','Alok'};

 

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : tempList)

{

 commaSepratedList += str + ',' ;

}

 

// remove last additional comma from string

commaSepratedList = commaSepratedLis.subString(0,commaSepratedList.length());

system.debug('comma seprated list is----> '+commaSepratedList);

This was selected as the best answer
Lee.ax1423Lee.ax1423

Thanks for replying.I have already achieved.

But thanks a lot for your response