Loading
Slack製品の問い合わせ先はSalesforce Helpへ統合・変更されます続きを読む

Sharing rules added with Apex triggers are deleted after case owner is changed

公開日: May 25, 2026
説明
When using an Apex trigger on the Case object to add manual sharing rows (CaseShare records), those sharing rows are automatically deleted when the case owner changes via the default Case Assignment Rule. This is expected, documented Salesforce platform behavior.

Why this happens:
Salesforce's sharing model states that when record ownership changes, all manual sharing rows (RowCause = 'Manual') are automatically removed. When a Case is inserted using the default Case Assignment Rule (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.
This behavior is documented in the Salesforce Apex Developer Guide under "Creating User Managed Sharing Using Apex": "If the owner of the record changes, the sharing is automatically deleted." See this for more details.

Example, 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 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

 
読み込み中
Salesforce Help | Article