+ Start a Discussion
Pooja05Pooja05 

Calling approval process from the apex class

I am using the following code to submit request to the approval process

 

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(quote.id);

 

However it doesnt trigger the approval process. (I mocked up data such that it meets the approval critieria.)

 

When I click on the submit for approval button on the related list ProcessSteps (Approval History) included in the VF page, it works fine. But the custom code above doesnt work.

 

 

Can you please help?

 

 

Thanks,

Pooja

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae
I haven't done it that way, but there is additional examples in the documentation on how to advance the work item and "auto approve" it.  I would look there first.

All Answers

JimRaeJimRae

That just creates the request, you aslo need to start the process.

 

 

Approval.ProcessResult result = Approval.process(req1);

 Then you can interrogate the result to make sure it submitted.

 

There is a full example of this in the Apex developers guide(page 296-297)

 

Pooja05Pooja05

thanks.

 

It now works when the criteria is met but doesnt work when the criteria is not met.

 

Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process found.

An unexpected error has occurred. Your development organization has been notified.

 

 

Is there any way I can catch this error. Basically when the criteria is not met, an approval is not required. I just need to make the status auto approved.

 

I want to achieve the following

If no applicable process 

  then status = autoapproved.

 

 

Thanks,

Pooja

 

JimRaeJimRae
I haven't done it that way, but there is additional examples in the documentation on how to advance the work item and "auto approve" it.  I would look there first.
This was selected as the best answer