Auto Populate date loading in YYYY-MM-DD instead of DD-MM-YYYY. Looking for Help

e__rajesh_badam
Mega Guru

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:

 function onChange(control, oldValue, newValue, isLoading, isTemplate) {
     if (isLoading || newValue === '') {
         return;
     }
     var userObject= g_form.getReference('u_show_impacted', doAlert);


     function doAlert(userObject) {
       
             g_form.setValue('u_show_start_date', userObject.getValue('u_start_date'));
       
     }
 }


Table data:

e__rajesh_badam_0-1743667469878.png

 

Incident reflection:

 

e__rajesh_badam_2-1743667564060.png

 

 

 

4 ACCEPTED SOLUTIONS

Nishant8
Giga Sage

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

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@e__rajesh_badam 

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

get date format.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

e__rajesh_badam
Mega Guru

Hi Everyone,

 

I Achieved it with below script:

Client Script 

 function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var userObject = g_form.getReference('u_show_impacted', updateShowStartDate);


    function updateShowStartDate(userObject) {

        // g_form.setValue('u_show_start_date', userObject.getValue('u_start_date'));

        var startDate = new Date(userObject.getValue('u_start_date'));
        var show_start_date = formatDate(startDate, g_user_date_format);
        g_form.setValue('u_show_start_date', show_start_date);
    }
}

View solution in original post

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

View solution in original post

10 REPLIES 10

GlideFather
Tera Patron

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! */


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

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! */


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 :-

 

Shivalika_0-1743669523847.png

 

 

Shivalika_1-1743669535130.png

 

 

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