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
Daniel KDaniel K 

concatenate values of a field and assign to other field

Hi, 
   I have Account(parent) object and custom(child) object C1.
   I'm trying to concatenate all values of a text(tf1) field of C1 object and assign to a text custom(Acctf1) field in Account object.
   
   Let's say..
    Account          Custom Object(tf1 values)
    --------         -------------
    Account1         cs1
                            cs2
                            cs3
                            cs4
    
    Account2         cs5
                            cs6
                            cs7
              
    Now, for Account1 value, Acctf1 field should have cs1:cs2:cs3:cs4
             for Account2 value, Acctf1 field should have cs5:cs6:cs7

 As I couldn't do it through configuration, can you suggest how to code it ?
Thanks.
Abhishek BansalAbhishek Bansal
Hi Daniel,

In order to fulfill your requirement you need to create a trigger on your child object whose code will look like something below:
trigger updateParentAccount on C1__c (after insert, after update){ //Replace C1__c with API name of your custom child object
	Set<Id> accIds = new Set<Id>();
	
	for(C1__c c1 : trigger.new){ //Replace C1__c with API name of your custom child object
		if(c1.Account__c != null){ //Replace Account__c with API name of your custom Account lookup field on your child object
			accIds.add(c1.Account__c);
		}
	}
	
	List<Account> listOfAccounts = new List<Account>();
	listOfAccounts = [Select Acctf1__c, (Select tf1__c from C1__r) from Account where ID IN :accIds];
	
	for(Account acc : listOfAccounts){
		acc.Acctf1__c = '';
		for(C1__c c1 : acc.C1__r){
			if(c1.tf1__c != null){
				if(acc.Acctf1__c == '' || acc.Acctf1__c == null){
					acc.Acctf1__c = c1.tf1__c;
				}
				else{
					acc.Acctf1__c = acc.Acctf1__c + ':' + c1.tf1__c;
				}
			}
		}
	}
}

//Please replace Acctf1__c with APi name of the field in the Account.
//Please replace C1__r with the chid relationship name of the Account lookup field in the child object.
//Please replace tf1__c with APi name of the field in the child object.


Please adjust the API name of the fields and let me know if you face any issue.

Thanks
Abhishek Bansal.