Print this page

Changing case ownership with assignment rules programmatically will remove any manual sharing rules added via apex triggers

Knowledge Article Number 000079752
Description
If you have the following trigger on Case which adds manual sharing rows: 
 
trigger ManuallyShareAccountsWithSysAdmin on Case (after insert) { 
List<CaseShare> caseSharesList = new List<CaseShare>(); 
 
for(Case c : Trigger.new) { 
CaseShare cs = new CaseShare(); 
cs.CaseId = c.Id; 
cs.UserOrGroupId = '00520000002ZdoB'; // hardcoding other user's id 
cs.CaseAccessLevel = 'Edit'; 
caseSharesList.add(cs); 
 
system.debug(caseSharesList); 
insert caseSharesList; 
 
and execute this block of code to insert a case using the default case assignment rule: 
 
Contact contact = [SELECT Id FROM Contact LIMIT 1]; 
Case c = new Case(subject = 'Test case', contactId = contact.Id); 
database.DMLOptions dmo = new database.DMLOptions(); 
dmo.AssignmentRuleHeader.UseDefaultRule= true; 
c.setOptions(dmo); 
insert c; 

The manual case sharing rows inserted by the trigger will get deleted. This is intentional and is due to the Case AssignmentRuleHeader using the default rule. Effectively, Case Ownership changes and any manual sharing rows are deleted at this time by design.
Resolution As a workaround you may insert the CaseShare records in a future method.




promote demote