function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael Haase 9Michael Haase 9 

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:
 
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!
Michael DsozaMichael Dsoza
HI Michael, 

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.
trigger updatePressesTrigger on Press__c (after insert, before update) {

for(Press__c p: Trigger.NEW) {
     if(p.Survey_6_Mth_After_Handover__c == false and p.Production_Start_Date__c != null and p.RecordTypeId  == '01220000000YE4U' and (p.Production_start_status__c = 'F' OR p.Production_start_status__c = 'I' ))  {

       List <Press__c> pressesToUpdate = new List<Press__c>();
       date prodStartDate = p.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;
      }
   }
}
Hope it will help you.
Mark it as best answer if it resolves your query.

Thanks
 
ManojjenaManojjena
Hi Michel ,
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
Michael Haase 9Michael Haase 9
Manoj, Michael, thank you for your advice. Actualy my plan is to setup a scheduled Batch job. Therefore I didnt use Trigger.new. I just wanted to write the logic and then see how I build it into the scheduled job. Not sure if it is correct to write it into the trigger. Might be I need to write a class for the code to work properly.
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?