I am creating a Rest API. I have one DateTime field(one of the field for POST request). Wondering what happens if someone calls the API, makes the POST request and fills out just the Date part of the field and leave the time part empty for the field. Would the API give an error since its a datetime field or would it go through fine?


2 Answers 2


If you're creating a REST API in Salesforce that other systems call into, then your result depends on how the variable is defined in your request class. If your request type serializes the field as a a datetime variable - it'll do what @cropredy says. However, if you serialize it as a String variable, the JSON will serialize, but upon inserting the record, you will get a System.TypeException: Invalid date/time

For example, consider the following code. In the JSON string the datetime is supplied and is serialized in req1 and the insert works as the previous poster showed. However, in req2, the serialization works, but the DML fails, because the Type is incorrect.

Hope this helps!

public class YourAPI {

    public static void requestHandler(){
        String jsonString = '{"lastName" : "foo","dateTimeStamp" : "2000-01-01"}';
        Request_dt req1 = (Request_dt)JSON.deserialize(jsonString, Request_dt.class);
        System.debug('req1' + JSON.serializePretty(req1));
        Request_Str req2 = (Request_Str)JSON.deserialize(jsonString, Request_Str.class); 
        System.debug('req2' + JSON.serializePretty(req2));

        Contact c1 = new Contact(
            FirstName = 'foo', 
            LastName = req1.lastName, 
            Email = '[email protected]', 
            Date_Time_Field__c = req1.dateTimeStamp);

        Contact c2 = new Contact(
            FirstName = 'foo',  
            LastName = req2.lastName, 
            Email = '[email protected]',             
            Date_Time_Field__c = DateTime.valueOf(req2.dateTimeStamp) //will only work if the string is in the format “yyyy-MM-dd HH:mm:ss”
        insert new List<Contact>{c1};

    public class Request_dt{
        String lastName;
        DateTime dateTimeStamp;
    public class Request_Str{
        String lastName;
        String dateTimeStamp;
  • Thanks for the detailed explanation.
    – Student
    May 11, 2019 at 23:51

This is fairly easy to test using Workbench

I used this PATCH REST command


with this body:

  "some_datetime_field__c" : "2000-01-01"

and the field in the Foo__c record is updated to 2000-01-01T00:00:00Z, displaying in my (Pacific) timezone as

12/31/1999 4:00 PM
  • Thanks for replying back. I am not sure if I understood, so it populated the current time by default when you left the time portion blank?
    – Student
    May 11, 2019 at 20:41
  • @Student leaving the time off defaults time to 00:00:00Z
    – cropredy
    May 11, 2019 at 21:53
  • ok which is 7:00 PM Eastern time?
    – Student
    May 11, 2019 at 22:07
  • I'm not sure I understand, if the incoming value is a date only, SFDC will interpret that (in a datetime) as a UTC date at midnight UTC
    – cropredy
    May 11, 2019 at 22:09
  • 1
    @Student you are correct. Datetime values saved in the database are UTC
    – cropredy
    May 11, 2019 at 22:41

You must log in to answer this question.

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