However, Telesales wants to keep the Opportunity creator within the loop. When the WF reassigns the owner, the Opportunity creator is kicked out of the Opp Team, and its sharing rule for the Opp is removed.
I created a trigger to attempt Apex sharing on the Oppty record. I'm using Upsert because I didn't see the insert() method in the support doc. (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunityshare.htm)
But I'm hitting a wall:
"EXCEPTION_THROWN|[24]|System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
line 24, column 1"
The log reads:
11:01:01.85 (85553148)|EXECUTION_STARTED 11:01:01.85 (85565790)|CODE_UNIT_STARTED|[EXTERNAL]|01qQ0000000####|OppApxSharing on Opportunity trigger event AfterInsert for [006Q000000#####] 11:01:01.85 (85846722)|USER_DEBUG|[4]|INFO|Captured User 0053700000####### 11:01:01.85 (86340804)|DML_BEGIN|[24]|Op:Upsert|Type:OpportunityShare|Rows:1 11:01:01.85 (106965141)|DML_END|[24] 11:01:01.85 (107108567)|EXCEPTION_THROWN|[24]|System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] 11:01:01.85 (107665366)|FATAL_ERROR|System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Trigger.OppApxSharing: line 24, column 1 11:01:01.85 (107678827)|FATAL_ERROR|System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
trigger OppApxSharing on Opportunity (after insert) {
// get the id for the user creating the Oppty to perma-share them in
ID userId = UserInfo.getUserId();
system.debug(LoggingLevel.Info, 'Captured User '+ userId);
// insert record section
if (Trigger.isInsert) {
List<OpportunityShare> oppShare= new List<OpportunityShare>();
for (Opportunity o : Trigger.new) {
// create the new share for user
OpportunityShare os = new OpportunityShare();
os.OpportunityAccessLevel = 'Edit';
os.OpportunityId = o.Id;
os.UserOrGroupId= userId;
//os.UserOrGroupId = o.OwnerId;
os.RowCause = 'MANUAL';
oppShare.add(os);
}
if (!oppShare.isEmpty())
upsert oppShare;
}
}
Is it possible at all to Manually insert/upsert opportunity share records ???
Well, for anyone interested, I solved the thing with a Builder Flow on Opportunity Team. After testing for a few conditions on each record insertion, I added a ownerid change according to my territory lookup, and then another action that adds the record creator to the Opportunity Team with Read/Write...
BTW, SFDC is a little buggy here, as it was not setting R/W on the Team Sharing if the sharing record was added while creator = owner..