2

An apex controller invoked by vf page in Salesforce orgs (both sandbox and prod) start to fail recently. No code update was made for years on this apex file.

The failing line is like Datetime.parse('15.12.2019 13:00')

The exception is:

System.TypeException: Invalid date/time: 15.12.2019 13:00

However, if I run this line below in anonymous Apex in dev console, there is no exception.

System.debug('Prc----'+Datetime.parse('15.12.2019 13:00'));

I have seen win20 updates related to DateTime here, but couldn't figure out if this is the cause.

Updates

  • I tested to access the vf page with the system admin user (the one ran anonymous Apex above) instead of the public portal user used behind the vf, the page does NOT throw exceptions. So the issue is definitely relating to the user!
  • However, the system admin user and the public portal user are using the exactly same locale.
  • The public portal user had no update since 1 year ago, not sure if locale is really the root cause.
  • 3
    DateTime.parse() uses the locale (date format) of the running user. Is the running user for the Visualforce page the same as the running user for the Anonymous Apex test? – David Reed Dec 4 '19 at 15:38
  • Any chances that user profile locale settings has been changed recently? – kurunve Dec 4 '19 at 15:38
  • @DavidReed it is different users. the user in Apex controller doesn't have sufficient permission, I cannot "login as" and open dev console to run anonymous – Xi Xiao Dec 4 '19 at 15:46
  • @kurunve I checked the user time stamp, it was not updated for over 1 year. Even a couple of days ago this function worked well (I can see valid data was entering database). – Xi Xiao Dec 4 '19 at 15:47
  • @Xi could you edit and add these details? It might also be helpful to share what locale both users are in. – David Reed Dec 4 '19 at 15:53
2

Hard to know what change has made the code start to fail. But passing a hard-coded string (or wrongly formatted string) to a parse method that takes account of the user's locale is not code that you should keep given that a user's locale can be changed easily. It's always worth understanding the root cause of a problem, but occasionally you just have to resort to making a fix because the root cause is not entirely clear.

The DateTime.valueOf accepts this fixed string format yyyy-MM-dd HH:mm:ss and so is a better choice being unaffected by the user's locale.

| improve this answer | |
1

Someone in the future might hit the same problem. Here is our solution:

Specifically add language="xx" attribute into the apex page <apex:page>. This forces the DateTime.parse() using the correct string format.

I haven't had the chance to test what Keith C mentioned. You should check his answer. Indeed, parsing strings depends on user locale is a bad idea.

Other info

  • Not sure why it was not needed and all of sudden is needed now.
  • We only need to support one language, so adding the attribute won't cause a problem.

How did we even come up this was the cause?

We have had some hard lessons from implementing Salesforce SSO, this language attribute was one of the lessons.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.