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
mgodseymgodsey 

Trigger to update Account Owner with User ID stored in custom field

I have an approval process, and as a result of the approval process, I would like the system to update the Account Owner field to be the user who submitted the approval request. I've created two custom fields that I think should help me accomplish this.

 

1) a text field called Submitter_ID__c that automatically updates (as part of the approval process) to display the User ID of the user that submitted the approval request

 

2) a checkbox called Reassign_Account_Owner__c that will be marked as true as an action on the last approval step.

 

What I would like to do is build a trigger that fires when the Reassign_Account_Owner__c field is TRUE and updates the Account Owner using the User ID that is in the Submitter_ID__c field.

 

I'm new to Apex though and I'm not sure of the best way to start this. Does anyone have any ideas or anywhere they can point me to get some direction? Thank you very much!

 

Best Answer chosen by Admin (Salesforce Developers) 
craigmhcraigmh

Something like this

 

trigger AccountUpdated on Account (before insert, before update) {
	for(Account a: trigger.new) {
		if(a.Reassign_Account_Owner__c && a.Submitter_ID__c != null) { a.OwnerId = a.Submitter_ID__c; }
	}
}

 

 

All Answers

craigmhcraigmh

Something like this

 

trigger AccountUpdated on Account (before insert, before update) {
	for(Account a: trigger.new) {
		if(a.Reassign_Account_Owner__c && a.Submitter_ID__c != null) { a.OwnerId = a.Submitter_ID__c; }
	}
}

 

 

This was selected as the best answer
mgodseymgodsey

Thank you so much Craig! That worked like a charm. Really appreciate the help.