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
Rakesh Babu 3Rakesh Babu 3 

Field is not writeable: OpportunityShare.OpportunityId


I receiving "Field is not writeable: OpportunityShare.OpportunityId" error for the below code.Can someone please help me to sort out the error?

trigger shareRecord on Opportunity (after insert,after update) {
    List<OpportunityShare> share=new List<OpportunityShare>();
    User u=[select id from User where alias='kshar'];
    for(Opportunity op:Trigger.New){
            OpportunityShare p=new OpportunityShare();
    insert share;
Is the OWD setting for opportunity set as private?
For Apex Sharing : The object’s organization-wide default access level must not be set to the most permissive access level. For custom objects, this level is Public Read/Write.
Rohit Sharma 66Rohit Sharma 66

Use p.ParentId in place of p.opportunityId.
opportunityId is read only field and u cannnot modify it.
Here u are creating a opportunityShare record so for that mention the parentId which will be your opportunityId.

Just try and let me know if it works for you.
Have you tried using parentId field on OpportunityShare?
There is no parentId field available on OpportunityShare object.
Hi Vineetkumar,

can you clear me about owd settings?
i too having same error.,
public class Opportunity_Share_Handler 
    public static void create(Map<id,opportunity>oldMap, Map<id,opportunity>newMap)
        set<id> ownerids = new set<id>();
        //fetch all the opportunities from the newMap and put their ownerids in the set
        list<opportunity> optylist = newmap.values();
        for(opportunity op:optylist)
        //write a soql to get user with corresponding manager details of those opportunity owners
        list<user> users = [select id, Managerid from user where id in: ownerids];
        //create a map with userid as key and corresponding manager as value
        Map<id,id> usermap = new map<id, id>();
        for(user u: users)
            usermap.put(, u.Managerid);
        List<opportunityshare> share = new list<opportunityshare>();
        //get all the ids of opportunities which are modified
        set<id> optyids = oldMap.keySet();
        for(id key:optyids)
            //take the old value from the oldmap
            opportunity old=oldmap.get(key);
            //take the old value from the newmap
            opportunity newOpty=newmap.get(key);
            //check whether old stagename is not closed won and new stagename is closed won
            if(old.stagename!='closed won' && newOpty.stageName=='closed won')
                OpportunityShare op = new OpportunityShare();
                op.OpportunityId=key; //Id of the opportunity
                id manager=usermap.get(newOpty.ownerId); //get the manager of the owner
                op.UserOrGroupId=manager; // share with manager
        insert share;
vikrant Chauhan 5vikrant Chauhan 5
Hi Rakesh,
Your code seems good .I also got this error but it is removed by putting owd as private for opportunity object .Please try your code after changing that setting in security controls.And if this helps please mark my solution as best answer.
vikrant c