You need to sign in to do that
Don't have an account?
youji37
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
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++; } }
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
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.
Yes, you can share this to other users by inserting additional records into the CaseShare object. You can also use Apex Managed Sharing
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).