- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:10 AM
Hi Everyone,
Need a help to fix below issue in Servicenow.
Script which i used is to auto populate date in start date when select the Show Name. But the Date in the format of DD-MM-YYYY but in incident form it is loading as YYYY-MM-DD and not allowing to save form throwing error as invalid date.
Client Script:
Table data:
Incident reflection:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:43 AM
Hello @e__rajesh_badam , Can you please try to replace your dlAlert method with below one and share the feedback?
function doAlert(userObject) {
var date = new Date(userObject.getValue('u_start_date'));
g_form.setValue('u_show_start_date', formatDate(date,'dd-MM-yyyy' ));
}
P.S: it better to call SI instead of using g_form.getReference
Regards,
Nishant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:49 AM - edited 04-03-2025 02:10 AM
Please use onChange + GlideAjax and return the displayValue from script include
Whenever you use onChange + getReference then this issue comes up
try this and add logs and alert to see what came
Script Include: It should be client callable
var DateTimeUtils = Class.create();
DateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDateValue: function() {
var sysId = this.getParameter('sysparm_sysid');
var rec = new GlideRecord('tableName');
if (rec.get(sysId)) {
return rec.getDisplayValue('u_start_date');
}
},
type: 'DateTimeUtils'
});
onChange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('DateTimeUtils');
ajax.addParam('sysparm_name', 'getDateValue');
ajax.addParam('sysparm_sysid', newValue);
ajax.getXML(getDate);
function getDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_show_start_date', answer);
}
}
Output: It gave me the correct date and in correct format
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 03:37 AM
Hi Everyone,
I Achieved it with below script:
Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 08:13 AM - edited 04-03-2025 08:14 AM
Hello @e__rajesh_badam , if you want to clear the date field, you can make a small modification in shared Client script as below and try:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
} else if (newValue == '') {
g_form.clearValue('u_show_start_date');
return;
}
var ajax = new GlideAjax('DateTimeUtils');
ajax.addParam('sysparm_name', 'getDateValue');
ajax.addParam('sysparm_sysid', newValue);
ajax.getXML(getDate);
function getDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_show_start_date', answer);
}
}
Regards,
Nishant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:23 AM - edited 04-03-2025 01:43 AM
you can convert it using getByFormat();
Example getByFormat("DD-MM-YYYY");
var valueBefore= g_form.setValue('u_show_start_date', userObject.getValue('u_start_date')); //before conversion
var showStartDate = valueBefore.getByFormat("DD-MM-YYYY");
EDIT: depends whether it is using GlideDate or GlideDateTime.. if the latter option it will require to add the time stamp as well it can be zeros
getByFormat("DD-MM-YYYY HH:MM:SS") >>> getByFormat("31-12-2025 00:00:00");
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:26 AM
Hi @GlideFather ,
I did find it out but not getting where i have to define the code exactly. i.e., in Client Script which i shared above? please suggest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:33 AM
The script that is populating the field with the invalid value, perhaps something like this:
var valueBefore= userObject.getValue('u_start_date');
g_form.setValue('u_show_start_date', valueBefore.getByFormat("dd-mm-YYYY");
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2025 01:39 AM
Hello @e__rajesh_badam
No that is a server side script. So, you might use a "Display BR" in there store the correctly formatted "date" in "g_scratchpad" object. Access it in your client script and you are done.
look the below screeshot :-
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY QQ