+ Start a Discussion
pranav shah 40pranav shah 40 

How To Write a trigger To update the Account name.

these is What i have Written,,

trigger UpdateAccount on Account (after insert,after update, after delete, after undelete) 
{
set<ID> setAccountIDs=new Set<ID>();
for (Account a:Trigger.new)
{
setAccountIDs.add(a.AccountID);
List <Account> accounts=[select ID,Name from Account Where ID IN: setAccountIDs];
String accName = '';
a.name=accName;
}
update account;
}
Best Answer chosen by pranav shah 40
Vasani ParthVasani Parth
Pranav,

If you are looking to write a trigger which will update the Account name based on the contacts, then below is the working code,
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
    Set<ID> setAccountIDs = new Set<ID>();
    for(Contact c : Trigger.new){
        setAccountIDs.add(c.AccountId);
    }
  
    List<Account> accounts = [Select ID, Name,(Select Name From Contacts)  From Account WHERE ID IN :setAccountIDs];
    for(Account a : accounts){
        String accName = '';
        for(Contact c : a.Contacts){
            accName +=c.Name+ ' ';                      
        }
        a.Name=accName;
    }
    update accounts;
  
}

Please mark this as the best answer if this helps

All Answers

Vasani ParthVasani Parth
Pranav,

If you are looking to write a trigger which will update the Account name based on the contacts, then below is the working code,
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
    Set<ID> setAccountIDs = new Set<ID>();
    for(Contact c : Trigger.new){
        setAccountIDs.add(c.AccountId);
    }
  
    List<Account> accounts = [Select ID, Name,(Select Name From Contacts)  From Account WHERE ID IN :setAccountIDs];
    for(Account a : accounts){
        String accName = '';
        for(Contact c : a.Contacts){
            accName +=c.Name+ ' ';                      
        }
        a.Name=accName;
    }
    update accounts;
  
}

Please mark this as the best answer if this helps
This was selected as the best answer
pranav shah 40pranav shah 40
Hi @Vasani parth.

will u please Explain me the Logic For Above code 
VineetKumarVineetKumar
Hi Pranav,

The very first mistake that I can see in your code is that you have written a SOQL query inside FOR loop, as per the salesforce best practices, you should never write a SOQL query inside a FOR loop, as it risks your code hitting the governor limit.

Also, there is no point in running the below logic for the delete / undelete context.

Below is your code with some minor tweaks :

trigger UpdateAccount on Account (after insert, after update){
    set<ID> setAccountIDs=new Set<ID>();
    for (Account a:Trigger.new){
        setAccountIDs.add(a.ID);
    }
    List<Account> accountList = [select ID,Name from Account Where ID IN: setAccountIDs];
    for(Account thisAccount : accountList){
        String accName = 'XYZ';
        thisAccount.name=accName;
    }
    if(!accountList.isEmpty()){
        update accountList;
    }
}

If my suggestion(s) worked, do let me know by marking the answer as "Best Answer" right under the comment.
This will help the rest of the community should they have a similar issue in the future. 

Thank you..
pranav shah 40pranav shah 40
i am getting following error With above code

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger UpdateAccount caused an unexpected exception, contact your administrator: UpdateAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000ZWDryAAH; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccount: maximum trigger depth exceeded Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry] Account trigger event AfterUpdate for [0012800000ZWDry]: []: Trigger.UpdateAccount: line 12, column 1
Nithya tsNithya ts
trigger up_Account on Account (before insert, before update){
    
    
    list<account> liacc=new list<account>();
    for(Account thisAccount :trigger.new){
        String accName = 'XYZ';
        thisAccount.name=accName;
        liacc.add(thisAccount);
    }
   
       
    
}