2016-08-12 66 views
0

我需要將以下觸發器邏輯轉移到apex類。但在頂尖班,我不能使用Trigger.New,因爲它會給出錯誤。一旦我給出apex調用的邏輯,我可以在觸發器中調用該方法,那麼是否有人可以告訴我如何將該觸發器轉換爲頂點類?如何將觸發器轉換爲Apex類

trigger updatecontactrolecount on Opportunity(before insert, before update) { 
    Boolean isPrimary; 
    Integer iCount; 
    Map < String, Opportunity > oppty_con = new Map < String, Opportunity >(); //check if the contact role is needed and add it to the oppty_con map 
    for (Integer i = 0; i < Trigger.new.size(); i++) { 
     oppty_con.put(Trigger.new[i].id, 
      Trigger.new[i]); 
    } 
    isPrimary = False; 
    for (List <OpportunityContactRole> oppcntctrle: [select OpportunityId from OpportunityContactRole where(OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in : oppty_con.keySet())]) { 
     if (oppcntctrle.Size() > 0) { 
      isPrimary = True; 
     } 
    } 
    iCount = 0; 
    for (List <OpportunityContactRole> oppcntctrle2: [select OpportunityId from OpportunityContactRole where(OpportunityContactRole.OpportunityId in : oppty_con.keySet())]) //Query for Contact Roles 
    { 
     if (oppcntctrle2.Size() > 0) { 
      iCount = oppcntctrle2.Size(); 
     } 
    } 
    for (Opportunity Oppty: system.trigger.new) //Check if roles exist in the map or contact role isn't required 
    { 
     Oppty.Number_of_Contacts_Roles_Assigned__c = iCount; 
     Oppty.Primary_Contact_Assigned__c = isPrimary; 
    } 
} 
+0

這很簡單,只需將地圖傳入您的課程 – EricSSH

+0

您可以在課程中創建一個靜態方法,並將'Trigger.new'地圖傳遞給該課程。你可以用'Trigger.isInsert'和'Trigger.isBefore'上下文變量做同樣的事情,但你也可以在觸發器中檢查觸發的DML類型,然後從處理器類中調用適當的靜態方法。 還有一個小費。您可以使用Trigger.newMap上下文變量,而不是遍歷整個觸發器上下文並創建自己的映射。 Google針對APEX中的「觸發器上下文變量」獲取更多信息。 – pawelhajduk

+0

爲單個SObject類型創建只有一個觸發器也是一個好習慣,並且在此觸發器中僅從處理程序類中調用適當的方法。這樣您可以更好地控制觸發器執行順序,並且可以檢查其他條件。 – pawelhajduk

回答

1

可以使用TriggerHandler類來管理在同一個地方的所有triggerred事件: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

在你觸發打電話給你的獨特類:

trigger OpportunityTrigger on Opportunity (before insert,after insert,before update,after update, before delete,after delete) { 
    try { 
     new OpportunityTriggerHandler().run(); 
    } 
    catch(Exception e) { 
     System.debug(e); 
    } 

然後做一切TriggerHandler中的邏輯:

public with sharing class OpportunityTriggerHandler extends TriggerHandler { 
    private Map<Id, Opportunity> newOpportunityMap; 
    private Map<Id, Opportunity> oldOpportunityMap; 
    private List<Opportunity> newOpportunity; 
    private List<Opportunity> oldOpportunity; 

    public OpportunityTriggerHandler() { 
     this.newOpportunityMap = (Map<Id, Opportunity>) Trigger.newMap; 
     this.oldOpportunityMap = (Map<Id, Opportunity>) Trigger.oldMap; 
     this.newOpportunity = (List<Opportunity>) Trigger.new; 
     this.oldOpportunity = (List<Opportunity>) Trigger.old; 
    } 

    public override void beforeInsert() { 
     for (Opportunity o : this.newOpportunity) { 
      if (o.Name == '...') { 
       //do your stuff 
      } 
     } 
    } 

    public override void afterInsert() { 
     //... 
    } 

}