Here's the use case. When a lead is converted to an Opportunity, I want to automatically share the opportunity with the previous owner of the lead. This is needed because upon conversion, the opportunity ownership is granted to someone else but the original lead owner needs ongoing access to the opportunity.


  • A partner submits/creates a lead and is the lead owner
  • Lead is converted to opportunity and internal sales person gains ownership
  • Original partner needs to have view/edit access on this opportunity

I know you can use Apex Managed Sharing to share a custom object, but I'm hitting a wall trying to do this with an opportunity since the fields in OpportunityShare are not writable. The following won't even compile as a result:

List<OpportunityShare> oppShares = new List<OpportunityShare>();
for(Id i : oppIds)
   OpportunityShare os = new OpportunityShare(OpportunityId = i);
   os.OpportunityId = i; // *** ERROR - not writable ***
   os.OpportunityAccessLevel = 'Read/Write';
   os.UserOrGroupId = oppIdToLead.get(i).OwnerId;
   os.RowCause = 'Manual Sharing';

Database.SaveResult[] result = Database.insert(oppShares, false);

2 Answers 2


This works in my org (I modified a little to allow for missing variables). Once I commented out the row cause, I was able to compile it...if you do that, does it still fail? If so, what's the error?

Set<Id> oppIds = new Set<Id> ();
Id newOwnerId;
List<OpportunityShare> oppShares = new List<OpportunityShare>();
for(Id i : oppIds) {
   OpportunityShare os = new OpportunityShare();
   os.OpportunityId = i; 
   os.OpportunityAccessLevel = 'Read/Write';
   os.UserOrGroupId = newOwnerId;
  // os.RowCause = 'Manual Sharing';

Database.SaveResult[] result = Database.insert(oppShares, false);
  • What could I be missing? I tried this in two orgs and I get the following error just like before: "Save error: Field is not writeable: OpportunityShare.OpportunityId" Did you configure something first? Can you get yours to save in a standard dev org?
    – Adam
    Apr 17, 2013 at 23:38
  • 1
    One question - does the Opportunity object having sharing enabled .i.e it's not public? If not, there won't be any sharing rules available...I just tried this code in a new dev org I have and got that same error because Opportunity is Public Read/Write Apr 17, 2013 at 23:53
  • 1
    That was it. After setting the org-wide default on opportunity to Private, then sharing was permitted. Thanks!
    – Adam
    Apr 18, 2013 at 0:20
  • I am getting similar kind of error - Why i am getting “Field is not writable: OpportunityShare.UserOrGroupId”. can u give some pointers why i am getting this -salesforce.stackexchange.com/questions/15227/… Aug 10, 2013 at 16:39

You may want to take a look at the RowCause and OpportunityAccessLevel values. For Manual sharing the RowCause would be 'Manual' or leave it blank and it should default. For the OpportunityAccessLevel, to accomplish Read/Write try using 'Edit' as the value.

Also realize that this isn't really Apex Managed Sharing in the sense that its not using a custom sharing reason. Unfortunately, only custom objects can have custom sharing reasons defined so you certainly can add sharing records via Apex as you're trying to do for Opportunities but be careful because if the Owner of that record changes the manual shares will be removed as it will treat your Apex-added shares as if the owner manually added via the Share button.


  • Apparently, some people can modify the OpportunityId when first creating an OpportunityShare instance in Apex. I can't, and I suspect that when I can do this I'll be able to share an opp with someone. Are you able to initially set OpportunityShare.OpportunityId on a new instance in Apex? If so, do you know if there is a required config that I'm missing or something? (using a standard dev org as an example). Thanks!
    – Adam
    Apr 17, 2013 at 23:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .