Hi I am submitting record for approval in test class, and then again I am approving it. Still the status is showing as pending. Can anyone help here?

public static testMethod void ContractTestMethod()

   Account acc = new Account(Name = 'TestAcc');
    Insert acc;

    Contract c = new Contract(AccountId = acc.Id,
                              Name = 'TestContract',
    Insert c;

    Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

    //Submit the approval request
    Approval.ProcessResult result = Approval.Process(req);

    List<ProcessInstanceWorkitem> workItems = [
        SELECT Id, ProcessInstanceId 
        FROM ProcessInstanceWorkitem 
        WHERE ProcessInstance.TargetObjectId = :c.ID 

    List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
    for(ProcessInstanceWorkitem workItem : workItems){
        Approval.ProcessWorkitemRequest req1 = new Approval.ProcessWorkitemRequest();
        //Valid values are: Approve, Reject, or Removed. 
        //Only system administrators can specify Removed.
        req1.setComments('Your Comment.');
    Approval.ProcessResult[] processResults = Approval.process(requests);
    system.debug('*******Approval Status*****'+processResults);

    list<contract> con=new list<contract>();

  • What you have tried so far? – Santanu Boral Mar 7 '19 at 18:00
  • Can you query ``ProcessInstanceWorkitem` with the processInstance id's that you have approved? – Pranay Jaiswal Mar 7 '19 at 18:01
  • I have quried it is in still pending state. @pranay – Vivian Mar 7 '19 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.