+ Start a Discussion
Joe HayesJoe Hayes 

Display list of contacts ...htmBody.replace

Hi Everyone,

Hopefully this is dead easy,

I have got an apex class to get a list of contacts, what I need to do is then display that list of names in an email sent from apex,
I have got everything working but cannot get the list to populate if more than one contact is in the list.
 

Does anyone know how I can display the list?

...

List<Contacts> cons = [SELECT Id, Name FROM Contact WHERE......

//Further down in the code

htmlBody = htmlBody.replace('[ListOfContacts]', cons.Name);  //This needs to display the list

Thanks for your help,
Joe
Best Answer chosen by Joe Hayes
Nagendra Prasad ChNagendra Prasad Ch
Hi Joe,

In line htmlBody = htmlBody.replace('[ListOfContacts]', listOfContacts); it is not listOfContacts, it is listOfCntacts. 

It's declared in line no 2 in above code. 


List<Contacts> cons = [SELECT Id, Name FROM Contact WHERE......
String listOfCntacts;

for(contact con: cons){
// Add all Names to list comma separated, If u want each name in new line replace ', ' with '<br/>' in below 
if(listOfCntacts == null) listOfCntacts = con.Name;
listOfCntacts = listOfCntacts+ ', '+con.Name ;
}

//Further down in the code

htmlBody = htmlBody.replace('[ListOfContacts]', listOfCntacts);  //This needs to display the list


 

All Answers

Nagendra Prasad ChNagendra Prasad Ch
Hi Joe,

Try this,
All Names will be listed as comma separated, If u want each name in new line replace ', ' with '<br/>' in line 7
 
List<Contacts> cons = [SELECT Id, Name FROM Contact WHERE......
String listOfCntacts;

for(contact con: cons){
// Add all Names to list comma separated, If u want each name in new line replace ', ' with '<br/>' in below 
if(listOfCntacts == null) listOfCntacts = con.Name;
listOfCntacts = listOfCntacts+ ', '+con.Name ;
}

//Further down in the code

htmlBody = htmlBody.replace('[ListOfContacts]', listOfCntacts);  //This needs to display the list

Please let me know if it helps.

Thanks,
Nagendra
Joe HayesJoe Hayes
Hi Nagendra,

Thanks for the reply,

I am getting an error on the line htmlBody = htmlBody.replace('[ListOfContacts]', listOfContacts);
The error is Variable does not exist listOfContacts.

Do you know why?

Thanks
Joe
Nagendra Prasad ChNagendra Prasad Ch
Hi Joe,

In line htmlBody = htmlBody.replace('[ListOfContacts]', listOfContacts); it is not listOfContacts, it is listOfCntacts. 

It's declared in line no 2 in above code. 


List<Contacts> cons = [SELECT Id, Name FROM Contact WHERE......
String listOfCntacts;

for(contact con: cons){
// Add all Names to list comma separated, If u want each name in new line replace ', ' with '<br/>' in below 
if(listOfCntacts == null) listOfCntacts = con.Name;
listOfCntacts = listOfCntacts+ ', '+con.Name ;
}

//Further down in the code

htmlBody = htmlBody.replace('[ListOfContacts]', listOfCntacts);  //This needs to display the list


 
This was selected as the best answer
Joe HayesJoe Hayes
Thanks very much :)
Joe HayesJoe Hayes
Hi Nagendra,

Thank you for your help on this, it is now displaying the contacts. However, the very first contact in the list is repeating twice e.g.

Contact1
Contact1
Contact2
Contact3


I can't think how to alter the coding to stop it from displaying the first one twice.

Do you have any suggestions?

Thanks
Joe