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
Roy SinghRoy Singh 

Trigger on contact object when i change account name

Hi All,

I need to write trigger on contact object.
My problem is whenever i change the account name from in existing contact record then all account name should be changed related to contact object record like case and task. In case record and task record account name should be changed whenever i change account name on contact record.

Thanks,
Jayshil BhagatJayshil Bhagat
Hi Roy

try below code and let me know if it doesn't work
trigger updateRelatedRecords on Contact (before update) {
    
    List<Case> caseList = new List<Case>();
    List<Task> taskList = new List<Task>();
    
    for(Contact con: trigger.new){
        
        Contact con1 = trigger.old[0];
        
        if(con1.AccountId != con.AccountId){
            Task newTask = [Select Id from Task where WhoId =: con.Id limit 1];
            newTask.WhatId = con.AccountId;
            Case newCase = [Select Id from Case where ContactId =: con.Id limit 1];
            newCase.AccountId = con.AccountId;
            
            taskList.add(newTask);
            caseList.add(newCase);
        }
        
    }
    
    update taskList;
    update caseList;
}

I tried it and it is working for me
Jainam ContractorJainam Contractor
Hi Roy,

The solution given by Jayshil will work fine unless and until you do not have Bulk contact records getting their Account Name changed. If it does, then it will hit the governor limit of 100 SOQL in a Transaction as he has used SOQL inside a FOR loop.

Please check the below Trigger and let me know if it works for you. It abides by all the Governor limit and is bulkified as well.
trigger UpdateRelatedAccount on Contact (after update) {
    set<Id> ConId = new set<Id>();
    for(Contact C : Trigger.New){
        for(Contact C1 : Trigger.Old){
            if(C.AccountId != C1.AccountId && C.Id == C1.Id){
            	ConId.add(C.Id);	    
            }
        }
    }
    List<Case> CaseList = [select id, ContactId, AccountId from Case WHERE ContactId IN :ConId];
	List<Task> TaskList = [select id, whoId, WhatId from Task WHERE whoId IN :ConId];
    List<Case> CaseUpdList = new List<Case>();
    List<Task> TaskUpdList = new List<Task>();
    for(Contact C : Trigger.New){
        for(Task T : TaskList){
            if(C.Id == T.WhoId){
                T.WhatId = C.AccountId;
                TaskUpdList.add(T);
            }
        }
        for(Case Ca : CaseList){
            if(C.Id == Ca.ContactId){
                Ca.AccountId = C.AccountId;
                CaseUpdList.add(Ca);
            }
        }
    }
    if(CaseUpdList != NULL && CaseUpdList.size() > 0){
        update CaseUpdList;
    }
    if(TaskUpdList != NULL && TaskUpdList.size() > 0){
        update TaskUpdList;
    }
}

Please let me know if it works or you need any more assistance.

Please mark this as the solution to help Community grow and move this question out of the Unsolved list.

Thanks & Regards,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC
www.varasi.com
Ramakrishna Reddy GouniRamakrishna Reddy Gouni
might be question is not clear, please update it.  are you editing account name or changing account for the contact? 

1. if you want to chagne account for contact. write trigger on contact only. 
2. if you want to edit the account name of contact. no need to write trigger on contact. just write on account... 

because of.....
1.  relation between objects is based on ID's not on labels....