3

I am getting compile time error while trying to assign userId to opShare.UserOrGroupId field. I am not sure what is wrong in it -

    List<OpportunityShare> grantAccessUpsertLst = new List<OpportunityShare>(); 
    for(String userId : userIds){
            List<UserRecordAccess> userRecords = [SELECT RecordId, HasEditAccess FROM UserRecordAccess WHERE UserId =: userId AND RecordId =: oppId];
            if(userRecords.size() > 0 && !(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() > 0)
        upsert grantAccessUpsertLst;

Thanks in advance

2 Answers 2

4
List<OpportunityShare> grantAccessUpsertLst = new List<OpportunityShare>(); 
for(String userId : userIds){
        List<UserRecordAccess> userRecords = [SELECT RecordId, HasEditAccess FROM UserRecordAccess WHERE UserId =: userId AND RecordId =: oppId];
        if(userRecords.size() > 0 && !(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() > 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.

3
  • does OpportunityShare not support "Upsert"? Commented Aug 11, 2013 at 15:54
  • No .Its not updatable and hence no upsert allowed.It will support insert though Commented Aug 11, 2013 at 16:05
  • It supports upsert as well. I didn't get any exception while using upsert keyword. Commented Sep 17, 2013 at 8:51
1

As of the salesforce doc for the Opportunity Share object:

UserOrGroupId: ID of the User or Group that has been given access to the opportunity.
This field can’t be updated.

enter image description here

2
  • If this is the case then how can i share an opportunity with user using Apex code? Commented Aug 10, 2013 at 2:57
  • @PramodKumar I have answered please check Commented Aug 11, 2013 at 5:50

You must log in to answer this question.

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