
Use more general search terms.
Select fewer filters to broaden your search.
You are here:
Share temporary access to related records with your approvers by setting up triggers. After the approval action is complete, use a trigger to withdraw the record access.
When you submit a record for approval, the approver must have access to the record’s related object to perform actions on the approval request. If your object’s internal organization-wide sharing default is Private, you can use a trigger to create a share object that grants access to the approver. After the record is approved, you can withdraw the record access by using a trigger to delete the share record.
If you're sharing access to a custom object, you can create an Apex sharing reason and include it in the trigger. An apex sharing reason restricts the record access to your specific use case.
You can customize the sample code snippet to suit your specific use case.
trigger AWIUpdate on ApprovalWorkItem(after update) {
List<ApprovalWorkItem> awiList = new List<ApprovalWorkItem>();
// Get the related opportunities for the accounts in this trigger
awiList = [SELECT Id, RelatedRecordId, AssignedToId FROM ApprovalWorkItem WHERE Id IN :Trigger.new];
for (ApprovalWorkItem awi : awiList) {
// Use the related record id, to delete the share record created for the user
Id relatedRecordId = awi.RelatedRecordId;
Id assignedToId = awi.AssignedToId;
SObjectType relatedRecordType = relatedRecordId.getSObjectType();
if (relatedRecordType.getDescribe().isCustom()) {
Map<String, Schema.SObjectType> tokens = Schema.getGlobalDescribe();
String relatedRecordShareName = relatedRecordType.getDescribe().getName().removeEnd('__c') + '__Share';
if(tokens.containsKey(relatedRecordShareName)) {
// Share object exists
SObject[] customShare = Database.query('SELECT Id FROM '
+ relatedRecordShareName + ' WHERE '
+ ' RowCause = \'AdvancedApprovalTriggerShareReason__c\' '
+ ' AND ParentId = :relatedRecordId'
+ ' AND UserOrGroupId = :assignedToId');
} else {
// OWD is Public Read Write
}
}
}
}

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.