1

I'm creating a record in a test class that needs a specific LastModifiedDate, that date would be, today's date minus 365 days. When I create the record, this is what the code looks like:

String OppJSON = '{"attributes":{"type":"Opportunity","url":"/services/data/v25.0/sobjects/Opportunity/500E0000002nH2fIAE"},"Id":"500E0000002nH2fIAE","LastModifiedDate":"2015-01-06T17:54:26.000+0000"}';
Opportunity opp = (Opportunity) JSON.deserialize(OppJSON, Opportunity.class );

But I would like to instead of manually specify the datetime of LastModifiedDate to take today's date/time and substract 365 days. What would the syntax look like for this? I know I can do this:

Datetime oneyearago = System.today().addDays(-365);
String value = String.valueOf(oneyearago);

but I don't know how to use that value string in my json. Thanks in advanced.

1
  • If you have the date in the correct format, inserting it in your JSON is trivial. Just concatenate the constant JSON string with your timestamp using the '+' operator
    – mkorman
    Commented Jan 6, 2016 at 18:21

2 Answers 2

4

There's probably a neater way to do it but you could just serialize the date into a string like so:

Datetime oneyearago = System.today().addDays(-365);
String jsonDatetime = JSON.serialize(oneyearago);

Which outputs the specified date in the correct format:

2015-01-06T00:00:00.000Z

In response to your comment:

String OppJSON = '{"attributes":{"type":"Opportunity","url":"/services/data/v25.0/sobjects/Opport‌​unity/500E0000002nH2fIAE"},"Id":"500E0000002nH2fIAE","LastModifiedDate":' + jsonDatetime + '}';

Opportunity opp = (Opportunity) JSON.deserialize(OppJSON, Opportunity.class );

Updated with a string that works correctly, but really you should look into crop1645's approach as that is more aligned with unit testing best practices.

4
  • Davin, thanks for the quick response, now that I have the jsonDatetime how should I use it while I create the opportunity record: String OppJSON = '{"attributes":{"type":"Opportunity","url":"/services/data/v25.0/sobjects/Opportunity/500E0000002nH2fIAE"},"Id":"500E0000002nH2fIAE","LastModifiedDate":"2015-01-06T17:54:26.000+0000"}'; Opportunity opp = (Opportunity) JSON.deserialize(OppJSON, Opportunity.class ); How do I replace this part: "LastModifiedDate":"2015-01-06T17:54:26.000+0000"}'; with the jsonDatetime string that you created? Thanks again.
    – Leo
    Commented Jan 6, 2016 at 17:11
  • Updated my answer Commented Jan 6, 2016 at 18:39
  • Thanks. I tried that and it didn't like something, look this is the debug log: System.JSONException: Unexpected character ('2' (code 50)): was expecting comma to separate OBJECT entries.
    – Leo
    Commented Jan 6, 2016 at 19:15
  • Updated my answer (again). Commented Jan 6, 2016 at 22:42
2

One way around this issue is to create testdata with whatever dates you want, including the current date and change the logic of your application to rely on a Utility class property:

Date todayDt {
    get {return todayDt == null ? Date.today() : todayDt;}
    set;
}

and everywhere in your PROD code when you need to reference "today", you don't use Date.today() but instead Utility.todayDt

Your testmethod can thus create testdata with audit field dates to whatever you want (from today onwards) and then before you execute test.StartTest(), do

Utility.todayDt = Date.today().addDays(180); // example - for creating testdata with audit dates 0...180 days in past

Your PROD code will think that "today" is 180 days from the real today but as long as all references to Date.today() are changed to the Utility.todayDt; everything works. Sort of a "back to the future"

1
  • hmm...that makes sense, I will try it and see if it works on my Dev Sandbox. Thank you @crop1645!
    – Leo
    Commented Jan 22, 2016 at 21:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .