You need to sign in to do that
Don't have an account?
bikla78
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;
}
}
This worked finally with a before update only. thanks again for all your help
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
All Answers
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.
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
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
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
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
Hai
Change your code like below
trigger Event_Before_Insert on Event (before insert,before update)
Thanga
This worked finally with a before update only. thanks again for all your help
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