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
jungleeejungleee 

Opportunity Share is deleted when owner is changed

Hi ,

I am inserting records into the opportunityShare object programtically thru a trigger, while inserting the record I am setting the userOrGroupID field in the oppotunityShare to a public group.  When I change the owner of the opportunity, all these sharing records get deleted. While if I change any other field on the opportunity, the records in the opportunityShare object are intact.

 

Could anyone please let me know as to why this behaviour is happening?? And how can we avoid this?

 

Regards

Sam

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

This is standard behavior. Changing the owner of an opportunity automatically removes all shares from it. See https://help.salesforce.com/htviewhelpdoc?id=account_owner.htm&siteLang=en_US ... Basically, sharing reverts back to the default values for the organization, except for those shares that result from an opportunity or account team.

All Answers

sfdcfoxsfdcfox

This is standard behavior. Changing the owner of an opportunity automatically removes all shares from it. See https://help.salesforce.com/htviewhelpdoc?id=account_owner.htm&siteLang=en_US ... Basically, sharing reverts back to the default values for the organization, except for those shares that result from an opportunity or account team.

This was selected as the best answer
jungleeejungleee

Thanks for the reply. Is there anyway we can stop this from happeing apart from adding all the existing share records tru trigger when the owner of the opportunity is changed(which I am doing right now.) ??

 

Thanks

Sam

sfdcfoxsfdcfox

Nope. This is standard functionality. You can't prevent standard functionality, you have to work around it.

Peter GruhlPeter Gruhl
For @jungleee, and any others subsequently viewing this topic.  JungLeee is using Apex Sharing already.  Apex Sharing defaults Row_Cause (the reason the record is being shared) to "Manual".  You can set up to 10 other reasons per sObject.  When the Apex Sharing code uses a Row_Cause other than Manual, the programmatic sharing is *not* deleted when the record Ownership changes. 

See: https://help.salesforce.com/articleView?id=security_apex_sharing_reasons.htm&type=5 for more information. 

All JungLeee needs to do is to set up a specific Apex Sharing Reason for the insert, and the share will not be removed when the record ownership changes.
Andrew HoveyAndrew Hovey
@Peter Gruhl
Unfortunately that isn't an option (I wish it were!).
Apex managed sharing (including sharing reasons) are not available for standard objects like opportunity.