Skip to main content
added 26 characters in body
Source Link
Davin Casey
  • 4.2k
  • 1
  • 21
  • 35

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 );

Basically just replace the hardcoded valueUpdated with the variable.

P.S. There may a formatting issue therestring that works correctly, but really you get the ideashould look into crop1645's approach as that is more aligned with unit testing best practices.

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 );

Basically just replace the hardcoded value with the variable.

P.S. There may a formatting issue there, but you get the idea.

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.

added 451 characters in body
Source Link
Davin Casey
  • 4.2k
  • 1
  • 21
  • 35

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 );

Basically just replace the hardcoded value with the variable.

P.S. There may a formatting issue there, but you get the idea.

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

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 );

Basically just replace the hardcoded value with the variable.

P.S. There may a formatting issue there, but you get the idea.

Source Link
Davin Casey
  • 4.2k
  • 1
  • 21
  • 35

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