Trigger to Automatically Submit Record for Approval

Use Case:

You want all new records for an object to be submitted to an Approval Process, but you do not want to rely on users to manually click the ‘Submit for Approval’ button after creating the record.

Solution:

After creating and testing a new Approval Process (Setup / App Setup / Create / Workflow & Approvals / Approval Processes), implement this simple Apex Trigger (the following example is for the Account object). You will also need to create a test method to ensure code coverage if your current production Apex Classes do not provide adequate coverage. Developers may also want to add try / catch statements to ensure that exceptions are caught and handled properly.

The Apex Trigger code:

trigger accountApprovalSubmit on Account (after insert) {

	for (Account a : trigger.new) {

		Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();

		app.setObjectId(a.id);

		Approval.ProcessResult result = Approval.process(app);

	}

}
Mike Topalovich Salesforce Technical Architect in Chicago
Mike Topalovich - Salesforce Certified Force.com Platform Developer I Mike Topalovich - Salesforce Certified Force.com Platform Developer II Mike Topalovich - Salesforce Certified Force.com Developer Mike Topalovich - Salesforce Certified Force.com Advanced Developer
Mike Topalovich - Salesforce Certified Mobile Solutions Architecture Designer Mike Topalovich - Salesforce Certified Force.com Platform App Builder Mike Topalovich - Salesforce Certified Administrator Mike Topalovich - Salesforce Certified Advanced Administrator
Mike Topalovich

Hi, I’m Mike. I help companies like yours do business in new ways with Salesforce.


I am a freelance Salesforce Developer, Architect, and CTO as well as a part time instructor for Salesforce University.


Connect with me today to discuss how I can become a part of your team on an ongoing retainer basis.


Mike Topalovich on EmailMike Topalovich on FlickrMike Topalovich on LinkedinMike Topalovich on RssMike Topalovich on Twitter

Leave a Reply