function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mgodseymgodsey 

Errors with Unit Test for Approval Process Trigger

I have a trigger that captures the approver ID in an approval process and updates a field with it on the Opportunity. I'm trying to write the unit test for it, but I'm running into errors. It is saying that there are required fields, but I have no idea what these fields are or how to set them. Has anyone been able to resolve something like this before?

 

Below is the specific error message, trigger code, and test code. Thank you so much for the help!

 

Trigger:

rigger TagSalesPlanner on Opportunity (after update) {

for (Opportunity opp : Trigger.new) {
Opportunity OldOpp = Trigger.oldMap.get(opp.ID);
if ((oldopp.ApprovalStatus__c != 'Approved' && opp.ApprovalStatus__c == 'Approved') || (oldopp.ApprovalStatus__c != 'Rejected' && opp.ApprovalStatus__c == 'Rejected')){

//get list of Approved Opportunities
List<Opportunity> ApprovedOpps = [SELECT ID, ProposalApprovers__c
FROM Opportunity
WHERE Id in: trigger.new];
    system.debug('* * * * Approved Opps List* * * *' + ApprovedOpps);

//get list of related Approval Processes
List<ProcessInstance> ap = [SELECT ID, TargetObjectID
                            FROM ProcessInstance
                            WHERE TargetObjectID in : ApprovedOpps];
                            system.debug('processInstance List' + ap);

List<ProcessInstanceStep> actor = [SELECT ActorID,ProcessInstanceID
                                   FROM ProcessInstanceStep
                                   WHERE (StepStatus='Approved' OR StepStatus='Rejected') AND ProcessInstanceID in: ap];
                                   system.debug('processInstanceStep List' + actor);

/*****************************************************
To get the list of proposal approvers in the opportunity record , I have created Maps and List to get the records.
**********************************************************/

Map<ID,List<ID>> pactors = new  Map<ID,List<ID>>();
 List<ID> l;
for(ProcessInstanceStep pa : actor){
        if(pactors.get(pa.ProcessInstanceID) == null){
           l = new List<ID>();
           l.add(pa.ActorID);
           pactors.put(pa.ProcessInstanceID,l);
           }
        else
            pactors.get(pa.ProcessInstanceID).add(pa.ActorID);
    
    }
  system.debug('processinstancemap '+ pactors);  
  
  
Map<ID,List<ID>> actors = new Map<ID,List<ID>>();
for(ProcessInstance pi: ap){
    actors.put(pi.TargetObjectID,pactors.get(pi.ID));
}
    system.debug('Process Instance map'+ actors);

for(Opportunity opp1 : ApprovedOpps){
    List<ID> approverIds = actors.get(opp1.ID);
    system.debug('approver Ids'+ approverIds);

opp1.ProposalApprovers__c='';
for (Integer i=0; i<approverIds.size(); i++){
    opp1.ProposalApprovers__c = opp1.ProposalApprovers__c + ','+ approverIds[i];}
    system.debug('Proposal approvers'+ opp1.ProposalApprovers__c);
}
update ApprovedOpps;
}
}
}

 

Unit Test

@isTest(SeeAllData = true)
private class Test_TagSalesPlanner{
    static testMethod void testTagSalesPlanner(){

//Set up the test records for Master Terms

Account account = new Account();
    account.Name='TestAccount';
    account.website='test.com';
    account.Industry='Entertainment';
       insert account;

Opportunity opportunity = new Opportunity();
    opportunity.AccountId = account.Id;
    opportunity.Name = 'Test Opportunity';
    opportunity.CloseDate = System.today();
    opportunity.StageName = 'In Negotiations';
    opportunity.Amount = 10000.00;
    opportunity.Type = 'New Client';
    opportunity.HasMasterDDForm__c = null;
    opportunity.approvalstatus__c = 'Not Submitted';
    opportunity.rate_type__c = 'CPM';
    
    System.debug('******** Pricebook Entry ID***********' + opportunity.PricebookEntryID__c);
    System.debug('******** Pricebook Entry Name__c ***********' + opportunity.workflowPricebookEntryName__c);
    
    insert opportunity;
    
    List<Opportunity> oppList1 = [SELECT id, workflowPricebookEntryName__c, PricebookEntryID__c FROM Opportunity WHERE id =:opportunity.id];
    System.debug('********* Pricebook Entry ID After Insert*****' + oppList1[0].PricebookEntryID__c);
    System.debug('********* Pricebook Entry Name After Insert*****' + oppList1[0].workflowPricebookEntryName__c);
    
ProposalFlight__c proposalflight = new ProposalFlight__c();
   proposalflight.opportunity__c = opportunity.Id;
   proposalflight.flightbudget__c = 100;
   proposalflight.flightstartdate__c = system.today();
   proposalflight.flightenddate__c = system.today()+60;
   proposalflight.approvalstatus__c = 'Not Submitted';
   proposalflight.versiontype__c = 'Master Terms';
   insert proposalflight;
   
ProcessInstance processInstance = new ProcessInstance();
    processInstance.TargetObjectID = opportunity.ID;
    processInstance.Status = 'Pending';
    insert processInstance;
    
ProcessInstanceStep piStep = new ProcessInstanceStep();
    piStep.StepStatus = 'Approved';
    piStep.ProcessInstanceID = processInstance.ID;
    piStep.ActorId = '00570000001ups6';

//cause trigger to fire    
    Test.startTest();
    insert piStep;
    Test.stopTest();
    system.debug('*********Approver IDs***********' + opportunity.ProposalApprovers__c);
    
//Validate that the Opportunity Flight Approval Status was updated
    List<Opportunity> oppList = [SELECT id, approvalstatus__c FROM Opportunity WHERE id =:opportunity.id];
    System.assertequals('Approved', oppList[0].approvalStatus__c);
   
//Validate that the Proposal Flight Approval Status was updated
    List<ProposalFlight__c> pfList = [SELECT id, approvalstatus__c FROM ProposalFlight__c WHERE id = :proposalflight.id];
    System.assertequals('Approved',pfList[0].approvalstatus__c);   
   }
   }

 

Error Message:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProcessDefinitionId, CurrentNodeId]: [ProcessDefinitionId, CurrentNodeId]Class.Test_TagSalesPlanner.testTagSalesPlanner: line 45, column 1

 

 

harsha__charsha__c

Hi, 

 

we dont have create permission for ProcessInstanceStep in salesforce.

 

In this case In the test class do the following

 

> Query on ProcessInstanceStep

> Assign to actor 

 

Means do the same as you did in the Class and just change the ProcessInstanceId

 

I hope it will work..!

 

Any how if it works, then mark it as a solution for other's reference

 

 

James (CloudAnswers)James (CloudAnswers)

Since this is the top search result for ProcessInstance, I wanted to leave the note that you no longer get results from the ProcessInstance query in api v26 - so you actually have to trigger a workflow via `Approval.process(request);`.

Admin1112Admin1112

my approval process works fine but getting error in test coverage, can you please help, below is the code and error.

 

 

@isTest(SeeAllData=true)
private class TestOpportunitySubmitForApproval {

static testMethod void myUnitTest() {
// TO DO: implement unit test
Opportunity opp = new Opportunity ();
opp.StageName = 'Demo Call';
opp.Name = 'Test 1';
opp.CloseDate = Date.today();
opp.Work_Order_Discount_1__c = 10;
insert opp;
opp.Work_Order_Discount_1__c = 25;
opp.Id='006O0000002bhG0';
update opp ;


List<ProcessInstance> ap = [SELECT ID,Status, TargetObjectID
FROM ProcessInstance
WHERE TargetObjectID= :opp.id];
system.debug('processInstance List' + ap);

ProcessInstance processInstance = new ProcessInstance();
processInstance.TargetObjectId = opp.Id;
processInstance.Status = 'Pending';
insert processInstance;

List<ProcessInstanceStep> actor = [SELECT ActorID,ProcessInstanceID
FROM ProcessInstanceStep
WHERE (StepStatus='Approved' OR StepStatus='Rejected') AND ProcessInstanceID in: ap];
system.debug('processInstanceStep List' + actor);

ProcessInstanceStep piStep = new ProcessInstanceStep();
piStep.StepStatus = 'Approved';
piStep.ProcessInstanceID = processInstance.ID;
piStep.ActorId = '00590000000vc59';


}
}

 

------

 

error

 

|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProcessDefinitionId, CurrentNodeId]: [ProcessDefinitionId, CurrentNodeId]