function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Evan C. DeckerEvan C. Decker 

Trigger replication help

Hi everyone, I have a question about two apex triggers in our org. The first works as expected. Here's the code:

trigger LinkToInstallation on Plan_Design_Detail__c (before insert, before update) {
  
    set<ID> salesSummaryIds = new set<ID>();
    Map<Id, Id> SCInsMap = new Map<Id, Id>();
  
    for(Plan_Design_Detail__c pdd: trigger.new){
        if(pdd.Sales_Summary__c!= null){
            salesSummaryIds.add(pdd.Sales_Summary__c);
        } 
    }
    if(!salesSummaryIds.isEmpty()){
        // prepare Inst - SC Map
        for(Installation__c ins: [select Id, Sales_Summary__c
                                          from Installation__c
                                          where Sales_Summary__c  IN :salesSummaryIds]){
            SCInsMap.put(ins.Sales_Summary__c, ins.Id);
        }
        for(Plan_Design_Detail__c pdd: trigger.new){
            pdd.Installation__c = SCInsMap.get(pdd.Sales_Summary__c);
        } 
    }
                                                      
}

This trigger involves three objects - Sale Confirmations (api name is Sales_Summary__c), Plan Design Details, and Installation. A Sale Confirmation is always created first, then a Plan Design Detail record, then an Installation record. Plan Design Details and Installations are both related to Sale Confirmations. The action this trigger does is link the Installation to the Plan Design Detail record based on the related Sale Confirmation. Once the Installation record is created the Plan Design Detail can be refreshed and the Installation record will be linked.

The second does not work as expected. I recreated the above, except it's for a new object called CBA. I need it to function in the same way. Here's the code:

trigger LinkToInstallation2 on CBA__c (before insert, before update) {
  
    set<ID> salesSummaryIds = new set<ID>();
    Map<Id, Id> SCInsMap = new Map<Id, Id>();
  
    for(CBA__c cba: trigger.new){
        if(cba.Sale_Confirmation__c!= null){
            salesSummaryIds.add(cba.Sale_Confirmation__c);
        } 
    }
    if(!salesSummaryIds.isEmpty()){
        // prepare Inst - SC Map
        for(Installation__c ins: [select Id, Sales_Summary__c
                                          from Installation__c
                                          where Sales_Summary__c  IN :salesSummaryIds]){
            SCInsMap.put(ins.Sales_Summary__c, ins.Id);
        }
        for(CBA__c cba: trigger.new){
            cba.Installation__c = SCInsMap.get(cba.Sale_Confirmation__c);
        } 
    }
                                                      
}

When I refresh the CBA record after creating the Installation the link is not there. However, if I edit and save the CBA record the link is made. I thought I could replace all the appropriate fields in the trigger and it would function the same but I'm finding that isn't the case. Does anyone have any ideas? Thank you very much!
logontokartiklogontokartik
Hi evancdecker,

From what you explain, I think the trigger is working as expected. The trigger you wrote is on CBA__c object and its fired only when the record is inserted or updated. So when you create the Installation record or refresh the CBA record, its not fired.

Yes, your edit & save is firing your trigger updating the installation link and thus you can see it. 

Not sure, what you are looking for but I can definitely say that what you wrote is working as expected. 


Hope this helps. Let me know if you have any addln questions/clarifications.

Thank you
Kartik
 
Evan C. DeckerEvan C. Decker
Hi, thanks for your answer. I guess I'm just confused why the trigger on the Plan Design Detail object doesn't require me to edit and save the record, while the trigger on the CBA record does. Do you know why that would be?
logontokartiklogontokartik
I am not sure why it would do that, Refresh does not/should not trigger an update, unless there is something going on other than the refresh. 

Are you sure that there is no Trigger on the Installation Object that is updating the link? Ideally you would have the trigger on the Installation Object as thats what is getting updated on Plan Design Detail and CBA.