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
Melissa Mueller 9Melissa Mueller 9 

Why is my Approval Process instance not creating NewWorkItems?

I am trying to create an approval history report and have followed the basic instructions in this post: Create approval history report with object fields?

I am now onto getting the information from an approval process instance and am stuck because I cannot get the instance to create any NewWorkItems. I have followed the process found here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm

I have also noticed that my getActorIds() returns null and the getInstanceStatus() returns Approved which it absolutely should not.
Any ideas on why this is happening would be greatly appreciated.

Apex Class:
global class GetQuoteAndApprovalProcessIds {
@InvocableMethod(label='Get Quote And Approval Ids')
global static List<Id> getIds(List<Id> quoteIdList){
    Id quoteId = quoteIdList[0];
    System.debug('!!!!!!quoteId!!!!!' + quoteId);
    List<Id> quoteAndApprovalProcessIds = new List<Id>();
    Id actorId;
    Id originalActorId;
    Id processInstanceId;

    ProcessInstance instance = [SELECT Id FROM ProcessInstance WHERE TargetObjectId =: quoteId Limit 1];
    System.debug('!!!!!!instance.Id!!!!!' + instance.Id);

    ProcessInstance step = [SELECT CompletedDate, CreatedById, CreatedDate,Id,IsDeleted,LastActorId,LastModifiedById,LastModifiedDate,
                                ProcessDefinitionId,Status,SubmittedById,SystemModstamp,TargetObjectId, (SELECT ID, ProcessNodeId, StepStatus,
                                                                                                         Comments,TargetObjectId,ActorId,CreatedById,
                                                                                                         IsDeleted,IsPending,OriginalActorId,ProcessInstanceId,
                                                                                                         RemindersSent,CreatedDate 
                                                                                                         FROM StepsAndWorkitems ) 
                                FROM ProcessInstance WHERE ProcessInstance.Id =: instance.Id];
    System.debug('!!!!!!ProcessInstanceStep!!!!!' + step);
    return quoteAndApprovalProcessIds;
}   

global class QuoteAndApprovalIdResults {
    @InvocableVariable
    global Id quoteId;
    @InvocableVariable
    global List<Id> approvalProcessIds = new List<Id>();
}   
}

Apex Test Class:
@isTest(seeAllData = true)
public class GetQuoteAndApprovalProcessIds_Test {
static testMethod void testQuoteApprovalReportingObject(){
    // Commercial Sales Manager Joe Vincenti
    Id userId = '005i0000004yabM';
    User u = new User();
    u.Id = userId;
    // Using record types of commercial no pm
    Id recordTypeQuoteId = '01231000000y0r3';
    Id recordTypeProjectId = '01231000000y0qv';
    date projectCloseDate = date.parse('09/21/2018');
    Id priceBookId = '01s0Z000004H39F';

    System.runAs(u){
        Opportunity project = new Opportunity(name='Quote Reporting Test Project', CloseDate=projectCloseDate, StageName = 'Bid', Probability=70, Project_State__c='GA', Construction_Type__c='Retrofit', Competitors1__c='LG', Type='OEM', Federal_Government_Project__c='No', RecordTypeId=recordTypeProjectId);
        insert project;
        System.debug('!!!!!!project!!!!!' + project);

        SBQQ__Quote__c quote = new SBQQ__Quote__c(SBQQ__Status__c='New', Include_in_Forecasting__c='No', SBQQ__Opportunity2__c=project.Id, Approval_Justification__c='Facing strong competition from LG.', RecordTypeId=recordTypeQuoteId, SBQQ__PricebookId__c=priceBookId);
        insert quote;
        System.debug('!!!!!!quote!!!!!' + quote);

        List<Product2> productsList = [select Id, productCode, isActive, Product_Family__c, substitution_Product_code__c, Other_Category__c, Spec_Product__c, 
                                       Product_Bundle__c from Product2 where productCode = 'AE-201'];
        System.debug('!!!!!!productsList!!!!!!' + productsList);

        List<SBQQ__QuoteLine__c> quoteLines = new List<SBQQ__QuoteLine__c>();
        quoteLines.add(new SBQQ__QuoteLine__c(SBQQ__Quantity__c = 1, SBQQ__Product__c = productsList[0].Id, SBQQ__Quote__c = quote.Id, Multiplier__c = 0.1));

        insert quoteLines;
        System.debug('!!!!!!quoteLines!!!!!' + quoteLines);    

    // Create an approval request for the account
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitting request for approval.');
    req1.setObjectId(quote.id);

    // Submit on behalf of a specific submitter
    req1.setSubmitterId(u.Id); 

    // Submit the record to specific process and skip the criteria evaluation
    req1.setProcessDefinitionNameOrId('Commercial_Sales_Manager_Approval');
    req1.setSkipEntryCriteria(true);
    System.debug('!!!!!!req1!!!!!' + req1);

    // Submit the approval request for the account
    Approval.ProcessResult result = Approval.process(req1);
    // Verify the result
    System.assert(result.isSuccess());
    System.debug('!!!!!!result!!!!!' + result);

    // Approve the submitted request
    // First, get the ID of the newly created item
    List<Id> newWorkItemIds = result.getNewWorkitemIds();
    System.debug('!!!!!!newWorkItemIds!!!!!' + newWorkItemIds);

    // Instantiate the new ProcessWorkitemRequest object and populate it
    Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
    req2.setComments('Rejecting request. Discount to high! Please adjust by -5%.');
    req2.setAction('Rejected');

    // Use the ID from the newly created item to specify the item to be worked
    req2.setWorkitemId(newWorkItemIds.get(0));

    // Submit the request for approval
    Approval.ProcessResult result2 =  Approval.process(req2);

    ApexPages.currentPage().getParameters().put('Id', quote.Id);
    System.debug('*******ApexPages.currentPage().getParameters().get()*******' +      ApexPages.currentPage().getParameters().get('id'));
    Id quoteId = ApexPages.currentPage().getParameters().get('id');
    List<Id> quoteIdList = new List<Id>();
    quoteIdList.add(quoteId);            
    GetQuoteAndApprovalProcessIds.getIds(quoteIdList);
    } 
}
}

Debug Log (relevant output only due to this working with the Steel Brick managed package, there are tons of irrelevant lines): 

Project (AKA Opportunity) is created:

15:11:17.1 (493292522)|USER_DEBUG|[21]|DEBUG|!!!!!!project!!!!!Opportunity:{Name=Quote Reporting Test Project, CloseDate=2018-09-21 00:00:00, StageName=Bid, Probability=70, Project_State__c=GA, Construction_Type__c=Retrofit, Competitors1__c=LG, Type=OEM, Federal_Government_Project__c=No, RecordTypeId=01231000000y0qvAAA, Id=006e000000E9KqHAAV}

Steel Brick CPQ Quote and ProductList created:
15:11:18.87 (1322517172)|USER_DEBUG|[26]|DEBUG|!!!!!!quote!!!!!SBQQ__Quote__c:{SBQQ__Status__c=New, Include_in_Forecasting__c=No, SBQQ__Opportunity2__c=006e000000E9KqHAAV, Approval_Justification__c=Facing strong competition from LG., RecordTypeId=01231000000y0r3AAA, SBQQ__PricebookId__c=01s0Z000004H39FQAS, Id=a0Ue0000008h29HEAQ}
15:11:18.87 (1329296217)|USER_DEBUG|[30]|DEBUG|!!!!!!productsList!!!!!!(Product2:{Id=01t31000007kX7uAAE, ProductCode=AE-201, IsActive=true, Product_Family__c=PSG, Other_Category__c=5, Product_Bundle__c=false})

Quote Lines added and Approval.ProcessSubmitRequest created:
15:11:19.618 (3353871002)|USER_DEBUG|[36]|DEBUG|!!!!!!quoteLines!!!!!(SBQQ__QuoteLine__c:{SBQQ__Quantity__c=1, SBQQ__Product__c=01t31000007kX7uAAE, SBQQ__Quote__c=a0Ue0000008h29HEAQ, Multiplier__c=0.1, Id=a0Qe0000008rWDtEAM})
15:11:19.618 (3354137090)|USER_DEBUG|[54]|DEBUG|!!!!!!req1!!!!!Approval.ProcessSubmitRequest[getObjectId=a0Ue0000008h29HEAQ;getProcessDefinitionNameOrId=Commercial_Sales_Manager_Approval;getSkipEntryCriteria=true;getSubmitterId=005i0000004yabMAAQ;]

The result of submitting the process request and the lines that follow it to the end of the debug file:
15:11:20.623 (3624353425)|USER_DEBUG|[60]|DEBUG|!!!!!!result!!!!!Approval.ProcessResult[getActorIds=null;getEntityId=a0Ue0000008h29HEAQ;getErrors=null;getInstanceId=04ge000000173YgAAI;getInstanceStatus=Approved;getNewWorkitemIds=();isSuccess=true;]
15:11:20.623 (3624460915)|USER_DEBUG|[65]|DEBUG|!!!!!!newWorkItemIds!!!!!()
15:11:20.623 (3630604821)|FATAL_ERROR|System.ListException: List index out of bounds: 0

Class.GetQuoteAndApprovalProcessIds_Test.testQuoteApprovalReportingObject: line 74, column 1
15:11:20.623 (3630619052)|FATAL_ERROR|System.ListException: List index out of bounds: 0

Class.GetQuoteAndApprovalProcessIds_Test.testQuoteApprovalReportingObject: line 74, column 1
15:11:20.623 (3630636277)|CODE_UNIT_FINISHED|GetQuoteAndApprovalProcessIds_Test.testQuoteApprovalReportingObject
15:11:20.623 (3631347772)|EXECUTION_FINISHED