Share Accounts with Standard Users.

This trigger automatically shares newly created Account records with a user holding the “Standard User” profile, granting them edit permissions. It works seamlessly with private OWD settings.

Learn more with this YouTube playlist.

Solution:

trigger AccountTrigger on Account (after insert) {
if(Trigger.isInsert){ if(Trigger.isAfter){
               AccountTriggerHandler.shareAccWithStdUser(Trigger.new);
            }
     }
}
public class AccountTriggerHandler{ public static void
            shareAccWithStdUser(List<Account> accList){

                     Id standartUserId = [SELECT Id FROM Profile WHERE
                     Name=’Standard User’].Id;
                     List<User> listOfUserId = [SELECT Id FROM User WHERE
                     ProfileId= :standartUserId AND IsActive=True LIMIT 1];
                     List<AccountShare> accShareList=new List();
                     for(Account acc:accList){
                             AccountShare aShare= new AccountShare();
                            aShare.UserOrGroupId=listOfUserId[0].Id;
                            aShare.AccountId=acc.Id;
                            aShare.RowCause=’Manual’;
                            aShare.AccountAccessLevel=’Edit’;
                            aShare.OpportunityAccessLevel=’Edit’;
                            accShareList.add(aShare);
                }
                if(!accShareList.isEmpty()){ insert accShareList;
                }
         }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)