- 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: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 07:41 AM
Hi @Ankur Bawiskar ,
Sorry to back again on it.
The script was working as mentioned but when am clearing value in Show Impacted field(Calle name in your scenerio) the Data field value is not getting clear automatically and even after Saving the form also.
Please help me on it.
When we
Is there any possibility to in above 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 08:28 AM
Thank you @Nishant8 for the suggestion and the Mistake which i did (Bold and Underlined)