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
Girbson BijouGirbson Bijou 

Concatenate child field record To parent

I need to concatenate the individual CampaignMember Contact names into a single field for each Campaign (again, as below).
User-added image
v varaprasadv varaprasad
Hi Gribson,

Please execute below code in developer console and check once in description field am updating all campagin mebers names.



 
list<Campaign>  lstCamp = [Select id,Description,(SELECT Id, Name FROM CampaignMembers) FROM Campaign];
system.debug('==lstCamp=='+lstCamp);

for(Campaign camp : lstCamp){
   system.debug('==camp=='+camp.CampaignMembers);
   list<string> camMebers = new list<string>();
   if(camp.CampaignMembers.size()>0){
       for(CampaignMember cm : camp.CampaignMembers){
	      camMebers.add(cm.name);  
	   
	   }
   
   }
   
   camp.Description = String.join(camMebers, ',');
   
}

update lstCamp;

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1