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
Lalit Dhupar 13Lalit Dhupar 13 

update custom field on account with contact name with trigger

Hello,

i am new to salesforce and I have a custom field on account as Consecutive_Contacts_Name__c. Whenever a new contact is inserted the field should have value of the contact first name if Consecutive_Contacts_Name__c =null on account. if Consecutive_Contacts_Name__c != null it should have the previous value +',' + recent contact first name. Below is my code but it is not working properly. Could anyone please help on the same.

rigger ContactOnAccount on Contact (after insert) {
    List<Account> accList = new List<Account>();
    for (Contact con:[select FirstName, Account.Consecutive_Contacts_Name__c, Account.Id from Contact where id in:trigger.new]) 
    {
        
        Account acc = new Account(id=con.Account.id);  
        
        acclist.add(acc);
        if(acc.Consecutive_Contacts_Name__c == '' && acc.Consecutive_Contacts_Name__c == Null)
        {
            acc.Consecutive_Contacts_Name__c = con.FirstName; 
            update acclist;       
        }
        else{
            acc.Consecutive_Contacts_Name__c = acc.Consecutive_Contacts_Name__c + ',' + Con.FirstName;
           update acclist;
            }
    }
     
    
}
Best Answer chosen by Lalit Dhupar 13
Anurag SaxenaAnurag Saxena

Hi Lalit,

try this codeIt will if any issue comes in above solutions

trigger ContactOnAccount on Contact (after insert,after update) 
{
    List<Account> lstAccToUpdate = new List<Account>();
    Set<id>Acc_id = new Set<id>();
    
    for (contact contact_Obj : trigger.new)
    {
        Acc_id.add(contact_Obj.accountid);
    }
    
    Map<ID, Account> mapAccounts = new Map<ID, Account>([SELECT Id, Consecutive_Contacts_Name__c FROM Account where Id IN :Acc_id]);
    
    For(Contact Contact_Obj: Trigger.new)
	{
		Account acc_Obj = mapAccounts.get(Contact_Obj.AccountId);
      
	if(acc_Obj.Consecutive_Contacts_Name__c=='' || acc_Obj.Consecutive_Contacts_Name__c == NULL)
    {
        acc_Obj.Consecutive_Contacts_Name__c = Contact_Obj.firstname;
    }
    else
    {
        acc_Obj.Consecutive_Contacts_Name__c = acc_Obj.Consecutive_Contacts_Name__c + ',' + Contact_Obj.FirstName;
    }
    
		lstAccToUpdate.add(acc_Obj);
	}
		update lstAccToUpdate;
  
}

Thanks:)

All Answers

GauravGargGauravGarg
Hi Lalit,

Please update line:
 
if(acc.Consecutive_Contacts_Name__c == '' || acc.Consecutive_Contacts_Name__c == Null)

Hope this works, else we need to query on Account and update accordingly. 

Thanks,
Gaurav
Skype: gaurav62990
DeveloperSudDeveloperSud
Hi,
try the below code. Let us know if you still have the problem.
 
trigger ContactTrigger2 on Contact (after insert,after update) {
     
    set<id> actsids= new set<id>();
    for(contact c : trigger.new){
        actsids.add(c.accountid);
    }
    
    map<id,account> mapOfAct= new map<id,account>([select id,Consecutive_Contacts_Name__c from Account where
                                          id IN :actsids]);
                                                  
    List<Account> actToUpdate= new List<account>();
    String prevValue=null;
    for(contact c : trigger.new){
        if(mapOfAct.get(c.AccountId).Consecutive_Contacts_Name__c ==null){
            mapOfAct.get(c.AccountId).Consecutive_Contacts_Name__c = c.FirstName;
            actToUpdate.add(mapOfAct.get(c.AccountId));
        }
        else{
            mapOfAct.get(c.AccountId).Consecutive_Contacts_Name__c += '  '+c.FirstName;
            actToUpdate.add(mapOfAct.get(c.AccountId));
        }
    }
    update actToUpdate;
    
}

 
Anurag SaxenaAnurag Saxena

Hi Lalit,

try this codeIt will if any issue comes in above solutions

trigger ContactOnAccount on Contact (after insert,after update) 
{
    List<Account> lstAccToUpdate = new List<Account>();
    Set<id>Acc_id = new Set<id>();
    
    for (contact contact_Obj : trigger.new)
    {
        Acc_id.add(contact_Obj.accountid);
    }
    
    Map<ID, Account> mapAccounts = new Map<ID, Account>([SELECT Id, Consecutive_Contacts_Name__c FROM Account where Id IN :Acc_id]);
    
    For(Contact Contact_Obj: Trigger.new)
	{
		Account acc_Obj = mapAccounts.get(Contact_Obj.AccountId);
      
	if(acc_Obj.Consecutive_Contacts_Name__c=='' || acc_Obj.Consecutive_Contacts_Name__c == NULL)
    {
        acc_Obj.Consecutive_Contacts_Name__c = Contact_Obj.firstname;
    }
    else
    {
        acc_Obj.Consecutive_Contacts_Name__c = acc_Obj.Consecutive_Contacts_Name__c + ',' + Contact_Obj.FirstName;
    }
    
		lstAccToUpdate.add(acc_Obj);
	}
		update lstAccToUpdate;
  
}

Thanks:)
This was selected as the best answer