+ Start a Discussion
Ajitkumar Pradhan 9Ajitkumar Pradhan 9 

Populate the comma separated related Contact name on Account Description field which should work for insert, update and delete case.

Populate the comma separated related Contact name on Account Description field which should work for insert, update and delete case.
Best Answer chosen by Ajitkumar Pradhan 9
Parag Bhatt 10Parag Bhatt 10
Hi Ajitkumar,
The Trigger scenario you mentioned can only work on before update and delete Trtigger Functionality cannot work on account insertion as contact can only be created when there is account. :)

Other functionality can be performed please find below:-

trigger FetchContactOnAccountDescription on Account (before update,before Delete) {
    list<id> lstaccid= new list<id>();
    string glue = '';
    map<id,string> mapcontact = new map<id,string>();
  
        if(Trigger.isBefore){
              if(Trigger.isUpdate || Trigger.isDelete){
            For(Account acc:Trigger.new){
                lstaccid.add(acc.id);                
            }
            
            
            if(lstaccid.size()>0){
                for(Contact con:[Select id,name,Accountid from contact where Accountid IN :lstaccid]){
                    mapcontact.put(con.Accountid, con.Name);    
                }
            }
            
            if(mapcontact.size() != null){
                
                for(Account acc:trigger.new){
                    if(mapcontact.containsKey(acc.id)){
                        acc.Description += glue + mapcontact.get(acc.id);
                        glue=';';
                    }
                }
            }
        }
    }
}

It is working as expected :)


Please let us know if this will help you.

Thanks,
Parag Bhatt

All Answers

Parag Bhatt 10Parag Bhatt 10
Hi Ajitkumar,
The Trigger scenario you mentioned can only work on before update and delete Trtigger Functionality cannot work on account insertion as contact can only be created when there is account. :)

Other functionality can be performed please find below:-

trigger FetchContactOnAccountDescription on Account (before update,before Delete) {
    list<id> lstaccid= new list<id>();
    string glue = '';
    map<id,string> mapcontact = new map<id,string>();
  
        if(Trigger.isBefore){
              if(Trigger.isUpdate || Trigger.isDelete){
            For(Account acc:Trigger.new){
                lstaccid.add(acc.id);                
            }
            
            
            if(lstaccid.size()>0){
                for(Contact con:[Select id,name,Accountid from contact where Accountid IN :lstaccid]){
                    mapcontact.put(con.Accountid, con.Name);    
                }
            }
            
            if(mapcontact.size() != null){
                
                for(Account acc:trigger.new){
                    if(mapcontact.containsKey(acc.id)){
                        acc.Description += glue + mapcontact.get(acc.id);
                        glue=';';
                    }
                }
            }
        }
    }
}

It is working as expected :)


Please let us know if this will help you.

Thanks,
Parag Bhatt
This was selected as the best answer
adi salesforceadi salesforce
By using this we are not getting all names of contacts to added to account
 
Shraddha Barbhai 9Shraddha Barbhai 9
By the above code all Contact names are not updated. Null and 1st Contact name is added twice, without comma seperation.