Salesforce 如何从内部方法更新父级 table 上的汇总摘要字段,该方法在子级持久化时触发

Salesforce How to update a roll-up summary field on parent table from inside method that gets triggered when child is persisted

I created a Commission object with field called commission_amount__c, This object has a master detail relationship with Contact. Contact Has a sum up field called Total_Commission__c which sums up all commission amounts from Commissions related to each Contact. I've put a trigger on Commission which calls the method updatePrimary. I want to get each persons total commission but when I query this inside updatePrimary I get the old value in the sumup field(total_commission) and not the new value with the new commission amount added. it hasn't summed up yet. Some help would be very much appreciated if possible.

trigger UpdatePrimaryTrigger on Commission__c (after insert, after update) {
    
    
        for(Commission__c c : Trigger.New) {
            ContactHandler.updatePrimary(c);
        }
    }
public class ContactHandler {
    
    public static void updatePrimary(Commission__c comIn){ 
        
        Contact receiverOfCommission = [Select Name, Total_commission__c
                                       FROM Contact
                                       Where Id = :comIn.Contact__c];
    }

您在 save order of execution 中的位置错误。

在第 18 步更新父对象上的汇总摘要字段。after 在第 8 步执行触发器。

如果您想在汇总摘要字段更新时采取行动,您需要在父对象上编写触发器。但是,如果我正确理解了您的要求,您可以简单地在引用联系人汇总字段的佣金对象上放置一个公式字段。