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
daniel.duartedaniel.duarte 

Approving with Apex

Hello,

I have created an approval process for a custom object MyObject__c with two approval steps, and have assigned different users with different profiles as approvers for each step. Another user, associated with a profile with less privileges, creates a record of MyObject__c and submits it for approval. I have then created a command button associated with an action that creates a new version of the record: it clones the record and all of its children, submitting it for approval if the old record had any approval process pending. The old record has its status changed to 'Inactive', and the new record, or new version, is an exactly copy of the old one.

Using the classes ProcessInstanceHistory, ProcessWorkitemRequest, and other Approval classes, I am able to detect if the old record was approved in step one, or step two, or rejected, and I am also able to submit for approval and approve the new version using Apex. It works perfectly if I use the System Administrator user and click on the Create New Version button. But when I'm logged as the basic user that creates the record and is not assigned as an approver, I am not able to approve using Apex Code. I get an "Insufficient Privileges" error message. Is there anyway that a user can trigger an action that approves an object using apex?

Thanks

Ritesh AswaneyRitesh Aswaney
The record is usually locked as part of the approval process, editable only to the approved and system admin.

You might want to check that the Profile of the user you're trying to approve with has security access to the apex classes implementing your approval. Also you might want to try adding 'without sharing' to your apex class declaration.