How to auto populate catalog item variables?

User177031
Kilo Guru

I want to auto populate the Manager, Location and Department based on 'Requested_For' under Variable Set. Also want to auto populate the same when logged in user try to change the 'Requested_For'.

I tried writing "OnChange" Client script but facing an error on Portal page as "There is a JavaScript error in your browser Console". Attached the screenshot.

Also tried as per below URL but still the issue exists.

https://community.servicenow.com/community?id=community_question&sys_id=196b8b21db9cdbc01dcaf3231f96193b

Can anyone please help me with the Script?

1 ACCEPTED SOLUTION

Vishal Khandve
Kilo Sage

Hi Sai,

 

try below one-

 

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

var gr = g_form.getReference('requested_for',callBack);
}
function callBack(gr){
g_form.setValue('manger',gr.manager);

g_form.setValue('phone_number',gr.mobile_number);
g_form.setValue('email',gr.email);
g_form.setValue('location',gr.location);
g_form.setValue('zipcode',gr.zip);
g_form.setValue('country',gr.country);
g_form.setValue('company_code',gr.company);


}

 

Thank you

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sai,

You need to use getReference() with callback since without callback function it won't work in portal.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

User177031
Kilo Guru

Hi Ankur,

I did the same. Please refer to attached screenshot of Client Script. If there is any issue, can you please try to add the script when you reply?

- Sai

Hi Sai,

 

There is a OOB client script "(BP) Set Location to User" on incident table. You can refer same to see how getReference works. 

 

I don't see a callback function parameter parameter in your getReference.

 

var abc = g_form.getReference('Requested_for'); >> callback function parameter is missing. 

 

Please update this code as below.

var abc = g_form.getReference('Requested_for', setUserInfo);

 

function setUserInfo(abc) {
if (abc){
g_form.setValue('location', abc.location);

}

 

Thanks,

Akshay.

Hi Akshay,

I tried the code as per your suggestion, but it is not populating the values on Portal page.

- Sai.