Need to Populate Date based on selection of the show Name

e__rajesh_badam
Mega Guru

Hi ,

 

In Incident form Show Impacted (reference field ) and Show Start Date are two columns and These two columns are part of one Table only.

 

When I select the Show Name in show impacted column then it need to display related Show Date in Show Start Date field.

 

Instead it is showing value as undefined when selecting particular show name even after it has the date.

 

Please provide suggestions.

 

 

Script:

function onChange(control, oldValue, newValue, isLoading) {
   var userObject = g_form.getReference('u_show_impacted', doAlert); // doAlert is our callback function
}
function doAlert(userObject) { //reference is passed into callback as first arguments
   g_form.setValue('u_show_start_date', userObject.u_show_start_date);
}
 
e__rajesh_badam_0-1743602299890.png
 
e__rajesh_badam_1-1743602520553.png

 

 

3 ACCEPTED SOLUTIONS

J Siva
Tera Sage

Hi @e__rajesh_badam 

Try below 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')); //Make sure to use the correct field name
        
    }
}

NOTE: While setting the date value you are not picking the correct field name. Check that. Also please share the snap of your onchange client script configuration.

As per my script, you onchange client script should be configured on "Show impacted" field and UI type should be "All"

 

Regards ,

Siva

View solution in original post

@e__rajesh_badam 

you should have script when value changes so do this and not when form loads

Ensure all field names are correct

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue == '')
        g_form.clearValue('u_show_start_date');

    if (oldValue != newValue)
        var userObject = g_form.getReference("u_show_impacted", doAlert);

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

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

Hi @Ankur Bawiskar ,

Thank you so much for your quick assistance and I achieved it with your initial code itself by adding date format conditions (bold and Underlined).

 

Here is the Script for reference

 

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

15 REPLIES 15

Thank you @Ankur Bawiskar script was working but date format taking (in incident form) in opposite

Table data:

e__rajesh_badam_0-1743609220233.png

Incident reflection:

e__rajesh_badam_1-1743609266268.png

 

@e__rajesh_badam 

the value you are getting convert that to logged in user's format.

To get the correct date format, you can use GlideAjax call and get the value in logged in user's and return that value

Why not show that Date field as dot walked from reference field and no scripting required?

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

Could you please help me with an example please @Ankur Bawiskar 

 

@e__rajesh_badam 

something like this

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.u_start_date.getDisplayValue();
        }
    },

    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_inc', newValue);
    ajax.getXML(getDate);
    function getDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_show_start_date', answer);
    }
}

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

Date was not populating @Ankur Bawiskar 

 

e__rajesh_badam_0-1743612925958.pnge__rajesh_badam_1-1743612956648.pnge__rajesh_badam_2-1743612981537.png