You need to sign in to do that
Don't have an account?
sales4ce
Help needed in bulkifying my trigger
Hi,
I have a use case where i have to check if the record has a date overlapping criteria. I have a trigger, but it kinda works only for one record and is not bulkified.
I am hitting a road block to bulkify the trigger. Any ideas?
trigger datesCannotOverlap on Tip__c (before Insert, before Update) {
Tip__c newTips=New Tip__c();
if(trigger.isInsert || trigger.isUpdate){
newTips=Trigger.New[0];
}
List<Tip__c> overlaps=[select Tip_Start_Date__c, Tip_End_Date__c, Business_Unit__c
from Tip__c
where
(
(Tip_Start_Date__c >= :newTips.Tip_Start_Date__c AND Tip_Start_Date__c <= :newTips.Tip_End_Date__c) OR
(Tip_end_Date__c >= :newTips.Tip_Start_Date__c AND Tip_End_Date__c <= :newTips.Tip_End_Date__c) OR
(Tip_Start_Date__c <= :newTips.Tip_Start_Date__c AND Tip_end_Date__c >= :newTips.Tip_end_Date__c)
)
and Business_Unit__c=:newTips.Business_Unit__c];
System.Debug('overlaps >>>>>'+overlaps);
if(overlaps.Size() > 0){
newTips.addError('Dates Overlapping with other Tips. please choose New dates');
}
}
Thanks,
Sales4ce
Oh I missed it, Thanks For update
Even it will give exceptiion as trigger.newmap will be null so retrieing keySet will give attemp to dereference error. Thats why I always write test methods to find out these issues :)
I have updated trigger
All Answers
Please change it like this
This will work for bulk data as well
Thanks for your reply. But i get an error when i save your version of trigger.
Error: Compile Error: IN operator must be used with an iterable expression at line 43 column 74
The trigger.newMap is where is bumping.Can you help me figure out what the problem is.
Thanks,
Sales4ce
Sorry change it like this
used
trigger.newMap.keySet()
Just replace
with
Thanks for your help folks.
i can now save my trigger successfully. But i get a run time exception.
System.NullPointerException: Attempt to de-reference a null object: Trigger.datesCannotOverlap: line 43, column 81
My Purpose of the trigger is:
I have a custom object , which includes three custom fields (Start date, End Date and Business Unit(Type: Picklist).
The three custom fields should be unique for a record, so the combination of these fields makes my record unique.
Example: start date: 06/20/2011, End Date: 06/27/2011, Business Type: Devices
Start date: 06/20/2011, End date:06/27/2011, Business Type: Drugs
Also, we must make sure that for new record the start and end dates do not fall within the above mentioned dates.
So the next acceptable record is:
Start date: 06/28/2011, end date: 06/30/2011,business type:Devices.
I need to throw an error for this condition:
start date: 06/25/2011, end date: 06/30/2011, Business Type: Devices
My trigger works fine for single record. I am unable to bulkify and need assistance here.
Let me know if this was helpful.
Thanks,
Sales4ce
Your welcome,
For bulkifying your trigger please see :
http://forceschool.blogspot.com/2011/05/writing-apex-trigger-save-limits-in.html
Thanks for pointing to the best practises. The blog is informative.
Can you help me figure out bulkifying for this trigger. I appreciate your help on this.
Thanks,
Sales4ce
I think in bulk case the problem with your code is that it is validating agaist all overlapped records even if they are related to some other. Here is your code
In above code some script stmts can be saved using collections.
Something is wrong in the Query logic:
This query will result output of, Business Unit in Trigger.new and Id not in Trigger.new.
Isn't it contradictory?
Oh I missed it, Thanks For update
Even it will give exceptiion as trigger.newmap will be null so retrieing keySet will give attemp to dereference error. Thats why I always write test methods to find out these issues :)
I have updated trigger
bro, u missed to check overlapstemp != empty before for loop.
Good to know you are scaning my post but this time you missed it mate :)
in for loop if list is empty it will not iterate no issues in it, we don't need to check any thing.
Thanks Sashikanth and Rahul, it worked like charm.
Appreciate your time and help.
Thanks,
Sales4ce
Your welcome :)