You need to sign in to do that
Don't have an account?

Field is not writeable: OpportunityShare.OpportunityId
Hi,
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){
if(op.amount>50000){
OpportunityShare p=new OpportunityShare();
p.OpportunityId=op.id;
p.UserOrGroupId=u.id;
p.OpportunityAccessLevel='Read';
p.RowCause='Manual';
share.add(p);
}
}
insert share;
}
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){
if(op.amount>50000){
OpportunityShare p=new OpportunityShare();
p.OpportunityId=op.id;
p.UserOrGroupId=u.id;
p.OpportunityAccessLevel='Read';
p.RowCause='Manual';
share.add(p);
}
}
insert share;
}
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.
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.
There is no parentId field available on OpportunityShare object.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunityshare.htm
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)
{
ownerids.add(op.ownerid);
}
//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.id, 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
op.OpportunityAccessLevel='Read';
op.Rowcause='Manual';
share.add(op);
}
}
insert share;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks,
Sriram.R
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.
Thanks,
vikrant c