How to master the ServiceNow duration field

The ServiceNow duration field allows you to store, guess what, a duration !

While the official documentation is well built, there are still some dark undocumented corners on the platform, where the community stays your last resort. Otherwise, it’ll be each man for himself. Man, this article is drifting away from its main course 😯 !

The ServiceNow duration field is one of these corners, I struggle every time I have to handle it, and it always feels like the first time I’m working on it.

In my opinion, I think that the root cause of that suffering is that there’s no Glide API object to represent durations like it’s the case with GlideDate and GlideDateTime fields.

 

 

That said, I decided to gather the most-faced use-cases, and provide a solution to each one. Stay tuned 🙂

Set a duration value from a non-duration field (like a String or Integer field)

In that use case, imagine you have a source field, let’s say a String, containing a duration expressed in minutes, and you want to initialize a duration field based on that value.

//sourceRecord.rawDuration is a string field containing a duration expressed in minutes
//targetRecord.duration is a duration field

//The conversion varies depending on the raw duration's unity (seconds, minutes, etc.) 
targetRecord.duration.setDateNumericValue(sourceRecord.rawDuration * 60 * 1000);

Compare two durations

In that second scenario, imagine you have two duration fields. You want to compare them so that the field containing the longest duration is considered greater than the other one.

To do so, you’ll have to go through the dateNumericValue() which returns the equivalent of GlideDateTime’s getNumericValue().

return (sourceRecord.durationA.dateNumericValue() > sourceRecord.durationB.dateNumericValue());

Duration fields on transform maps

To map a duration field on a transform map, you can’t go through the ordinary “Field Maps”, instead you have to check “Run script” on your Transform Map definition, then map your target duration with the source field.

(function transformRow(source, target, map, log, isUpdate) {

//In this example, the source field is expressed in minutes
target.durationField.setDateNumericValue(source.rawDuration * 60 * 1000);

})(source, target, map, log, action === "update");

Setting a duration on a client script

If you want to use the setValue method of the g_form object, you have to respect the following format:

DAYS HOURS:MINUTES:SECONDS

So, to set a duration for 1 day and 12 hours, the method should be called like this:

g_form.setValue('durationField', '1 12:00:00');

Conclusion

To have the complete image, I invite you to read the other use cases provided in the official documentation. Leave a comment if you’re still struggling with the ServiceNow duration field, or tell us what use-cases you confronted and how did you resolve the issue.

Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *