Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Variable Sets vs Variables and Catalog Client Scripts

kemmy1
Tera Guru

I have a simple onChange Catalog Client Script (the applies to field is "Variable Set").  This is setting the phone number

Variable set: xxxx
Variable Name: requested_for

var caller = g_form.getReference('requested_for', setupUsersPhones);
function setupUsersPhones(caller) {
if (caller){
g_form.setValue('contact_number', caller.phone);
} }

requested_for and contact_number are both variables in the variable set.  THIS CATALOG CLIENT SCRIPT WORKS FINE.

Then I have another simple onChange Catalog Client Script (the applies to field is "Catalog Item").  This is setting the company.  Company is not part of the variable set.

Catalog Item: xxxxxx
Variable name: xxxxxx -> requested_for (xxx is the variable set name)

var caller = g_form.getReference('requested_for', setCompany);

alert(caller);
function setCompany(caller) {
if (caller){
g_form.setDisplayValue('slt_company', caller.company);
}

alert(caller); comes back undefined.  And the company is not changing either. 

Do I need to do something different in my script to "dot walk" it (for lack of a better term) to the variable set?

Lisa

1 ACCEPTED SOLUTION

Using 1 server call is certainly best in terms of performance.

But, if you want to squeeze a bit more out of the script, you should return both the sys_id and the display value for company so when you use g_form.setValue(), you can pass the display value as a third parameter.

g_form.setValue('ref_company', answer.company_sys_id, answer.company_name);

Without the display value, the platform has to go back to the server to find out what the display value is in order to show it.

Are u_branch and u_division reference variables as well?  If so, should do the same with them as well.

View solution in original post

12 REPLIES 12

Jim Coyne
Kilo Patron

Did you find a solution to your issue?

I actually just decided to go the AJAX way and it started to work.  I collected more data as well.  Again company is not part of the variable set, so I'm not sure why it wasn't working with a simple catalog client script.

 

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

                if (isLoading || newValue === '') {

                                return;

                }             

 

                var fields = "u_branch,u_division,company"; //comma seperated list of fields to return

                var ga = new GlideAjax('XXDYNAMICAJAX'); // DHSAJAX is the script include class

                var caller = newValue;

                ga.addParam('sysparm_name','getRecordValues'); // getRecordValues is the script include method we are calling.

                ga.addParam('sysparm_table', 'sys_user'); // setting table to be passed to script include.

                ga.addParam('sysparm_fields', fields); // Set parameter sysparm_fields to user values you want

                ga.addParam('search_value', caller); //Returns the sys_id of the onChange field.

                ga.getXML(ReturnRecords);

               

                // the callback function for returning the result from the server-side code

                function ReturnRecords(response) {

                                var answer = response.responseXML.documentElement.getAttribute("answer");

                                answer = JSON.parse(answer); //Transform the JSON string to an object

                                g_form.setValue('ref_branch', answer.u_branch);            

                                g_form.setValue('ref_division', answer.u_division);         

                                g_form.setValue('ref_company', answer.company);

                }

}

Using 1 server call is certainly best in terms of performance.

But, if you want to squeeze a bit more out of the script, you should return both the sys_id and the display value for company so when you use g_form.setValue(), you can pass the display value as a third parameter.

g_form.setValue('ref_company', answer.company_sys_id, answer.company_name);

Without the display value, the platform has to go back to the server to find out what the display value is in order to show it.

Are u_branch and u_division reference variables as well?  If so, should do the same with them as well.