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
chercher 

Concatenating strings via apex trigger

Hi


I have child object "Relationship" related to Account. There is field on the Account "Relationship__c", in this field i want to auto populate concatenated values of distinct createdbyIds for each relationship record. 

For instace, if the createdbyids for relationship records pertaining to an account "A" are A1, B1, C1. Then the Relationship field on the Account should be populated with A1;B1;C1. 

Let me know if this is possible by apex triiger.
Vinit_KumarVinit_Kumar
I am assuming that you want to populate the Account field while updating the  Account record.Then,you can create an before update Trigger on Accoun t and fetch the related createdbyids of all child records and populate it on Relationship__c field on Account .The Trigger should be something like below :-
trigger populateRelationship on Account(before update)
{
	List<Id> accIds = new List<Id>();
	List<String> relString = new List<String>();
	List<Relationship___c> relList = new List<Relationship__c>();
	
	for(Account acc : trigger.new)
	{
		accIds.add(acc.id);
	}
	
	relList = [select id,createdbyid from Relationship__c where Account__c in:accIds];
//Assuming your relationship field name is Account__c on child object if not replace 
//it with relationship field name	
	
	for(Relationship__c rel:relList)
	{
		relString.add(rel.createdbyid);
	}
	
	String s = String.join(relString,';');
	
	for(Account acc : trigger.new)
	{
		acc.Relationship__c = s;
	}
	
}


If this helps,please mark it as best answer to help others :)


chercher

Hi Vinit, 

Thanks for your response. 

The trigger is on the relationship object, when a relationship record is inserted/deleted/updated the change should be reflected on the "Relationship__c" field on the Account Object. 


Based on the idea you gave, i have modified the code but the problem is createdbyids are being overwritten in the "Relationship__c" field on the Account object. when ever a new relaitonship record is created i want the createdbyids to be appended. For instance if a relationship record was created by A, then the value in the relationship field should be A.

Now, if a new relationship record, is created for the same account by a B, then the value in the relaitonship field should be A : B.

Let me know if this is possible to achive. I am posting my code below :

trigger Test on Relationship__c (after delete, after insert, after update) {

Account AccountList=[Select Id,Relationships__c from Account];


List<Relationship__c> LevelOfRelationship=new List<Relationship__c>([Select CreatedById
                                                                                     from Relationship__c
                                                                                    where System_SAM__c IN: AccountList]);
  
List<String> levstring=new List<String>();

            for(Relationship__c k: Trigger.old){
               
               
               
             
               
                for(integer i=0;i<LevelOfRelationship.size();i++){
               
               
               
                levstring.add(k.CreatedById);
               
                if(LevelOfRelationship[i]!=null) {
               
                 s = String.join(levstring,';');
               
                    
              
                AccountList.Relationships__c=s;
               
                }
               
            }
               
               





    update AccountList;



}

Vinit_KumarVinit_Kumar
Try below code :-

trigger populateRelationship on Relationship___c(before insert,before update)
{
	List<Id> accIds = new List<Id>();
	List<Relationship___c> relList = new List<Relationship__c>();
	
	for(Relationship___c rel : trigger.new)
	{
		accIds.add(rel.Account__c); //Assuming your relationship field name is Account__c on child object if not replace 
									//it with relationship field name								
	}
	
	Map<Id,Account> accMap = new Map<Id,Account>([select id,Relationship___c from Account where id in:accIds]);
	
	for(Relationship___c r : trigger.new)
	{
		if(accMap.get(r.Account__c).Relationship___c==null)
		{
			accMap.get(r.Account__c).Relationship___c=r.CreatedbyId;
		}
		else
		{
			accMap.get(r.Account__c).Relationship___c=accMap.get(r.Account__c).Relationship___c + ':' + r.CreatedbyId;
		}
	}
	
}

If this helps,please mark it as best answer to help others :)