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

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

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

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.

e__rajesh_badam_0-1743691199810.pnge__rajesh_badam_1-1743691223167.png

 

 

 

When we 
Is there any possibility to in above client Script

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

Thank you @Nishant8 for the suggestion and the Mistake which i did (Bold and Underlined)

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 } else if (newValue == '') {
        g_form.clearValue('u_show_start_date');
        return;
    }