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

e__rajesh_badam
Mega Guru

@Ankur Bawiskar  could you please help me on it.

Ankur Bawiskar
Tera Patron
Tera Patron

@e__rajesh_badam 

check what came in alert?

Is that record having date in it?

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
    alert('date is' + userObject.u_show_start_date);
    g_form.setValue('u_show_start_date', userObject.u_show_start_date);
}
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Just on the form loading (without selecting Show Impact value )only it is showing as Date is undefined

e__rajesh_badam_0-1743604769046.png

 

@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