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
NewBee21NewBee21 

Trigger to update Contact's Last Name when Account Name is updated

I have written a Trigger in which whenever I create an Account, new Contacts are created with the Last Name same as the Account Name. But now when I try to update the Account Name, Contact's last name remains unchanged. I want it to get updated whenever I update my Account Name. How do I achieve this?

**Trigger** ==>

trigger Account1 on Account (after insert,after update) {
    list <contact> con=new list <contact>();    
    for(Account acc : trigger.new){
        contact c = new contact();
        c.LastName = acc.Name;
        c.AccountID = acc.ID;
        con.add(c);    
    }
    insert con;
}
Best Answer chosen by NewBee21
CharuDuttCharuDutt
Hii NewBee
Try Below Code
trigger Account1 on Account (after insert,after update) {
    map<Id,String> AccMap = new map<Id,String>();
    if(trigger.IsAfter){
        if(Trigger.IsInsert){
            for(Account acc : trigger.new){
                contact c = new contact();
                c.LastName = acc.Name;
                c.AccountId = acc.ID;
                   insert c;
            }
        }else{
            for(Account acc : trigger.new){
                if(Acc.name != trigger.oldmap.get(Acc.id).Name){
					AccMap.put(Acc.id,Acc.name);
                }
            }
        }
    }
	list<Contact> lstCon = [Select Id,AccountId ,LastName From Contact Where AccountId In :AccMap.keySet()];
    for(Contact Con : lstCon){
        if(AccMap.containsKey(Con.AccountId)){
            Con.lastName = AccMap.get(Con.AccountId);
        }
    } 
    if(lstCon.size()>0){
        update lstCon;
    }
    
   
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii NewBee
Try Below Code
trigger Account1 on Account (after insert,after update) {
    map<Id,String> AccMap = new map<Id,String>();
    if(trigger.IsAfter){
        if(Trigger.IsInsert){
            for(Account acc : trigger.new){
                contact c = new contact();
                c.LastName = acc.Name;
                c.AccountId = acc.ID;
                   insert c;
            }
        }else{
            for(Account acc : trigger.new){
                if(Acc.name != trigger.oldmap.get(Acc.id).Name){
					AccMap.put(Acc.id,Acc.name);
                }
            }
        }
    }
	list<Contact> lstCon = [Select Id,AccountId ,LastName From Contact Where AccountId In :AccMap.keySet()];
    for(Contact Con : lstCon){
        if(AccMap.containsKey(Con.AccountId)){
            Con.lastName = AccMap.get(Con.AccountId);
        }
    } 
    if(lstCon.size()>0){
        update lstCon;
    }
    
   
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
NewBee21NewBee21
Thanks a lot Charu for the prompt reply as usual. Appriciate it a lot!!

Just another query,Can insert and update possible only using "after update" in above scenario and not using after insert?