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
youji37youji37 

insert record into CaseShare table returns error.

I try to insert a record to CaseShare table with following trigger, but return error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger prmSharingTrigger caused an unexpected exception, contact your administrator: prmSharingTrigger: 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.prmSharingTrigger: line 14, column 13

 

Trigger:

trigger prmSharingTrigger on Case (after insert) {
    integer i=0;
    
    for (Case a : Trigger.new) {
        
        if(a.RecordTypeId == '012R00000008hE5'){
            
            CaseShare newCaseShare = new CaseShare(
                caseId=a.id,
                UserOrGroupId=a.LastModifiedById,
                CaseAccessLevel='Read'
            );

            insert newCaseShare;
        
        }else{
        //do nothing for other record type case.
        }
                
        i++;
    }
}
Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Your snippet is on the right track. But you need to specify the UserId or GroupId's for the "other" users you want to grant additional access to. Your code sample sets the CaseShare object's UserOrGroupId field to the Case's LastModifiedById. Instead, you want to get the UserId for the other users and set that field. You will have to create a single CaseShare record for each User you want to grant access to. So identify the Users you want to grant access, then loop through building the proper CaseShare objects in Apex, and lastly, do a single insert DML operation to insert all the CaseShare records in a single operation (more efficient than one at a time). 

 

 

All Answers

aalbertaalbert

If I read this code snippet right, are you trying to make the Case creator only have "Read" access to the Case?

I am not sure you can restrict the Case Owner (which is defaulted to the Case creator) to only have Read access.

 

I tried your code in my own org and it failed as you suggest. I then created a Case, changed the Owner, re-ran the apex, and it worked fine. So it seems like its tied to the fact that you are trying to restrict the Case owner's access to Read-only which is not allowed. 

 

 

youji37youji37
if there's anyway to do this? Or is there a way to share this case to other users when it is inserted?
aalbertaalbert

Yes, you can share this to other users by inserting additional records into the CaseShare object. You can also use Apex Managed Sharing

 

 

youji37youji37
How to write the code, many thanks!
aalbertaalbert

Your snippet is on the right track. But you need to specify the UserId or GroupId's for the "other" users you want to grant additional access to. Your code sample sets the CaseShare object's UserOrGroupId field to the Case's LastModifiedById. Instead, you want to get the UserId for the other users and set that field. You will have to create a single CaseShare record for each User you want to grant access to. So identify the Users you want to grant access, then loop through building the proper CaseShare objects in Apex, and lastly, do a single insert DML operation to insert all the CaseShare records in a single operation (more efficient than one at a time). 

 

 

This was selected as the best answer