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

Yes. I don't see any issue. If this doesnt work, bay be you should use GlideRecord

 

var gp = new GlideRecord('sys_user');

gp.addQuery('sys_id', g_form.getValue('requested_for'));

gp.query(callBack);

 

 

function callBack(gp){

if (gp.next()){

g_form.setValue('contact_number', gp.phone);

  }

}


Please mark this response as correct or helpful if it assisted you with your question.

Jim Coyne
Kilo Patron

The "alert(caller)" line is being executed before the getReference call is even completed because of the use of the callback function so it makes sense that it is undefined at that point.

And the Company field is not changing because you are using setDisplayValue with a sys_id (caller.company) when it is expecting the display value of a Company record.  It's trying to find a Company record where Name = sys_id, which will not find anything.

Switch it to "g_form.setValue('slt_company', caller.company);" and it should work.

However, you should really combine both scripts and use a GlideAjax call instead.  Take a look at my response in this post to see how the GlideAjax call can return multiple values for you in order to improve performance - https://community.servicenow.com/community?id=community_question&sys_id=6bb5f263db7f1b4067a72926ca96...

So I moved by alert to the bottom of my script and I'm still getting 'undefined' and I updated the g_form.getDisplayValue to g_form.setValue and the value as a sys_id, but at least it's changing now.

var caller = g_form.getReference('requested_for', setCompany);
function setCompany(caller) {
if (caller){
g_form.setValue('slt_company', caller.company);
}
}
alert("From Company Script " + caller);
}

 

I'm going to try the AJAX method.

 

Lisa

var caller = g_form.getReference('requested_for', setCompany);
function setCompany(caller) {
if (caller){
g_form.setValue('slt_company', caller.company);

alert("From Company Script " + caller.company); // it should be here
}
}


I'm thinking

alert("From Company Script " + caller.company);

...with the extra ".company" might have worked for you?