Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Client Script to auto populate field based on another field not working

Not applicable
I want to display the 'parent' value in the 'project' field based on the budget. I tried a getReference and that kept returning the sys id. How do I return the value correctly using displayBox method or getDisplayValue?
 
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var budget1 = g_form.getDisplayBox('parent').value;
   


 
   
g_form.setValue('project', budget1.parent);
7 REPLIES 7

dhanrajb
Tera Guru

Hi @Community Alums ,

 

Try this code and make sure the backend value of the fields are correct.

 

var budget1 = g_form.getDisplayValue('parent');
g_form.project = budget1;

 

 

Regards,

Dhanraj

dgarad
Tera Sage

Hi @Community Alums 

Please try with the script below.



function onChange(control, oldValue, newValue, isLoading) {

 

    var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function

 

}

 

function doAlert(caller) { //reference is passed into callback as first arguments

 

  g_form.setValue('email',caller.email);

 

  g_form.setValue('phone',caller.mobile_phone);

 

  g_form.setValue('phone',caller.location);

 

}

Please change the variable name 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Sandeep Rajput
Tera Patron

@Community Alums Please update the client script as follows.

 

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

    var budget1 = g_form.getReference('budget', callback);
   
function callback(budget1){
g_form.setValue('project', budget1.parent);
}
}

Not applicable

This script returns the sys_id. I want the display value to show.