0

我试图弄清楚如何创建一个 Apex 触发器,该触发器根据机会类型触发不同的类。我在下面添加了我认为会起作用的内容,但它似乎并没有像我想象的那样拉动这些领域。有人可以告诉我如何从更新的记录中提取值吗?非常感谢任何帮助,因为我正试图尽快解决这个问题。

trigger OrderRollupSummaryTrigger on Order_Location_Package__c (after insert, after update, after delete, after undelete) {

if (Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type = 'NEW' || Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type = 'Renewal') {
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        OrderLocationRollupSummary.rollupOrderPackages(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        OrderLocationRollupSummary.rollupOrderPackages(trigger.old);
    }
}

else if (Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type != 'NEW' || Order_Location_Package__c.Order_Sheet__c.Opportunity__c.Type != 'Renewal') {
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        OrderLocationRollupSummaryOther.rollupOrderPackages(trigger.new);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        OrderLocationRollupSummaryOther.rollupOrderPackages(trigger.old);
    }
}
}

谢谢!

4

1 回答 1

0

经过大量阅读,我找到了最好的方法。特别是带有公式字段(以避免触发器中的 SOQL 查询)。

trigger OrderLocationRollupSummaryTrigger on Order_Location_Package__c (after insert, after update, after delete, after undelete) {
    
    List<Order_Location_Package__c> orderLocationPackageRenewalList = new list<Order_Location_Package__c>();
    List<Order_Location_Package__c> orderLocationPackageOtherList = new list<Order_Location_Package__c>();

    //Check and add the records to list based on condition
    if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)) {
        for(Order_Location_Package__c olp: trigger.new)
            if(olp.Opportunity_Type__c == 'NEW' || olp.Opportunity_Type__c == 'Renewal')
                orderLocationPackageRenewalList.add(olp);
            else
                orderLocationPackageOtherList.add(olp);
    }
    else if (trigger.isAfter && trigger.isDelete) {
        for(Order_Location_Package__c olp: trigger.old)
            if(olp.Opportunity_Type__c == 'NEW' || olp.Opportunity_Type__c == 'Renewal')
                orderLocationPackageRenewalList.add(olp);
            else
                orderLocationPackageOtherList.add(olp);     
    }


    //Send the records for processing to RollupSummary class
    if(orderLocationPackageRenewalList!=null && !orderLocationPackageRenewalList.isEmpty())
        OrderLocationRollupSummary.rollupOrderPackages(orderLocationPackageRenewalList);

    //Send the records not meeting the criteria to other RollupSummary class
    if(orderLocationPackageOtherList!=null && !orderLocationPackageOtherList.isEmpty())
        OrderLocationRollupSummaryOther.rollupOrderPackages(orderLocationPackageOtherList);

}
于 2020-09-18T06:27:14.457 回答