+ Start a Discussion
bikla78bikla78 

Date Method

I have a trigger that needs to get the number of days between the created date of the event and the actvity date and put the result into another field

 

I keep receieving this message:

 Error: Compile Error: Method does not exist or incorrect signature: [Datetime].daysBetween(Date) at line 9 column 33

 

 

 

trigger Event_Before_Insert on Event (before insert)
    {
        for( Event currentEvent : Trigger.new )
        {
        datetime CreatedDate =CurrentEvent.CreatedDate;
        date ActivityDate =CurrentEvent.ActivityDate;

        
         Integer numberofDays = CreatedDate.daysBetween(ActivityDate);
       
         CurrentEvent.Day_Count__c = numberofDays;


       }

}

Message Edited by bikla78 on 07-29-2009 12:04 PM
Best Answer chosen by Admin (Salesforce Developers) 
bikla78bikla78

This worked finally with a before update only. thanks again for all your help

 

trigger Event_Before_Insert on Event (before update)
{
for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);

CurrentEvent.Day_Count__c = numberofDays;


}

}

 

 

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Event_Before_Insert caused an unexpected exception, contact your administrator: Event_Before_Insert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Event_Before_Insert: line 8, column 49


Message Edited by bikla78 on 07-31-2009 11:19 AM
Message Edited by bikla78 on 07-31-2009 02:06 PM

All Answers

Jon Mountjoy_Jon Mountjoy_

I just looked up daysBetween in the documentation.  It's for the Date class.  You are invoking it for the DateTime class, which is a different class, which doesn't have that method.  See the Apex Code Developer's Guide for a list of methods available on each class.

 

 

bikla78bikla78

Okay. How would I be able to find the days between these 2 fields?

 

Also, even if there was a datetime method, would it crash since I am trying to find the value between 2 different datatypes?

 

I basically have 1 field that is a date and the other is a datetime. I think this is why the daysbetween for the date class failed.

 

Is there a way I can convert the datetime field to a date then possibly use the daysimbetween method?

 

If anybody has any suggestions how I could do this it would be much appreciated

 

 

Message Edited by bikla78 on 07-29-2009 01:06 PM
thangasan@yahoothangasan@yahoo

Hai

 

Try like below

 

datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);

 

Regards

Thanga

bikla78bikla78

Good news is the code compiled but I am receiving an apex exception now since the created date field only has date after it has been created but when i used an after update it gives me an error too. How can I get past this

 

 

 

 

trigger Event_Before_Insert on Event (before insert)
{
for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);

CurrentEvent.Day_Count__c = numberofDays;


}

}

 

 

 

 

 

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Event_Before_Insert caused an unexpected exception, contact your administrator: Event_Before_Insert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Event_Before_Insert: line 8, column 49


Message Edited by bikla78 on 07-30-2009 12:21 PM
thangasan@yahoothangasan@yahoo

Hai

 

  Change your code like below

 

  trigger Event_Before_Insert on Event (before insert,before update)

Thanga

bikla78bikla78

This worked finally with a before update only. thanks again for all your help

 

trigger Event_Before_Insert on Event (before update)
{
for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);

CurrentEvent.Day_Count__c = numberofDays;


}

}

 

 

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Event_Before_Insert caused an unexpected exception, contact your administrator: Event_Before_Insert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Event_Before_Insert: line 8, column 49


Message Edited by bikla78 on 07-31-2009 11:19 AM
Message Edited by bikla78 on 07-31-2009 02:06 PM
This was selected as the best answer