You need to sign in to do that
Don't have an account?
pranavshah
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;
}
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;
}
If you are looking to write a trigger which will update the Account name based on the contacts, then below is the working code,
Please mark this as the best answer if this helps
All Answers
If you are looking to write a trigger which will update the Account name based on the contacts, then below is the working code,
Please mark this as the best answer if this helps
will u please Explain me the Logic For Above code
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..
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
list<account> liacc=new list<account>();
for(Account thisAccount :trigger.new){
String accName = 'XYZ';
thisAccount.name=accName;
liacc.add(thisAccount);
}
}