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
Prasad KaragaonkarPrasad 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
Best Answer chosen by Prasad Karagaonkar
Damon ShawDamon Shaw
Hi 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
 
trigger AddDesctoAccount on Account (Before Update) {
 
    for(Account acc: Trigger.new){
        act.Description = UserInfo.getUserName();
    }
}

the trigger should update your Description field with the user's username and then continue processing the save. 
 

All Answers

Damon ShawDamon Shaw
Hi 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
 
trigger AddDesctoAccount on Account (Before Update) {
 
    for(Account acc: Trigger.new){
        act.Description = UserInfo.getUserName();
    }
}

the trigger should update your Description field with the user's username and then continue processing the save. 
 
This was selected as the best answer
jigarshahjigarshah
Prasad,

You will need to modify your trigger code as follows.
trigger AddDesctoAccount on Account (Before Update) {
 
	//Add this condition to ensure your business logic only executes on a Before Update event
	if(Trigger.isBefore){
	
		if(Trigger.isUpdate){
		
			User UserDetails = [select Id,Username from User where Id= :userinfo.getUserId()];
			String uName = UserDetails.Username;
			
			//Trigger.new values are editable within a Before event
			for(Account acc: Trigger.new){
				acc.Description = uName;
			}
		}
	}    
}
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.
Prasad KaragaonkarPrasad Karagaonkar
THANK YOU DAMON AND JIGAR,  Those were the valuable suggestions.