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
DoctorDoctor 

Revoke sharing using APEX

I know it is possible to create Sharing using APEX, but is it possible to revoke sharing based on a field value?

 

We want to be able to check a checkbox called 'Private' and have all sharing of that record removed.

 

Thanks in advance.

Ispita_NavatarIspita_Navatar

As you said sharing can be added using Apex , I think it can be removed using Apex , I am attaching a code snippet where in "All Users" group is added to an OpportunitySharing.

 

// After inserting the opportunity we are adding the group 'All Users'
        // to the sharing model of the opportunity.
        if(Trigger.isAfter)
        {
            ID GroupID;
            try
            {
                GroupID = [Select g.Id from Group g where Name='All Users'].Id ;
            }
            catch(System.QueryException e)
            {
            }
            OpportunityShare[] NewOpportunityShares = new OpportunityShare[]{};
            if(GroupID != null)
            {
                for(Opportunity o : Trigger.new)
                {
                       OpportunityShare NewOpportunityShare = new OpportunityShare(UserOrGroupId = GroupID ,OpportunityId = o.Id,OpportunityAccessLevel='Edit');
                       NewOpportunityShares.add(NewOpportunityShare);
                   
                }
                insert NewOpportunityShares;  
            }
        }

 

Since new sharing was implemented by creating a new OpportunityShare record similarly sharing can be removed by deleting such record.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

 

 

DoctorDoctor

Thanks for the suggestion.

Will give it a try and let you know how it goes.