- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago - last edited a month ago
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
you can use the below script logic
var gdt = new GlideDateTime();
var day = gdt.getDayOfMonthLocalTime();
var month = gdt.getMonthLocalTime();
var year = gdt.getYearLocalTime();
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
gs.print(day+'-'+ monthNames[month]+'-'+year);
Thanks ands Regards
Sai Venkatesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Thanks Sai I did the same, but it is not working for other user who are having same field in 2024-09-08 format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Thanks @MaryG
After all changes I am using script below which is setting the date as 02-Apr-2027 but for other user they are not using this format they are Insead using 2024-09-08 and they are getting invalid error. So not sure what exactly I have to set using Script.
