[SalesForce] How to prevent trigger firing on the detail object when master object is inserted/updated

Hi I have 2 custom objects and have a master-detail relationship between them.

Master : Certification Slot

Detail : Certification Slot Detail

I write a trigger on Master object for a Duplicate check based on a field value Week__c for that object. I have a ROLL up Summary field on Parent No_of_Booked_Slots__c
This trigger runs in before Insert and before update and it is working fine.
I'm facing a wierd issue which i have never come across.
When i try to insert or edit on the detail record, this trigger is running.

Note: I'm not updating any field on the Detail object based on this trigger.

TRIGGER HANDLER CLASS:

public with sharing class CI_CertificationSlotTriggerHandler{


    public static void CertificationSlotInsert(list<Certification_Slot__c> newSlotList){

        Map<Id, Certification_Slot__c> existingslotMap = new Map<Id, Certification_Slot__c>([Select Id,Booking_Date__c,Week_Interval__c,Week__c from Certification_Slot__c where (Week__c != null OR Week__c != '')]);

        for(Certification_Slot__c cNew : newSlotList){
            cNew.Week__c = String.valueOf(cNew.Booking_Date__c.toStartofWeek()).split(' ')[0] + ' TO ' + String.valueOf((cNew.Booking_Date__c.toStartofWeek()).addDays(6)).split(' ')[0];
            system.debug('WEEK VALUE***'+cNew.Week__c);
            if(!existingslotMap.isEmpty()){
                if(cNew.Week__c == existingslotMap.get(cNew.Id).Week__c || cNew.Booking_Date__c == existingslotMap.get(cNew.Id).Booking_Date__c){
                    cNew.addError('You cannot create a duplicate record for the same week');
                }
            }        
        }        

    }

    public static void CertificationSlotUpdate(list<Certification_Slot__c> updSlotList){
        Map<Id, Certification_Slot__c> existingslotMap = new Map<Id, Certification_Slot__c>([Select Id,Booking_Date__c,Week_Interval__c,Week__c from Certification_Slot__c where (Week__c != null OR Week__c != '')]);

        for(Certification_Slot__c cNew : updSlotList){
            cNew.Week__c = String.valueOf(cNew.Booking_Date__c.toStartofWeek()).split(' ')[0] + ' TO ' + String.valueOf((cNew.Booking_Date__c.toStartofWeek()).addDays(6)).split(' ')[0];
            system.debug('WEEK VALUE***'+cNew.Week__c);
            if(cNew.Week__c == existingslotMap.get(cNew.Id).Week__c || cNew.Booking_Date__c == existingslotMap.get(cNew.Id).Booking_Date__c){
                cNew.addError('You cannot create a duplicate record for the same week');
            }        
        }        

    }

}

TRIGGER:

trigger CI_CertificationSlotTrigger on Certification_Slot__c (before insert, before update) {

    if (trigger.isBefore && trigger.isInsert) {
        CI_CertificationSlotTriggerHandler.CertificationSlotInsert(trigger.new);
    }

    if (trigger.isBefore && trigger.isUpdate) {
        CI_CertificationSlotTriggerHandler.CertificationSlotUpdate(trigger.new);
    }
}

Best Answer

You'll want to review the Triggers and Order of Execution document. Here, they explain:

If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.

In other words, because you have a summary rollup field, the parent automatically goes through the entire save process automatically. This includes triggers, because the parent could fail to save because of SObject.addError or validation rules, and the parent also needs to maintain its consistent state, which may include updating other records as well.

Related Topic