0

i have a test method,creating a record then submitting to the approval by using process builder,then i have to approve the approval request,

@isTest
private class RequireRejectionCommentTest
{

     private static testmethod void testRejectionWithoutComment()
    {

        Id testWorkItemId = generateObject();
        Approval.ProcessWorkitemRequest testRej = new Approval.ProcessWorkitemRequest();
        testRej.setComments('');
        testRej.setAction  ('Reject');      
        testRej.setWorkitemId(testWorkItemId );



            try
            {
                Approval.ProcessResult testRejResult =  Approval.process(testRej);
                system.assert(true, 'A rejection with no comment should cause an exception');
            }
            catch(DMLException e)
            {
                system.assertEquals('Please provide a rejection reason!', 
                                    e.getDmlMessage(0), 
                  'Please provide a rejection reason!'); 
            }

    }

 private static Id generateObject()
    {

        TR_Currency_Approval__c testIS = new TR_Currency_Approval__c();
        testIS.Active__c=true;
        insert testIS;

  return Id;
    }
} 

in the generateObject method i need to get the id of the approval request submitted by process builder. if we are submitting the record for approval programmatically,we used to wrte something like this

Approval.ProcessSubmitRequest testReq = new Approval.ProcessSubmitRequest(); testReq.setObjectId(testIS.Id);
 Approval.ProcessResult reqResult = Approval.process(testReq);
 return reqResult.getNewWorkitemIds()[0];

I struck here please help me out guys, Thanks in advance

1

First You can create an approval request.Like method below.

public void submitApproval(Object obj)
    {
        // Create an approval request for the Repective object
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approve');
        req1.setObjectId(obj.id);
        req1.setNextApproverIds(new Id[] {obj.Next_Approver__c});

        // Submit the approval request for the respective object
        Approval.ProcessResult results = Approval.process(obj);

    }

Class ProcessSubmitRequest is used to automatically submit the approval process. We need to set following items while submitting the approval process using trigger:

  1. Comment
  2. TargetObjectId
  3. NextApproverIds – if needed. Here Custom logic can be written to dynamically set approver for approval process. In this case I am using the custom field present on the Opportunity.

You can then approve and reject records cover all the usecases.

The following post will explain you briefly about every use case.Dynamic approval process

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.