Problem/Motivation

I came across this issue while syncing User Last Login data with Salesforce.

This issue is applicable on Salesforce 8.x-4.x and 8.x-3.x.

There are essentially 3 problems.

Case: date/datetime is 0

Code that is run

$date = new DrupalDateTime('0', 'UTC');
$value = $date->format(DateTime::ISO8601);

$value is '' and this may runs into validation errors in Salesforce.
[{"message":"Cannot deserialize instance of date from VALUE_STRING value or request may be missing a required field at [line:1, column:373]","errorCode":"JSON_PARSER_ERROR"}]

Case: date/datetime is empty or ''

Code that is run

$date = new DrupalDateTime('', 'UTC');
$value = $date->format(DateTime::ISO8601);

$value is the current timestamp ('now') and this is incorrect data.

Case: date/datetime is a timestamp

Code that is run

$date = new DrupalDateTime('1585167349', 'UTC');
$value = $date->format(DateTime::ISO8601);

You cannont create a DateTime object from a timestamp this way.
This results in a bad Date object and Salesforce returns the following error.
[{"message":"Cannot deserialize instance of date from VALUE_STRING value or request may be missing a required field at [line:1, column:373]","errorCode":"JSON_PARSER_ERROR"}]

Proposed resolution

I am adding 3 things.
* Handle cases where the field is a timestamp field
* Handle cases where the date value is a timestamp (if it was not a timestamp field)
* Handle cases where the date value is empty() (returns NULL)
* Hanlde any outside cases (returns NULL)

Addendum:
* Handle 0, 0.0, and '0' date values as timestamps instead of emtpy values

Comments

jonnyeom created an issue. See original summary.

jonnyeom’s picture

Status: Active » Needs review
StatusFileSize
new1.74 KB

This patch does the 3 things mentioned in the description.
This patch works for both 4.x and 3.x branches.

Please let me know if you think this there needs to be more changes.

aaronbauman’s picture

Thanks for this patch.
Can you join the discussion on #3114456 please?
#3114456: Optional date field getting set to 'now' on push

bob.hinrichs’s picture

I tried patch #2, and it resolved my issue.

armyguyinfl’s picture

I believe this issue occurred from this issue: https://www.drupal.org/node/3074441#comment-13721132

  • This can also cause validation rule exceptions in Salesforce for future dates if one exists in your org.
  • If I am not mistaken, this will pollute any date fields that are mapped where you are trying to send a null value. You may want to look at your mappings and see if there are any use cases that you are expected NULL dates like a DOB or any mapped date fields and do data cleanup if already deployed 8-x.3.5+.
ironsizide’s picture

I found an edge case around the 0, 0.0, and '0' values that would normally evaluate to empty, but would generate a valid timestamp, so I've added logic to cause those values to drop through to the timestamp logic, and added an int type casting to take care of proper creation on textual timestamps such as '0'.

I've also added tests for the various date scenarios.

This patch works against the latest version of the 4.x branch. I have not tested it against the 8.x.3.x branch.

Status: Needs review » Needs work
ironsizide’s picture

Status: Needs work » Needs review

Well the PullQueueTest failure was unanticipated. If anyone can figure out why that's failing before I can get to it, I'd be most appreciative.

aaronbauman’s picture

I turned off pull settings for the new mapping - let's see if that appeases testbot

jonnyeom’s picture

2 more changes here.

  1. Added separate logic to handle Drupal Timestamp fields.
    So it looks like when you create a new DrupalDateTime object with a timestamp value, the timestamp value will be treated as a broken datetime string. As a result, I was getting incorrect date objects, with years like 5255 or 1160.

    Instead, I added a field check to see if it is a timestamp field and handle those fields separately.
  2. Updated format to use \DateTime::ATOM
    This is just a best practice thing.\DateTime::ISO8601 is actually not the proper ISO8601 format. ATOM is.

Adding both 4.x and 3.x patches

aaronbauman’s picture

Status: Needs review » Needs work

I'm not sure if the format change is incorrect, or the test needs to be updated. NW

acrosman’s picture

I believe the move to \DateTime::ATOM in #10 is correct, although Salesforce documents that they are looking for ISO 8601.

Throughout the Rest API guide you see the following for date formats:

The date and time should be provided in ISO 8601 format: YYYY-MM-DDThh:mm:ss+hh:mm.

But the PHP DateTime docs for that format are:

  • ISO 8601: const string DateTimeInterface::ISO8601 = "Y-m-d\TH:i:sO" ;
  • ATOM: const string DateTimeInterface::ATOM = "Y-m-d\TH:i:sP" ;

The difference being O vs P, which is the difference between that colons between hours and minutes.

From the date format docs of PHP:

O Difference to Greenwich time (GMT) without colon between hours and minutes Example: +0200

P Difference to Greenwich time (GMT) with colon between hours and minutes Example: +02:00

I am still testing the patch in #10 to make sure it works on my current project and will update and try to adjust the tests later this week.

acrosman’s picture

Status: Needs work » Needs review
StatusFileSize
new1.86 KB
new10.63 KB

Updated the patch from #10 to switch the tests to match the SFDC documented datetime format.

Status: Needs review » Needs work
acrosman’s picture

Earlier patches on this issue make a critical error and confuse dates and datetimes. They were handled previously in the same case clause because they were just protected against blanks, but the added handling of datetime format caused errors for plain dates. Revised patch moves the dates into their own clause that only worries about blanks again and assumes correct format. Tests on BirthDate were readjusted to assume proper date format on the salesforce side (even though this doesn't go to Salesforce during a test run that's a date field on the Contact so proper format seemed the right thing to test). This patch still needs new fields added to the tests to cover a Datetime field. Other processing on the date field tests might also need to be checked once the test runner completes.

acrosman’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
acrosman’s picture

I spent some time today looking to work on this patch a bit, and ended up questioning the current need for any change related to date and date/times.

I installed a fresh copy of the Drupal 8, SFS 8.x-4.2, connected it to ta simple Trailhead training org, using JWT authentication. I mapped contacts to a quickly created node type linking name fields, birthday, Last modified date, and a custom Salesforce field for date/time. For the date and date/time fields I mapped Drupal field's "Date Value" field.

  • The Birthdate field worked out of the box without error for updates in either system including for blank values.
  • The Date/Time field worked just fine for the same tests, including running to be okay with a different timezone in each system (Drupal on UTC, SF User on PDT).
  • The timestamp field failed to set correctly, with Drupal reading the last modified date as epoch.

I think this means that the fixes pushed in other issues may have largely resolve the issue for dates and date/times. Only timestamps seem to have ongoing problems with the released version of the suite.

aaronbauman’s picture

Status: Needs work » Closed (outdated)

I'm doing some cleanup on unsupported branches.
Please re-open this issue and update the version to 5.0.x-dev if this issue is still applicable to the latest release.