Approval Class

Contains methods for processing approval requests and setting approval-process locks and unlocks on records.

Namespace

System

Usage

Salesforce admins can edit locked records. Depending on your approval process configuration settings, an assigned approver can also edit locked records. Locks and unlocks that are set programmatically use the same record editability settings as other approval-process locks and unlocks.

Record locks and unlocks are treated as DML. They’re blocked before a callout, they count toward your DML limits, and if a failure occurs, they’re rolled back along with the rest of your transaction. To change this rollback behavior, use an allOrNone parameter.

Approval is also used as a namespace for the ProcessRequest and ProcessResult classes.

Approval Methods

The following are methods for Approval. All methods are static.

isLocked(id)

Returns true if the record with the ID id is locked, or false if it’s not.

Signature

public static Boolean isLocked(Id id)

Parameters

  • id: Type: Id The ID of the record whose lock or unlock status is in question.

Return Value

Type: Boolean

isLocked(ids)

Returns a map of record IDs and their lock statuses. If the record is locked the status is true. If the record is not locked the status is false.

Signature

public static Map<Id,Boolean> isLocked(List<Id> ids)

Parameters

  • ids: Type: List<Id> The IDs of the records whose lock or unlock statuses are in question.

Return Value

Type: Map<Id,Boolean>

isLocked(sobject)

Returns true if the sobject record is locked, or false if it’s not.

Signature

public static Boolean isLocked(SObject sobject)

Parameters

  • sobject: Type: SObject The record whose lock or unlock status is in question.

Return Value

Type: Boolean

isLocked(sobjects)

Returns a map of record IDs to lock statuses. If the record is locked the status is true. If the record is not locked the status is false.

Signature

public static Map<Id,Boolean> isLocked(List<SObject> sobjects)

Parameters

  • sobjects: Type: List<SObject> The records whose lock or unlock statuses are in question.

Return Value

Type: Map<Id,Boolean>

lock(recordId)

Locks an object, and returns the lock results.

Signature

public static Approval.LockResult lock(Id recordId)

Parameters

  • recordId: Type: Id ID of the object to lock.

Return Value

Type: Approval.LockResult

lock(recordIds)

Locks a set of objects, and returns the lock results, including failures.

Signature

public static List<Approval.LockResult> lock(List<Id> ids)

Parameters

  • ids: Type: List<Id> IDs of the objects to lock.

Return Value

Type: List<Approval.LockResult>

lock(recordToLock)

Locks an object, and returns the lock results.

Signature

public static Approval.LockResult lock(SObject recordToLock)

Parameters

Return Value

Type: Approval.LockResult

lock(recordsToLock)

Locks a set of objects, and returns the lock results, including failures.

Signature

public static List<Approval.LockResult> lock(List<SObject> recordsToLock)

Parameters

Return Value

Type: List<Approval.LockResult>

lock(recordId, allOrNothing)

Locks an object, with the option for partial success, and returns the lock result.

Signature

public static Approval.LockResult lock(Id recordId, Boolean allOrNothing)

Parameters

  • recordId: Type: Id ID of the object to lock.
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: Approval.LockResult

lock(recordIds, allOrNothing)

Locks a set of objects, with the option for partial success. It returns the lock results, including failures.

Signature

public static List<Approval.LockResult> lock(List<Id> recordIds, Boolean allOrNothing)

Parameters

  • recordIds: Type: List<Id> IDs of the objects to lock.
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: List<Approval.LockResult>

lock(recordToLock, allOrNothing)

Locks an object, with the option for partial success, and returns the lock result.

Signature

public static Approval.LockResult lock(SObject recordToLock, Boolean allOrNothing)

Parameters

  • recordToLock: Type: SObject
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: Approval.LockResult

lock(recordsToLock, allOrNothing)

Locks a set of objects, with the option for partial success. It returns the lock results, including failures.

Signature

public static List<Approval.LockResult> lock(List<SObject> recordsToLock, Boolean allOrNothing)

Parameters

  • recordsToLock: Type: List<SObject>
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: List<Approval.LockResult>

process(approvalRequest)

Submits a new approval request and approves or rejects existing approval requests.

Signature

public static Approval.ProcessResult process(Approval.ProcessRequest approvalRequest)

Parameters

Return Value

Type: Approval.ProcessResult

Example

1// Insert an account
2
3Account a = new Account(Name='Test',
4                     annualRevenue=100.0);
5
6insert a;
7
8// Create an approval request for the account
9Approval.ProcessSubmitRequest req1 = 
10      new Approval.ProcessSubmitRequest();
11req1.setObjectId(a.id);
12
13// Submit the approval request for the account
14Approval.ProcessResult result = 
15                   Approval.process(req1);

process(approvalRequest, allOrNone)

Submits a new approval request and approves or rejects existing approval requests.

Signature

public static Approval.ProcessResult process(Approval.ProcessRequest approvalRequest, Boolean allOrNone)

Parameters

  • approvalRequest: Approval.ProcessRequest
  • allOrNone: Type: Boolean The optional allOrNone parameter specifies whether the operation allows for partial success. If you specify false for this parameter and an approval fails, the remainder of the approval processes can still succeed.

Return Value

Approval.ProcessResult

process(approvalRequests)

Submits a list of new approval requests, and approves or rejects existing approval requests.

Signature

public static Approval.ProcessResult [] process(Approval.ProcessRequest[] approvalRequests)

Parameters

Return Value

Approval.ProcessResult []

process(approvalRequests, allOrNone)

Submits a list of new approval requests, and approves or rejects existing approval requests.

Signature

public static Approval.ProcessResult [] process(Approval.ProcessRequest[] approvalRequests, Boolean allOrNone)

Parameters

  • approvalRequests: Approval.ProcessRequest []
  • allOrNone: Type: Boolean The optional allOrNone parameter specifies whether the operation allows for partial success. If you specify false for this parameter and an approval fails, the remainder of the approval processes can still succeed.

Return Value

Approval.ProcessResult []

unlock(recordId)

Unlocks an object, and returns the unlock results.

Signature

public static Approval.UnlockResult unlock(Id recordId)

Parameters

  • recordId: Type: Id ID of the object to unlock.

Return Value

Type: Approval.UnlockResult

unlock(recordIds)

Unlocks a set of objects, and returns the unlock results, including failures.

Signature

public static List<Approval.UnlockResult> unlock(List<Id> recordIds)

Parameters

  • recordIds: Type: List<Id> IDs of the objects to unlock.

Return Value

Type: List<Approval.UnlockResult>

unlock(recordToUnlock)

Unlocks an object, and returns the unlock results.

Signature

public static Approval.UnlockResult unlock(SObject recordToUnlock)

Parameters

Return Value

Type: Approval.UnlockResult

unlock(recordsToUnlock)

Unlocks a set of objects, and returns the unlock results, including failures.

Signature

public static List<Approval.UnlockResult> unlock(List<SObject> recordsToUnlock)

Parameters

Return Value

Type: List<Approval.UnlockResult>

unlock(recordId, allOrNothing)

Unlocks an object, with the option for partial success, and returns the unlock result.

Signature

public static Approval.UnlockResult unlock(Id recordId, Boolean allOrNothing)

Parameters

  • recordId: Type: Id ID of the object to lock.
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: Approval.UnlockResult

unlock(recordIds, allOrNothing)

Unlocks a set of objects, with the option for partial success. It returns the unlock results, including failures.

Signature

public static List<Approval.UnlockResult> unlock(List<Id> recordIds, Boolean allOrNothing)

Parameters

  • recordIds: Type: List<Id> IDs of the objects to unlock.
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: List<Approval.UnlockResult>

unlock(recordToUnlock, allOrNothing)

Unlocks an object, with the option for partial success, and returns the unlock result.

Signature

public static Approval.UnlockResult unlock(SObject recordToUnlock, Boolean allOrNothing)

Parameters

  • recordToUnlock: Type: SObject
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: Approval.UnlockResult

unlock(recordsToUnlock, allOrNothing)

Unlocks a set of objects, with the option for partial success. It returns the unlock results, including failures.

Signature

public static List<Approval.UnlockResult> unlock(List<SObject> recordsToUnlock, Boolean allOrNothing)

Parameters

  • recordsToUnlock: Type: List<SObject>
  • allOrNothing: Type: Boolean Specifies whether this operation allows partial success. If you specify false and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that you can use to verify which records succeeded, which failed, and why.

Return Value

Type: List<Approval.UnlockResult>