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
SFDC@ErrorSFDC@Error 

Apex Sharing rule on personal/public group of a user

Hi All

How can i share the record using apex sharing on the personal group? In user level, i am adding the personal group for sharing the record.is it possible ?
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

I trust you are doing very well.

You can share a record using Apex with a User, Public Group or Role. Below is the sample code for sharing of an Account record with a user.
 
AccountShare Acctshr = new AccountShare();
Acctshr.AccountId = aid;
Acctshr.UserOrGroupId = 'User.Id'; // or group id
Acctshr.AccountAccessLevel = 'Edit';
Acctshr.OpportunityAccessLevel = 'Edit';
Database.SaveResult sr = Database.insert(Acctshr,false);

Please refer to the below links which contains sample code for Apex sharing.

http://sfdcsrini.blogspot.com/2015/01/what-is-apex-sharing-how-to-share.html

https://sfdcfanboy.com/2015/11/27/apex-based-sharing-records-with-roles/

https://www.jitendrazaa.com/blog/salesforce/apex-based-sharing-in-salesforce/


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
SFDC@ErrorSFDC@Error
Thanks Khan Anas,I have tried this, but it is not working for me.I want a dynamic share on user's public group.for example, i am creating the group on a particular user in users details page.How can i share record to a public group?
Public without sharing class clsAccoutShare{

    public void AccountMgrIns(id usrid, id Accntid){

        AccountShare share1 = new AccountShare();

        share1.AccountId=accntid;

        share1.UserOrGroupId=usrid;

        share1.AccountAccessLevel='Read';

        share1.OpportunityAccessLevel='Read';

        share1.CaseAccessLevel='Read';

        Insert share1;

    }

    public void AccountMgrHierIns(id pusrid, id pAccntid){

        Map<String,String> mPrnt = new Map<String,String>();

        //Map<String,String> mUsrId = new Map<String,String>();

        List<AccountShare> shares = new List<AccountShare>();

        List<UserRole> ur = new List<UserRole>([SELECT Id, Name, ParentRoleId FROM UserRole WHERE Id IN (SELECT UserRoleId  from user u where u.id=:pusrid)]);

        for(UserRole u : ur){

            mPrnt.put(String.valueOf(u.get('ParentRoleId')),String.valueOf(u.get('id')));

        }

        system.debug(mPrnt);

        for(User u1 : [Select id from user u where UserRoleId in :mPrnt.keySet()]){

            //mUsrId.put(String.valueOf(u1.get('id')),String.valueOf(u1.get('id')));

            AccountShare share2 = new AccountShare();

            share2.AccountId=pAccntid;

            share2.UserOrGroupId=u1.Id;

            share2.AccountAccessLevel='Read';

            share2.OpportunityAccessLevel='Read';

            share2.CaseAccessLevel='Read';

            shares.add(share2);

        }

        Insert shares;

    }

}