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
StephenBStephenB 

Apex sharing rules for standard objects?

Hi all, I am running into some problems trying to use Apex to manipulate the sharing rules. SFDC lets you do this for custom objects, but their doc says that std objects aren't supported. However - if I look at the schema I can see that OpportunitySharing is available, and creatable, so theory goes I should be able to write to it.

 

I am trying to make some additions to sharing for a particular opportunity by inserting into this object - I can set all the fields I need to, but doing a database.insert gives me 'insufficient access on cross-reference object'. Now I'm not sure if this is a red herring for not being able to insert at all, or if I am missing something else. I am the owner of the record I'm trying to change - would this exclude me from being able to add an explicit sharing record for myself?

 

If anyone has tried Apex sharing on std objects or has some advice I'd be most grateful.

 

Thanks,

Stephen

London, UK

JimRaeJimRae

I have set up code that adds read sharing to an Opportunity record.

 

Here is a code snippet:

 

 

 

private static void setsharing(String EventOppID){ String UserProfile =[select name from profile where id=:UserInfo.getProfileId() LIMIT 1].Name; system.debug('\n\nGot Sharing User: '+UserInfo.getUserName()); OpportunityShare POVShare = new OpportunityShare(); POVShare.UserOrGroupId=UserInfo.getUserId(); POVShare.OpportunityAccessLevel='Read'; POVShare.OpportunityID=EventOppID;//coming from an event record try{ if(!(UserProfile.equals('System Administrator'))){ insert POVShare; sendAdminEmail(); } }catch(DMLException DMLe){ system.assert(true,'Caught DML Exception: '+DMle.getDMLMessage(0)); system.debug('\n\nCaught DML Exception: '+DMle.getDMLMessage(0)); } }

 

 

 

 

StephenBStephenB

Jim, fantastic - thanks for your help. I think the problem I was running into was in not checking for the System Admin profile, as everything else I had was pretty much the same.

 

Anyway it's working now with the code you supplied so thanks for the help!

 

Regards,

Stephen

surya123surya123

Hi Stephen,

 

   I have seen your post on apex sharing rules, i am also trying to control the sharing of standard object through apex.

This is the first time i am using the apex sharing. I am trying to write apex sharing on account object. I made the organization wide setting for account as private.

 

My trigger code is:

 

Trigger apexsharing on Account (after insert, after update) {

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

for(Account acc : trigger.new)

{

//if it is private only share with sharing manager

if(acc.Is_Private__c==true)

{

AccountShare accShare = new  AccountShare();
accShare.AccountId = acc.Id;
accShare.UserOrGroupId = acc.Sharing_Manager__c;
accShare.AccountAccessLevel = 'Read';

accShares.add(accShare);

}

}

if(! accShares.isEmpty())

   { 
 

    Database.SaveResult[] tripShareInsertResult = Database.insert(accShares,false);

 

   }

}

 

   If the Is private is checked, then the record should be shared with 'Sharing Manager' with read only access. here sharing manager is a look up to user in account object.

 

 But when admin creates the account with 'is private' checked and 'sharing manger' as another user whose profile is other than admin, i expect the record should be shared with the user with read only access. But it's not happening.

 

can you please suggest what is the problem here.

 

  

NaniNani

Hi Jim,

 

I'm also facing the same error - INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY.

The scenario is something like this:

Person X creates an Opportunity (on a Custom Object) and changes the Owner to Y. But, X would still like to view the Opportunity. We know that whenever the Owner of a record is changed then all the sharing rules are deleted. So, I'm trying to write an Apex code in 'After Update' trigger of Opportunity to create a record in Opportunity_Share object (again a custom object) with Parent Id = Opty Id and UserOrGroupId = X Id. But, I'm getting INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY error.

 

Because X is no more the owner of the record, I guess this error is shooting up.

 

How do you think, can I get this working?

 

Thanks,

Nani.