Apex Trigger to When an Account’s field Share_With_User__c (a lookup to User) is filled, automatically share that Account with that user via Apex sharing in Salesforce
To automatically share an Account with a user when a lookup field (Share_With_User__c) is populated, you must use Apex Managed Sharing via an Apex Trigger.
Because the Account is a standard object, Apex sharing is achieved by inserting records into the AccountShare object. This approach requires the Organization-Wide Default (OWD) for Account to be Private or Public Read Only.
Step 1 Account Trigger (After Update): AccountSharingTrigger.apxt
TRIGGER AccountSharingTrigger ON Account (after INSERT, after UPDATE) {
IF (TRIGGER.isAfter) {
IF (TRIGGER.isInsert || TRIGGER.isUpdate) {
AccountSharingHelper.shareAccountWithUser(TRIGGER.new, TRIGGER.oldMap);
}}}
Step 2 Apex Controller: AccountSharingHelper.apxc
public class AccountSharingHelper {public static void shareAccountWithUser(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {
List<AccountShare> sharesToCreate = NEW List<AccountShare>();
FOR (Account acc : newAccounts) {
// CHECK IF USER lookup IS populated AND either NEW OR changed
IF (acc.Share_With_User__c != NULL &&
(oldAccountMap == NULL || acc.Share_With_User__c != oldAccountMap.get(acc.Id).Share_With_User__c)) {
// Do NOT share WITH the owner (owner already has access)
IF (acc.Share_With_User__c != acc.OwnerId) {
AccountShare share = NEW AccountShare();
share.AccountId = acc.Id;
share.UserOrGroupId = acc.Share_With_User__c;
share.AccountAccessLevel = 'Edit'; // 'Read' OR 'Edit'
share.OpportunityAccessLevel = 'Read'; // Required
share.CaseAccessLevel = 'Read'; // Required
share.RowCause = 'Manual'; // FOR standard objects, USE 'Manual'
sharesToCreate.add(share);
}}}IF (!sharesToCreate.isEmpty()) {
DATABASE.insert(sharesToCreate, FALSE); // FALSE allows partial success
}}}
Key Implementation Notes
- Access Level: AccountAccessLevel can be set to ‘Read’ or ‘Edit’.
- RowCause: For standard objects, Apex sharing must use Schema.AccountShare.RowCause.Manual (or simply ‘Manual’).
- Limitations: If the Account owner changes, sharing records with RowCause as ‘Manual’ are automatically deleted.
- Alternative: For low-code environments, this can be achieved using an After-save Record-Triggered Flow by creating records in the AccountShare object.
Approach 2:
Write apex login directly to Apex Trigger
Step 1 Account Trigger (After Update): AccountSharingTriggerr.apxt
|
🎁 Up to 99% Off Courses (Coupon)
💥 Use Promo Code: STANDARDOFFER💥 🚀 Get Free Salesforce Course Access: www.thevijaykumar.w3web.net |
TRIGGER ManualAccountSharing ON Account (after INSERT, after UPDATE) {
List<AccountShare> sharesToInsert = NEW List<AccountShare>();
FOR (Account acc : TRIGGER.new) {
// CHECK IF Share_With_User__c IS populated
IF (acc.Share_With_User__c != NULL) {
// CREATE a manual share record
AccountShare accShare = NEW AccountShare();
accShare.AccountId = acc.Id;
accShare.UserOrGroupId = acc.Share_With_User__c; // USER TO share WITH
accShare.AccountAccessLevel = 'Edit'; // OR 'Read'
accShare.OpportunityAccessLevel = 'None';
accShare.CaseAccessLevel = 'None';
accShare.RowCause = Schema.AccountShare.RowCause.Manual; // Manual sharing reason
sharesToInsert.add(accShare);
}}IF (!sharesToInsert.isEmpty()) {
INSERT sharesToInsert;}}
Further post that would you like to learn in Salesforce
What is apex-based sharing?
Apex managed sharing enables developers to programmatically manipulate sharing to support their application's behavior through either Apex or SOAP API. This type of sharing is similar to managed sharing. Only users with “Modify All Data” permission can add or change Apex managed sharing on a record.
How to give apex class access to user?
Select a profile, and click its name. In the Apex Class Access page or related list, click Edit. Select the Apex classes that you want to enable from the Available Apex Classes list and click Add. Or select the Apex classes that you want to disable from the Enabled Apex Classes list and click Remove.
Is account sharing allowed on Apex?
Never share your credentials. Sharing your Apex Legends account info means your account could get stolen, and we may decline to restore it.
Related Topics | You May Also Like
|
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course → |



