Monday, November 20, 2017

Salesforce: Time field (Beta)

This blog is written with Winter '18 release, where time field still in Beta, let us expect many improvements in Spring '18 release and beyond. This feature is enabled by default in Sandbox, but you need to request Salesforce if you would like to enable it for the production.

This function has been requested in Idea Exchange since 10 years ago, and at this moment it has 5,530 points.

There are 2 types of Time fields in Winter '18 beta:
- as a Custom field with Data Type = Time, or
- as a Formula field with Return type = Time

1. Custom field with Data Type = Time
As of now, the display in Lightning is not great, but much nicer in Classic.

As this is not formula field, users with appropriate permission able to type in the time directly into the page layout.

In Classic, you can enter the time in 24 hours format:
- "18", when you save, this will save as 6:00 PM
- "18:12", when you save, this will save as 6:12 PM
- "7:12", when you save, this will save as 7:12 AM
- "7.32", when you save, this will save as 7:00 AM  --> 18.12 (dot) will be ignored

You also can enter the time in 12 hours format with AM/PM (in Classic):
- "12 AM", when you save, this will save as 12:00 AM
- "12:30PM", when you save, this will save as 12:30 PM

While in Lightning, you need to enter in pattern of HH:MM:SS, example: 07:30:00, or  19:00:00, once stored, it will display as 07:30:00.000Z or 19:00:00.000Z

2. Formula Field return Time
Sample: MINUTE(Approved_Time__c), HOUR(Approved_Time__c)
To calculate hours and minutes between time fields, you cannot just minus or add them directly, Submission_Time__c - Approved_Time__c will return error "Incorrect parameter type for operator '-'".

To display the difference between 2 times, you can use formula field to return the formula as text, example:
IF( (MINUTE(Approved_Time__c) - MINUTE(Submission_Time__c)) >=0,
TEXT((HOUR(Approved_Time__c) - HOUR(Submission_Time__c)))
& ":" &
TEXT((MINUTE(Approved_Time__c) - MINUTE(Submission_Time__c))),
TEXT((HOUR(Approved_Time__c) - HOUR(Submission_Time__c) - 1))
& ":" &
TEXT((MINUTE(Approved_Time__c) - MINUTE(Submission_Time__c) + 60))

To read the complete documentation, read this article, and if you have further questions, feel free to join Custom Time Field success community.

Update since Spring '18 release, now you can calculate time field to return Number, example:
End_Time__c - Start_Time__c
This formula will return the time difference in milliseconds; 1 hour = 3,600,000 milliseconds.