Automating Salesforce Approval Processes with Apex Triggers

January 4th, 2010

This question came up on LinkedIn asking how to automatically fire off an approval process when an Opportunity reaches 30% probability. This example was on my to do list so I thought I would knock it out quickly.

The trigger fires when an Opportunity is updated and is submitted for approval if the Opportunity’s probability has moved from less than 30% to greater than or equal to 30%.

For the trigger to work you need to have an approval process with matching criteria. Mine is fairly simple and is where the Opportunity owner is the current user (there is only one user in a DE org). The trigger makes no attempt to trap for errors if an approval process doesn’t exist as I didn’t have time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
trigger OpportunitySubmitForApproval on Opportunity (after update) {
 
	for (Integer i = 0; i < Trigger.new.size(); i++) {
 
		if (Trigger.old[i].Probability < 30 && Trigger.new[i].Probability >= 30) {
 
			// create the new approval request to submit
			Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
			req.setComments('Submitted for approval. Please approve.');
			req.setObjectId(Trigger.new[i].Id);
			// submit the approval request for processing
			Approval.ProcessResult result = Approval.process(req);
			// display if the reqeust was successful
			System.debug('Submitted for approval successfully: '+result.isSuccess());
 
		}
 
	}
 
}

Here is the test class but you might want to enhance it to handle bulk operations and Opportunities that are not submitted for approval because their probability is not greater than 30%.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@isTest
private class TestOpportunitySubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.Amount = 100;
        opp.CloseDate = Date.today();
        opp.Probability = 10;
        opp.StageName = 'Prospecting';
        // insert the new opp
        insert opp;
        // change the probability of the opp so the trigger submits it for approval
	opp.Probability = 40;
	// update the opp which should submit it for approval
	update opp;
 
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];
	System.assertEquals(processInstances.size(),1);
 
    }
 
}
  • Share/Bookmark

Related posts:

  1. Enhancing the Lead Conversion Process in Salesforce.com
  2. Relationship Lookup Objects in Triggers are NULL?
  3. Writing Bulk Triggers for Salesforce.com
  4. Triggers and Order of Execution
  5. How to Unit Test Sending Mail in Apex

Categories: Apex, Salesforce

Leave a comment

Comments Feed7 Comments

  1. Prasu

    Thanks for this great information!

  2. John Coppedge

    Nice example Jeff! Approval through workflow rule will hopefully be an option in the future http://sites.force.com/ideaexchange/ideaView?c=09a30000000D9xt&id=08730000000BreoAAC

  3. Sayeed

    great example Jeff.. I was looking for a similar example. In my case I am implementing approval process via after update trigger. User clicks a check box (ready to submit) and saves a custom object record. In the after update trigger am using your code to submit the record for approval process. However, I get a run time Apex script error “Record is read only”.

    Any thoughts ?

  4. jeffdonthemic

    You cannot update the same record in the after update trigger. You’ll have to look at alternatives like using a @future method call or re-querying for the records.

  5. Kristofor

    In my understanding, whenever we submit something into the approval process, the object is locked with the changes already made to the record. Those changes seem to be undone if the approval is rejected, not released upon final approval. Is there another process that can handle that situation, where the change is committed only after the final approval?

  6. Kristofor

    The approval process commits the change to the record and then locks it down. Is there another process out there that does not commit the change to the record until the approval has been granted? I don’t like the idea of having the change attached to the record until the approval has actually been granted. Any ideas?

  7. jeffdonthemic

    Not that I’ve heard of but you may want to post it to the Salesforce.com message board. Perhaps one of the PMs will pick up you questions.

Leave a comment

Feed

http://blog.jeffdouglas.com / Automating Salesforce Approval Processes with Apex Triggers