目录

Apex - 触发设计模式( Trigger Design Patterns)

设计模式用于使我们的代码更有效,并避免达到调控器限制。 通常,开发人员可以编写低效的代码,这些代码可能导致对象的重复实例化。 这可能导致代码效率低下,性能低下,并可能导致违反调控器限制。 这通常发生在触发器中,因为它们可以对一组记录进行操作。

我们将在本章中看到一些重要的设计模式策略。

批量触发器设计模式

在实际业务案例中,您可能需要一次处理数千条记录。 如果您的触发器不是为处理这种情况而设计的,那么在处理记录时它可能会失败。 在实现触发器时,您需要遵循一些最佳实践。 默认情况下,所有触发器都是批量触发器,并且可以一次处理多个记录。 您应该始终计划一次处理多个记录。

考虑一个业务案例,其中,您需要处理大量记录,并且您已经编写了如下所示的触发器。 这与我们在“客户状态”从“非活动”更改为“活动”时插入发票记录时所采用的示例相同。

// Bad Trigger Example
trigger Customer_After_Insert on APEX_Customer__c (after update) {
   for (APEX_Customer__c objCustomer: Trigger.new) {
      if (objCustomer.APEX_Customer_Status__c == 'Active' && 
         trigger.oldMap.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {
         // condition to check the old value and new value
         APEX_Invoice__c objInvoice = new APEX_Invoice__c();
         objInvoice.APEX_Status__c = 'Pending';
         insert objInvoice;   //DML to insert the Invoice List in SFDC
      }
   }
}

您现在可以看到已经为循环块编写了DML语句,该循环块仅在处理少量记录时起作用,但是当您处理数百条记录时,它将达到每个事务的DML语句限制,这是governor limit 。 我们将在后续章节中详细介绍Governer Limits。

为避免这种情况,我们必须使触发器有效地一次处理多个记录。

以下示例将帮助您理解相同的内容 -

// Modified Trigger Code-Bulk Trigger
trigger Customer_After_Insert on APEX_Customer__c (after update) {
   List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>();
   for (APEX_Customer__c objCustomer: Trigger.new) {
      if (objCustomer.APEX_Customer_Status__c == 'Active' &&
         trigger.oldMap.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {
         //condition to check the old value and new value
         APEX_Invoice__c objInvoice = new APEX_Invoice__c();
         objInvoice.APEX_Status__c = 'Pending';
         InvoiceList.add(objInvoice);//Adding records to List
      }
   }
   insert InvoiceList;
   // DML to insert the Invoice List in SFDC, this list contains the all records 
   // which need to be modified and will fire only one DML
}

此触发器仅触发1个DML语句,因为它将在List上运行,并且List具有需要修改的所有记录。

通过这种方式,您可以避免DML语句调控器限制。

触发助手类

在触发器中编写整个代码也不是一个好习惯。 因此,您应该调用Apex类并将处理从Trigger委托给Apex类,如下所示。 Trigger Helper类是执行触发器的所有处理的类。

让我们再次考虑我们的发票记录创建示例。

// Below is the Trigger without Helper class
trigger Customer_After_Insert on APEX_Customer__c (after update) {
   List<apex_invoice__c> InvoiceList = new List<apex_invoice__c>();
   for (APEX_Customer__c objCustomer: Trigger.new) {
      if (objCustomer.APEX_Customer_Status__c == 'Active' &&
         trigger.oldMap.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {
         // condition to check the old value and new value
         APEX_Invoice__c objInvoice = new APEX_Invoice__c();
         objInvoice.APEX_Status__c = 'Pending';
         InvoiceList.add(objInvoice);
      }
   }
   insert InvoiceList; // DML to insert the Invoice List in SFDC
}
// Below is the trigger with helper class
// Trigger with Helper Class
trigger Customer_After_Insert on APEX_Customer__c (after update) {
   CustomerTriggerHelper.createInvoiceRecords(Trigger.new, trigger.oldMap);
   // Trigger calls the helper class and does not have any code in Trigger
}

助手班

public class CustomerTriggerHelper {
   public static void createInvoiceRecords (List<apex_customer__c>
   customerList, Map<id, apex_customer__c> oldMapCustomer) {
      List<apex_invoice__c> InvoiceList = new Listvapex_invoice__c>();
      for (APEX_Customer__c objCustomer: customerList) {
         if (objCustomer.APEX_Customer_Status__c == 'Active' &&
            oldMapCustomer.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {
            // condition to check the old value and new value
            APEX_Invoice__c objInvoice = new APEX_Invoice__c();
            // objInvoice.APEX_Status__c = 'Pending';
            InvoiceList.add(objInvoice);
         }
      }
      insert InvoiceList;  // DML to insert the Invoice List in SFDC
   }
}

在这里,所有处理都被委托给了helper类,当我们需要一个新的功能时,我们可以简单地将代码添加到helper类而不修改触发器。

每个sObject上的单个触发器

始终在每个对象上创建一个触发器。 如果达到调控器限制,同一对象上的多个触发器可能会导致冲突和错误。

您可以根据需要使用上下文变量从helper类调用不同的方法。 考虑我们之前的例子。 假设我们的createInvoice方法只应在记录更新时和多个事件上调用。 然后我们可以控制执行如下 -

// Trigger with Context variable for controlling the calling flow
trigger Customer_After_Insert on APEX_Customer__c (after update, after insert) {
   if (trigger.isAfter && trigger.isUpdate) {
      // This condition will check for trigger events using isAfter and isUpdate
      // context variable
      CustomerTriggerHelper.createInvoiceRecords(Trigger.new);
      // Trigger calls the helper class and does not have any code in Trigger
      // and this will be called only when trigger ids after update
   }
}
// Helper Class
public class CustomerTriggerHelper {
   //Method To Create Invoice Records
   public static void createInvoiceRecords (List<apex_customer__c> customerList) {
      for (APEX_Customer__c objCustomer: customerList) {
         if (objCustomer.APEX_Customer_Status__c == 'Active' &&
            trigger.oldMap.get(objCustomer.id).APEX_Customer_Status__c == 'Inactive') {
            // condition to check the old value and new value
            APEX_Invoice__c objInvoice = new APEX_Invoice__c();
            objInvoice.APEX_Status__c = 'Pending';
            InvoiceList.add(objInvoice);
         }
      }
      insert InvoiceList; // DML to insert the Invoice List in SFDC
   }
}
↑回到顶部↑
WIKI教程 @2018