• Michael Haase 9
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Salesforce Solutions Architect
  • KBA AG

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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!
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!