You need to sign in to do that
Don't have an account?
Prasad Karagaonkar
Apex trigger code to : Whenever the user Updates the Account Record the Description of the Account Record will be changed to Loggedin User Name
// Code :
trigger AddDesctoAccount on Account (Before Update) {
Id userId= userinfo.getUserId();
User UserDetails = [select Id,Username from User where Id= :userId ];
String uName= UserDetails.Username;
List<Account> actToUpdate = new List<Account>();
for(Account acc: Trigger.new){
actToUpdate.add(acc);
}
for(Account act: actToUpdate){
act.Description=uName;
}
database.update(actToUpdate,false);
}
Output:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AddDesctoAccount caused an unexpected exception, contact your administrator: AddDesctoAccount: execution of BeforeUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Trigger.AddDesctoAccount: line 15, column 1
Please Help me with the code.
Thank You,
Prasad
trigger AddDesctoAccount on Account (Before Update) {
Id userId= userinfo.getUserId();
User UserDetails = [select Id,Username from User where Id= :userId ];
String uName= UserDetails.Username;
List<Account> actToUpdate = new List<Account>();
for(Account acc: Trigger.new){
actToUpdate.add(acc);
}
for(Account act: actToUpdate){
act.Description=uName;
}
database.update(actToUpdate,false);
}
Output:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger AddDesctoAccount caused an unexpected exception, contact your administrator: AddDesctoAccount: execution of BeforeUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Trigger.AddDesctoAccount: line 15, column 1
Please Help me with the code.
Thank You,
Prasad
Your trigger is already in the process of updating an Account record so you don't need to call database.update again, try removing the database.update(actToUpdate,false) line and see if that works.
you could also refactor the need to look up the running user and putting the accounts into a new list as Trigger.new is already a list, give this a go
the trigger should update your Description field with the user's username and then continue processing the save.
All Answers
Your trigger is already in the process of updating an Account record so you don't need to call database.update again, try removing the database.update(actToUpdate,false) line and see if that works.
you could also refactor the need to look up the running user and putting the accounts into a new list as Trigger.new is already a list, give this a go
the trigger should update your Description field with the user's username and then continue processing the save.
You will need to modify your trigger code as follows. Since you are executing a trigger on the Before event, the values within Trigger.new are editable. Moreover, a before trigger executes before the records are committed to the database and hence an explicit DML operation is not required. The values that you modify within Trigger.new are committed to the respective Sobject record if the committ goes through successfully.
I recommend you take the Apex Triggers trailhead (https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_intro)to get more acquainted with the same.
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.