1

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);
    }
}
3

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.

0

i think in this case Salesforce prevent user from edit child record and if you update child record then parent record also get calculated as stated in docs.

Salesforce prevents users from saving a record if it invalidates a related record. For example, a master record has a validation rule that requires the roll-up summary field value to be greater than 100. If the user’s change to a related child record would put the value over 100, the user can’t save the record

So i think when you update child record SF also recalculate Roll summary field which cause this trigger to run. There is also very good chances of Workflow which may be cause of this behavior. Just check above condition and see if it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.