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
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | interdiff_14-16.txt | 7.35 KB | acrosman |
| #16 | salesforce-better_handle_date_datetime_data-3122412-16.patch | 9.02 KB | acrosman |
Comments
Comment #2
jonnyeom commentedThis 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.
Comment #3
aaronbaumanThanks for this patch.
Can you join the discussion on #3114456 please?
#3114456: Optional date field getting set to 'now' on push
Comment #4
bob.hinrichs commentedI tried patch #2, and it resolved my issue.
Comment #5
armyguyinfl commentedI believe this issue occurred from this issue: https://www.drupal.org/node/3074441#comment-13721132
Comment #6
ironsizide commentedI 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.
Comment #8
ironsizide commentedWell 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.
Comment #9
aaronbaumanI turned off pull settings for the new mapping - let's see if that appeases testbot
Comment #10
jonnyeom commented2 more changes here.
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.
\DateTime::ATOMThis is just a best practice thing.
\DateTime::ISO8601is actually not the proper ISO8601 format. ATOM is.Adding both 4.x and 3.x patches
Comment #12
aaronbaumanI'm not sure if the format change is incorrect, or the test needs to be updated. NW
Comment #13
acrosmanI believe the move to
\DateTime::ATOMin #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:
But the PHP DateTime docs for that format are:
const string DateTimeInterface::ISO8601 = "Y-m-d\TH:i:sO" ;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:
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.
Comment #14
acrosmanUpdated the patch from #10 to switch the tests to match the SFDC documented datetime format.
Comment #16
acrosmanEarlier 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.
Comment #17
acrosmanComment #19
acrosmanI 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.
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.
Comment #20
aaronbaumanI'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.