I am trying to create share records for
workorder record gets created by community user (
profile:Customer Community User(custom) and
user license:Customer Community Login).
Below code that gets executed when record is created. Owner of the record is the logged in community user.
recId will the record id and
territoryId will be the territory Id that it should be shared to. These values are getting populated correctly.
Also, the class is defined as
global without sharing class, so it should be running in system context.
workordershare newShareRecord = new workordershare(); newShareRecord .put('AccessLevel','Edit'); newShareRecord .put('ParentId',recId); newShareRecord .put('RowCause','Manual'); newShareRecord .put('UserOrGroupId',territoryId); insert newShareRecord ;
This throws out error:
System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross- reference id: 
I tried to run the same code with developer console and it still gave me the same error.I did replace
recId with workorder record Id and
territoryId with actual territory id.
However, when I
manually updated the owner of that workorder record to internal user and then ran the same share record code with dev console, it worked and created share records.
So, now in the apex class, I wrote a code to update workorder
ownerId to an internal user before it executes share record code.But it continues to give the same error. I can see that
workorder ownerId has been updated to
I am not able to understand why it is giving error with apex class but not with dev console since both are running in system context.
Also, when I run the code in dev console, why does it throw error when record owner is a portal user but not when record owner is an internal user.