You need to sign in to do that
Don't have an account?
Basil Choudhry
Apex Approval Trigger Test
I'm trying to get an opportunity to be sent to be approved when a checkbox is ticked (the checkbox will be ticked by various workflows).
This is the code I've cobbled together from various sources:
trigger OpportunitySubmitForApproval1 on Opportunity (after update) {
for (Opportunity a : trigger.new) {
if (a.Send_for_Approval__c == TRUE) {
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
ID addendumId = a.Id;
Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
request.setObjectId(addendumId);
Approval.ProcessResult requestResult = Approval.process(request);
}
}
}
This is working fine for me in Sandbox. However, I'm having trouble making a test class to bring it over to production. The test class I've cobbled together is
@isTest
public class OpportunitySubmitForApproval1Test {
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';
opp.Send_for_Approval__c = FALSE;
// insert the new opp
insert opp;
// change the probability of the opp so the trigger submits it for approval
opp.Probability = 40;
opp.Send_for_Approval__c = TRUE;
// 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);
}
}
This is the code I've cobbled together from various sources:
trigger OpportunitySubmitForApproval1 on Opportunity (after update) {
for (Opportunity a : trigger.new) {
if (a.Send_for_Approval__c == TRUE) {
Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
ID addendumId = a.Id;
Approval.ProcessSubmitRequest request = new Approval.ProcessSubmitRequest();
request.setObjectId(addendumId);
Approval.ProcessResult requestResult = Approval.process(request);
}
}
}
This is working fine for me in Sandbox. However, I'm having trouble making a test class to bring it over to production. The test class I've cobbled together is
@isTest
public class OpportunitySubmitForApproval1Test {
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';
opp.Send_for_Approval__c = FALSE;
// insert the new opp
insert opp;
// change the probability of the opp so the trigger submits it for approval
opp.Probability = 40;
opp.Send_for_Approval__c = TRUE;
// 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);
}
}
Also, when the test class fails in Production can you check your debug logs to verify where exactly it failed?
Hope this helps.