Ran across this the other day and wanted to re-post it, looks easy enough to implement...
http://blog.jeffdouglas.com/2010/01/04/automating-salesforce-approval-processes-with-apex-triggers/
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());
}
}
}
|
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);
}
}
|
No comments:
Post a Comment