AssignmentRuleHeader.UseDefaultRule = true), the system changes the case owner as part of the assignment process — and any manual CaseShare records added in the same transaction are deleted at that time.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;
}
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 assignment rule changes the case owner — which triggers automatic removal of the manual sharing rows added by the trigger, because the owner change and the trigger run in the same transaction.
To prevent manual sharing rows from being deleted when the case owner changes, insert the CaseShare records in a future method (@future) instead of synchronously in the trigger.
A future method executes asynchronously — after the current transaction completes. This means the CaseShare records are inserted after the case assignment rule has run and the ownership change has occurred. Because the sharing rows are added in a separate, later transaction, they are not cleared by the ownership change.
000385703

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.