List&lt;OpportunityShare&gt; grantAccessUpsertLst = new List&lt;OpportunityShare&gt;(); 
for(String userId : userIds){
        List&lt;UserRecordAccess&gt; userRecords = [SELECT RecordId, HasEditAccess FROM UserRecordAccess WHERE UserId =: userId AND RecordId =: oppId];
        if(userRecords.size() &gt; 0 &amp;&amp; !(userRecords.get(0).HasEditAccess)){
            OpportunityShare opShare = new OpportunityShare();
            opShare.UserOrGroupId = userId; // Error Line
            opShare.OpportunityAccessLevel = ACCESS_LEVEL_EDIT;
            opShare.OpportunityID = oppId;
            grantAccessUpsertLst.add(opShare);          
        }
}
if(grantAccessUpsertLst.size() &gt; 0)
    insert grantAccessUpsertLst;//Use Insert instead of upsert


You can use insert to share opportunity share. Also if you want to update any share records you will need to first delete the existing share records for the user and insert the new one. Hope this helps.

Why i am getting "Field is not writable: OpportunityShare.UserOrGroupId" compile time error
I use Macs, Ubuntu, FreeBSD, OpenBSD, and Fedora for different purposes. I'm fairly busy and trying out a new distro takes a lot of time, but I hear lots of good things about Arch Linux from people I admire. I do mostly scientific computing (and some web development), and use Linux as both a desktop and a server. Does Arch offer anything I'm not already getting from one of my current installs? I'm particularly interested in the differences between Arch and Debian
