+ Start a Discussion
MayankAdmMayankAdm 

Apex sharing rule in opportunity share

Hi All,

I have a requirement I need to change opportunity owner and assign Read acces to old user in opportunityshare object

what I did i am changing a owner with the help of workflow where owner is changed after changing stage .

for providing read acces i have created trigger and that trigger giveing a issues

Please check it -

 

trigger UpdateOpportunityOwner on Opportunity (after update) {
list<OpportunityShare> updateOpportunityShare = new list<OpportunityShare>();
list<OpportunityShare> lstOpportunityShare = [Select UserOrGroupId, OpportunityId, OpportunityAccessLevel,Id From OpportunityShare where OpportunityId =: Trigger.new];
system.debug('lstOpportunityShare'+lstOpportunityShare);

for(Opportunity o:Trigger.new)
{
if(o.OwnerId != trigger.oldmap.get(o.id).OwnerId)
{
for(OpportunityShare os:lstOpportunityShare)
{

os.OpportunityAccessLevel = 'Read';
//os.RowCause = 'Manual';
updateOpportunityShare.add(os);

}
}
}

if(updateOpportunityShare.Size()>0)
{
update updateOpportunityShare;
}

}

 

Error-

"Apex trigger UpdateOpportunityOwner caused an unexpected exception, contact your administrator: UpdateOpportunityOwner: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00tO0000006dLwcIAE; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: RowCause (cannot update sharing row with this cause): [RowCause]: Trigger.UpdateOpportunityOwner: line 27, column 1".

 

when I am using RowCause than it giving field is not wrietabale 

Please help me out ...

Thanks in Advance 

tonante27tonante27

Just one quick question: When geting a list of shares, if trigger.new represents a batch of new opportunities, shouldn't you use 'IN' clause liike this?

list<OpportunityShare> lstOpportunityShare = [Select UserOrGroupId, OpportunityId, OpportunityAccessLevel,Id From OpportunityShare where OpportunityId IN: Trigger.new];