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
Tyler HarrisTyler Harris 

Case Apex Sharing Error INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY

I've got a trigger that's throwing the following error:

Error:

shareBackCase: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Trigger.shareBackCase: line 19, column 1


What I'm trying to do is when a case is assigned to a Queue I want the user to be able to access the case with "Read" access. I can't seem to get the Sharing Rule to insert. What do I need to do fix?
 
trigger shareBackCase on Case (after insert) {
	ID recordTypeId = [SELECT Id, Name FROM RecordType WHERE name ='Commissions' LIMIT 1].id;
    System.debug(recordTypeId);
    List <CaseShare> newCazeList = new List<CaseShare>();

    for(Case c:Trigger.new){
       CaseShare newCaze = new CaseShare();
        System.debug(c.RecordTypeId);
        if(c.RecordTypeId == recordTypeId){
            newCaze.CaseId = c.Id;
            newCaze.UserOrGroupId = c.CreatedById;
        	newCaze.CaseAccessLevel = 'Read';
            newCaze.RowCause = 'Manual';
            newCazeList.add(newCaze);
        }

    }
    
    insert newCazeList;
}

My org is a private sharing model.
srlawr uksrlawr uk
I think the problem here might be that you cannot insert a sharing rule that restricts the permissions of the user who owns a record. they always have to have full access to their records. You won't be able to use sharing rules to remove "edit" rights to a case whilst that users owns it, sorry!