You need to sign in to do that
Don't have an account?

date.daysBetween method works only "after update" in Trigger?
Hallo all together,
I am new to APEX. I am just writing a trigger that uses the date (DaysBetween) method. The trigger shall run after insert of a new record or update of an existing record. In my understanding I should use here "after insert, before update". However, the part when the record is updated (before update) does not work correctly when the date field is updated. The date.daysbetween method does not count according to the new date value but still calculates with the old date value. When I change the to "after update" the trigger works fine. But is this correctlike this? I believe for record updates I shold use "before update" as I have already the record Id.
Here is my code:
And here is the Test Class:
Thanks for your advice!
I am new to APEX. I am just writing a trigger that uses the date (DaysBetween) method. The trigger shall run after insert of a new record or update of an existing record. In my understanding I should use here "after insert, before update". However, the part when the record is updated (before update) does not work correctly when the date field is updated. The date.daysbetween method does not count according to the new date value but still calculates with the old date value. When I change the to "after update" the trigger works fine. But is this correctlike this? I believe for record updates I shold use "before update" as I have already the record Id.
Here is my code:
trigger updatePressesTrigger on Press__c (after insert, before update) { List <Press__c> presses = [Select Id, Production_Start_Date__c,Survey_6_Mth_After_Handover__c From Press__c Where Survey_6_Mth_After_Handover__c = False AND Production_Start_Date__c !=Null AND RecordTypeId ='01220000000YE4U' AND (Production_start_status__c = 'F' OR Production_start_status__c = 'I')]; If(presses.size() >0){ List <Press__c> pressesToUpdate = new List<Press__c>(); //Determine Presses where Production Start Date > 6 month For(Press__c press : presses){ date prodStartDate = press.production_start_date__c; date todayDate = date.today(); integer daysDifference = prodStartDate.daysBetween(todayDate); if(daysDifference > 180){ press.Survey_6_Mth_After_Handover__c = True; pressesToUpdate.add(press); } update pressesToUpdate; } } }
And here is the Test Class:
@isTest public class testUpdatePressesTrigger { static testMethod void testUpdatePressTrigger(){ //Create Account Account a = new Account(); a.name = 'Prospecta'; a.recordtypeid = '01220000000YE4P'; a.Market_segments__c = 'Industrial Printer'; insert a; //Create Press Press__c p = new Press__c(); p.Name = 'RA105'; p.RecordTypeId = '01220000000YE4U'; p.Year__c = '2015'; p.Survey_6_Mth_After_Handover__c = False; p.Production_start_date__c = Date.newInstance(2016,5,1); p.Account__c = a.Id; p.Production_start_status__c = 'F'; insert p; //Update press p.Production_start_date__c = p.Production_start_date__c - 200; update p; } }
Thanks for your advice!
If you want latest value of prodStartDate then use Trigger.NEW context variable which will give you list<Press__c>. Iterate over this list<Press> & then try to access prodStartDate. Now you will get expected output in before update.
Also, you dont need to query the same object for checking condition. Hope it will help you.
Mark it as best answer if it resolves your query.
Thanks
I saw some problem in your code .
Like you have hard coded record Type Id .
Query is not required I think you have trigger.new List of records .
You have DML inside for loop which should not .
If possible explain your requirment so that we will help you to write code .
Thanks
Manoj
The question about the "before update" in the trigger was more for my understanding.
Do you have a recommendation how to get the whole code working as a scheduled apex job?