You need to sign in to do that
Don't have an account?

Approval Process Test Class Help
I can't seem to figure out how to get code coverage a few lines in my Lead Trigger that validates on an approval process.
Apex Trigger
processInstanceIds.add(lds.ProcessInstances[0].Id);
and
if((pi.Steps[0].StepStatus == 'Rejected')){
rejectLeads.get(pi.TargetObjectId).addError('Operation Cancelled: Please provide a rejection reason on the Lead!');
Test Class
Apex Trigger
trigger RequireRejectionPartReg on Lead (before update) { Map<Id, Lead> rejectLeads = new Map<Id, Lead>{}; Id gd = Schema.SObjectType.Lead.RecordTypeInfosByName.get('GlobalPRM Partner Registration').RecordTypeId; for(Lead ld : Trigger.new){ if((ld.Rejection_Reason__c ==null && ld.Registration_Status__c =='Rejected')){ if(ld.RecordTypeId == gd){ rejectLeads.put(ld.id, ld); rejectLeads.get(ld.id).addError('Operation Cancelled: Please provide a rejection reason on the Lead!'); } } if(!rejectLeads.isEmpty()){ List<Id> processInstanceIds = new List<Id>{}; for(Lead lds: [SELECT(SELECT Id FROM ProcessInstances ORDER BY CreatedDate DESC LIMIT 1) FROM Lead WHERE Id IN :rejectLeads.keySet()]){ if(processInstanceIds.size() > 0){ processInstanceIds.add(lds.ProcessInstances[0].Id); } } for(ProcessInstance pi : [SELECT TargetObjectId,(SELECT Id, StepStatus, Comments FROM Steps ORDER BY CreatedDate DESC LIMIT 1) FROM ProcessInstance WHERE Id IN : processInstanceIds ORDER BY CreatedDate DESC]){ if((pi.Steps[0].StepStatus == 'Rejected')){ rejectLeads.get(pi.TargetObjectId).addError('Operation Cancelled: Please provide a rejection reason on the Lead!'); } } } } }Lines 19, 25 and 26 I'm still not getting code coverage.
processInstanceIds.add(lds.ProcessInstances[0].Id);
and
if((pi.Steps[0].StepStatus == 'Rejected')){
rejectLeads.get(pi.TargetObjectId).addError('Operation Cancelled: Please provide a rejection reason on the Lead!');
Test Class
@isTest public class TestRequireRejection { static testMethod void testRequireRejectMethod(){ test.startTest(); Id gl = [SELECT Id FROM RecordType WHERE Name='GlobalPRM Partner Registration' AND SObjectType='Lead' limit 1].Id; Lead l = new lead(); l.FirstName = 'test'; l.LastName = 'mctesty'; l.LeadSource = 'Banner'; l.Company = 'Test Company'; l.Business_Segment__c = 'Connect'; l.RecordTypeId = gl; l.Region__c = 'EMEA'; l.Areas_of_Interest__c = 'Readers'; l.Role__c = 'End-User'; l.Rejection_Reason__c = ''; insert l; Lead upl = [SELECT Id, Registration_Status__c FROM Lead WHERE Id = :l.id]; upl.Registration_Status__c = 'Rejected'; try{ update upl; system.debug(upl); } catch(Exception e){ system.debug(e.getMessage()); Boolean expectedException = e.getMessage().contains('Operation Cancelled: Please provide a rejection reason on the Lead!') ? true: false; system.assertEquals(expectedException, true); } Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest(); app.setObjectId(upl.id); app.setNextApproverIds(new Id[] {UserInfo.getUserId()}); Approval.ProcessResult result = Approval.process(app); test.stopTest(); } }
Thanks
Shashikant
All Answers
There seems to be issue in your execution order of test class code.
You are updating lead first which will invoke the trigger code but at this point you have not submitted yet for approval which starts from line 38. So when trigger gets executed there will not be any record in ProcessInstance object.
May be if you make another update after the submittion to approval process you might get code cover.
If it does not work let me know What is the condition for entering the Approval Process ?
Thanks
Shashikant
Thanks
Shashikant