+ Start a Discussion
grandak koygrandak koy 

Subtracting date/time from date.today()

If I did:
Date fiveDays =date.today().Add days(-5)
Order_received_date__c ==five days
Would that bring back a date result or a date/time results since order_received_date__c is a date/time field?
If i wanted to convert the order received date to bring back a date results should i:
Date order = order_received_date__c.date();
Then do this:
Order==five days
To see if my order_received_date__c was five days ago?
I have a trigger to send an email if an order was five days ago but its not shooting out when the order received date was five days ago from today. Does the date time have to be converted to date to work?
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
If you're testing to see if the order was received exactly 5 days ago:

Date fiveDaysAgo = Date.today().addDays( -5 );
Date orderReceivedDate = myOrder.Order_Received_Date__c.date();
if ( orderReceivedDate == fiveDaysAgo )
    //  send an email...

If you're willing to spend a query, you could also do something like this:

List<Order> fiveDayOldOrders =
(   [   SELECT  Id
        FROM    Order
        WHERE   (   Id IN :myOrderIds
                AND Order_Received_Date__c = LAST_N_DAYS:5    //  in the last 5 days
                AND Order_Received_Date__c < LAST_N_DAYS:4    //  older than the last 4 days
grandak koygrandak koy
Thank you glyn,How would i convert a date/time field into a date field in a test class?

I was able to do it in my trigger but when I went to test it, it does not over. if i keep it as a date/time it covers.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I'm not sure I understand your question.  Can you post the code?  Just enough so that I can answer the question - thanks!
grandak koygrandak koy
Snippet of Code:

private static Date fiveDays = System.today().addDays(-5);

List<Reserve__c> orders = new List<Reserve__c>();
        for(reserve__c reserve : reserves){
            Date orderDate = reserve.Order_received__c.date();
            if( orderDate==fiveDays ){
               orders.add(new Reserve__c(Account__c =order.Account__c,Status__c = ’Received’));

My code is to trigger when the order_received date is five days ago.

In my test class I have it set:
            res.Order_received__c = System.Today().addDays(-5);
            update res;
            sendEmailForOrder ord = new SendEmailForOrder();
            List<Reserve__c> reserve = [Select id, Account__c, status__cFROM reserve__c where Account__r.id =: account.id];
            System.assertEquals(1, reserve.size());

It covers everything but this line:
orders.add(new Reserve__c(Account__c =order.Account__c,Status__c = ’Received’));

When I change this line in my class:
if( orderDate==fiveDays ){


If(reserve.Order_received__c ==fiveDays){

Then it covers that line:
orders.add(new Reserve__c(Account__c =order.Account__c,Status__c = ’Received’));

So I was wondering how in my test class, I can convert Order_received__c to a date field. In the test class its still a date/time field
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Some ideas:

1) Since "if(reserve.Order_received__c ==fiveDays){" works, you could change the class and leave it that way.
2) change the test code from
    res.Order_received__c = System.Today().addDays(-5);
    res.Order_received__c = System.now().addDays(-5);

I can't think of anything else to try right now, but if I do, I'll post it here.