如何从 Apex 触发器中的记录中提取字段

How to pull in fields from record in Apex Trigger

我正在尝试弄清楚如何创建一个 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);
    }
}
}

谢谢!

经过大量阅读,我找到了执行此操作的最佳方法。特别是带有公式字段(以避免触发器中的 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);

}