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
JayaJayaJayaJaya 

Regarding accessing CaseShare record in Apex

Hi,

I am trying to delete a CaseShare record using the below code but getting an exception:

List<CaseShare> c = [Select Id,  CaseId, CaseAccessLevel, UserOrGroupId, RowCause From CaseShare where   CaseAccessLevel='Edit'  and CaseId = '50017000001b88e'];
System.debug(c);
delete c;

System.DmlException: Delete failed. First exception on row 0 with id 01n17000003gg7wAAA; first error: INVALID_CROSS_REFERENCE_KEY, id does not exist: []


I can see the SOQL query returning a record but not able to delete it. What could be the reason for not allowing deletion/updation. I am System Administrator.

Thanks.
sandeep sankhlasandeep sankhla
Hi jayaJaya,

If you check the schema then you will come to know that you can not delete the CaseShare object..It is only querable..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
JayaJayaJayaJaya
Hi Sandeep,

From below, CaseShare can be deleteable:

User-added image

 
sandeep sankhlasandeep sankhla
Hi jayaJaya,

May be somesetting are not enabled in my org so I am not able to see this...Okay now I think I can help you out..
Can you paste your code here where you are getting the error ?

Thanks,
Sandeep
Varun PareekVarun Pareek
Are you trying to delete the share record with Owner as well? In that case, can you try this:
List<CaseShare> c = [Select Id,  CaseId, CaseAccessLevel, UserOrGroupId, RowCause From CaseShare where   CaseAccessLevel='Edit'  and CaseId = '50017000001b88e' AND RowCause != 'Owner'];
System.debug(c);
delete c;

I don't think so that you can remove the share record where RowCause is Owner.