Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Change Date field automcatilly to 1 Year from today

Ashish Kathait
Tera Contributor

On kb_knowledge form when the workflow go to draft, I want to change the valid_to date to 1 year from today automatically. 

 

I am able to do it but format is a issue correct formt is 10-Jan-2023 but while setting it is error out due to 2027-04-01 format.

2 ACCEPTED SOLUTIONS

MaryG
ServiceNow Employee

Hi @Ashish Kathait — just want to flag that the format string above (01-Apr-2027) is actually the UI display format, not the value format ServiceNow expects. Setting a glide_date field using that string will likely cause the same error or set the field incorrectly.

The good news: yyyy-MM-dd is the correct internal format for glide_date fields, so you're actually close. The issue is probably in how you're setting the value, not the format itself.

Here's a Business Rule approach that should work:

var gdt = new GlideDateTime();
gdt.addYears(1);
current.valid_to.setValue(gdt.getDate().getValue()); // returns yyyy-MM-dd

If you're using Flow Designer instead, use the "Add Duration" action to add 1 year to today's date, then set valid_to from that output — it handles the format automatically.

If you're still seeing an error, share the script you're using and where it's throwing — happy to help debug.

View solution in original post

MaryG
ServiceNow Employee

Thanks for sharing the script @Ashish Kathait — that explains exactly what's happening.

The issue is that you're using g_form.setValue(), which is a client-side API. It accepts the display format for your locale (dd-MMM-yyyy), but other users on different locale settings expect a different format — which is why they're getting the invalid error. Client-side date setting is locale-dependent and will behave differently across users.

The reliable fix is to move this to a server-side Business Rule instead, where the format is always consistent:

var gdt = new GlideDateTime();
gdt.addYears(1);
current.valid_to.setValue(gdt.getDate().getValue()); // always yyyy-MM-dd regardless of locale

Set the Business Rule to run on Before Insert/Update, with a condition on whatever triggers the draft state (e.g. current.workflow_state == 'draft').

This way the date is set server-side before the record saves, no client formatting involved, and it works the same for every user.

On Flow Designer — the action name varies by release. Look for "Calculate Date" or check under the "ServiceNow Core" action category for date utility actions. If you don't see it, the Business Rule approach above is the cleaner solution anyway.

View solution in original post

6 REPLIES 6

Also, I do not see Add Duration Action within Flow Designer. 

MaryG
ServiceNow Employee

Thanks for sharing the script @Ashish Kathait — that explains exactly what's happening.

The issue is that you're using g_form.setValue(), which is a client-side API. It accepts the display format for your locale (dd-MMM-yyyy), but other users on different locale settings expect a different format — which is why they're getting the invalid error. Client-side date setting is locale-dependent and will behave differently across users.

The reliable fix is to move this to a server-side Business Rule instead, where the format is always consistent:

var gdt = new GlideDateTime();
gdt.addYears(1);
current.valid_to.setValue(gdt.getDate().getValue()); // always yyyy-MM-dd regardless of locale

Set the Business Rule to run on Before Insert/Update, with a condition on whatever triggers the draft state (e.g. current.workflow_state == 'draft').

This way the date is set server-side before the record saves, no client formatting involved, and it works the same for every user.

On Flow Designer — the action name varies by release. Look for "Calculate Date" or check under the "ServiceNow Core" action category for date utility actions. If you don't see it, the Business Rule approach above is the cleaner solution anyway.