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
dwwrightdwwright 

Problem With Apex Managed Sharing

I'm trying to give "apex managed sharing" a test run. I've created a custom object (in my sandbox) and I'm trying to run through how to handle creating share objects.

 

Right now, I'm getting a compile error that appears to be a syntactical error, but I can't figure it out.

 

In the documentation Here, the description of how to handle doing this is pretty clear: MyCustomObject__Share. In the example, referencing their Job__c object is done as "Job__Share".

 

My object is called RFQ. The api name is RFQ__c, so I would expect the share name to be RFQ__Share. I'm getting the error: "Invalid Type: RFQ__Share" when trying to compile my code. Here's a snippet:

 

 

/*
* Trigger to test Apex Managed Sharing
*/
trigger AssignSharing on RFQ__c (after insert) {
List<RFQ__Share> shares = new List<RFQ__Share>(); List<String> srNums = new List<String>(); Map<String, Id> userIds = new Map<String, Id>(); RFQ__Share shr = new RFQ__Share(); for(RFQ__c rfq : trigger.new) { //for each comma separated SR Num in the SR Num field, add it to //the list of relevant SR nums for(String num : rfq.Sales_Responsible_Numbers__c.split(',',0)) { srNums.add(num); } } //SR Nums now holds all the Sales Responsible numbers that apply to the //rfq's being inserted, so we can query them from the database; //next step is to iterate through all the users who will be assigned a
//sharing rule (query result)
//create a map of SR#s to Ids for quick reference later for(User u : [SELECT Id, Sales_Responsible_Number__c from User where Sales_Responsible_Number__c in :srNums]) { userIds.put(u.Sales_Responsible_Number__c, u.Id); } //userIds now maps each SR# to a user Id //for each new RFQ, assign the users that need to be given share priviledges
 for(RFQ__c rfq : trigger.new) { //for each sales responsible number for(String s :srNums) { //if this RFQ has that sales responsible number if(rfq.Sales_Responsible_Numbers__c.contains(s)) { //create a sharing rule that allows the corresponding user to be added shr.ParentId = rfq.Id; shr.AccessLevel = 'edit'; shr.UserOrGroupId = userIds.get(s); shr.RowCause = Schema.rfq__Share.RowCause.SalesResponsible__c; shr.add(shares); } }//end for loops over srNums for this rfq }//end for (trigger.new) insert shares; }//end trigger

 

(Don't make fun of my code... it's for testing, not even compiled once yet, and not made to be efficient, I just want to get it to compile)

 

 

Any insight?

Best Answer chosen by Admin (Salesforce Developers) 
dke01dke01

Have  you gone into your sandbox :

 

setup > Administration setup > Security Controls > Sharing Settings

 

You must first set the object to be private.  Only once you do this Salesforce will create a RFQ__Share.

 

You can use Apex Explorer http://wiki.developerforce.com/index.php/Apex_Explorer  to see what sObject exist or not

All Answers

dke01dke01

Have  you gone into your sandbox :

 

setup > Administration setup > Security Controls > Sharing Settings

 

You must first set the object to be private.  Only once you do this Salesforce will create a RFQ__Share.

 

You can use Apex Explorer http://wiki.developerforce.com/index.php/Apex_Explorer  to see what sObject exist or not

This was selected as the best answer
dwwrightdwwright

Thanks, this was indeed the problem. I set the object to private and now the code compiles. Thanks for your help!

ArunaAruna

Hi

 

can i  apply Apex managed sharing on Lead object.

 

if it can you pleas send me a sample code

 

Thank you