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);
 
    }
 
}

Categories: Apex, Code Sample, Salesforce

Leave a comment

Comments Feed16 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.

  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.

  9. Jeff Douglas

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

  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?

  11. Hadassa Golovenshitz

    Thanks for the tip about using future method to update record in after update trigger! Saved me a lot of work!

  12. Steve

    Hi Jeff,

    Help!!!! I have tried to implement your code as above, without much luck.

    I have an object called Service Contracts, quite simply, when a checkbox on the detail is checked, I wish for the record to be automatically sent for approval.

    I have done the approval process – but didnt realise manual submission was req!

    Any help would be much apprecaited.

    Thanks in advance

  13. Steve

    If you just pate your code into a new opportunity trigger and hit save you get the following error:

    Error: Compile Error: expecting a right parentheses, found ‘;’ at line 3 column 49

  14. Neil

    Great post, thanks Jeff,

    I’m facing a problem in writing test methods for a class that retrieves approval history. How can i ensure that an approval process exists or create one in code as part of my tests? (This is part of a managed package so no control over the org’s config)

    Any ideas much appreciated!

  15. Nik's

    Great …. Information …

  16. Elly Bockley

    I am trying to write a similar trigger bu I need it bulkified. I did find an example where they pass a list to the Approval Process like this:
    Approval.process(reqList, true)

    But I get an error saying I am missing a required field – but who knows what? I can;t find any documentation other than submitting one record at a time which could of course cause problems.

    Any thoughts?
    Thanks
    Elly

Leave a comment

Feed

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

WordPress Appliance - Powered by TurnKey Linux