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