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);
 
    }
 
}
VN:F [1.9.3_1094]
Rating: 2.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)
Automating Salesforce Approval Processes with Apex Triggers, 2.0 out of 10 based on 1 rating

Categories: Apex, Code Sample, Salesforce

Leave a comment

Comments Feed10 Comments

  1. Prasu

    Thanks for this great information!

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  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

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  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 ?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  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.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  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?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  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?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  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.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  8. Jaspreet

    hi,
    i am trying to create a delay between two approval steps of some days. Means if an approval step has been approved then next approval step wil be approved after a week.
    please help me out i am new to salesforce.

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  9. Jeff Douglas

    What about using some sort of time-based workflow instead?

    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  10. giribabu

    hi,

    i am inplimenting approval process on the after update trigger and the erro is process_alrady in progress.
    is it that the approval process be deactivated?

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

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

WordPress Appliance - Powered by TurnKey Linux