+ Start a Discussion
Priyanka SatputePriyanka Satpute 

user insert record in contact and update in account also using by created contact id

Suraj Tripathi 47Suraj Tripathi 47

Hi Priyanka,

Please find the solution. user insert record in contact and update in account also using by created contact id

You can write a trigger for this as whenever Contact is created then the related account is updated.

trigger ContactTrigger on Contact (After Insert) {

 if(trigger.isAfter && trigger.isInsert){
        set<Id> setId=new set<Id>();
        for(Contact con:trigger.new){
            if(con.AccountId!=null){
             setId.add(con.accountid);
            }
        }
        
        Map<Id,Account> accountMap=new Map<Id,Account>([select id,Name from account where id in: setId]);
        system.debug('accountMap::'+accountMap);
        for(Contact con:trigger.new){
            if(con.AccountId==accountMap.get(con.AccountId).id){
                system.debug('accountMap.get(con.AccountId).Name:: '+accountMap.get(con.AccountId).Name);
                accountMap.get(con.AccountId).Name=accountMap.get(con.AccountId).Name+'_'+con.LastName;
                //account is updated with accountName+ContactLastName
            } 
        }
        update accountMap.Values();
    }
    }

OR

Without Trigger

public class AccountUpdate{
    
    public static void accountData(){
        Account ac=new Account();
        ac.Name='Test';
        insert ac;
        Contact con=new Contact();
        con.lastName='Das';
        con.AccountId=ac.id;
        insert con;
        Account acInstance=new Account();
        acInstance.Id=ac.Id;
        acInstance.Name=ac.Name+'_'+con.LastName;
        update acInstance;
        system.debug('acInstance::: '+acInstance);
    }
}


Priyanka, Please let me know it is working or not.

Please mark it as Best Answer so that other people would take reference from it.

Thanks