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

vinothkumar
Tera Guru

You have to modify as below

 

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

 

function setUserInfo(abc) {
if (abc){

g_form.setValue('Manager', abc.manager);

g_form.setValue('Location', abc.location);

g_form.setValue('Division', abc.department);

}

Hi Vinoth,

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

- Sai.

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

Naveen63
Tera Contributor

Hi Sai,

 

Please try below script for auto populate catalog item:

 

SC Client Script - function onLoad() {

 
var Recipient = g_form.getValue('variables.recipient');
 
/
var geRecipient = new GlideAjax('autopopulate');
geRecipient.addParam('sysparm_name', 'recipientInformation');
geRecipient.addParam('sysparm_user_sysId', Recipient);
geRecipient.getXML(autoPopulateFields);
}
 
function autoPopulateFields(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
 
var arrRecipientInfo = answer.split('!#!');
 
 
if(arrRecipientInfo[0] != '') {
g_form.setValue('variables.yourmanagervariablename', arrRecipientInfo[0]);
}
 
if(arrRecipientInfo[1] != '') {
g_form.setValue('variables.yourlocationvariablename', arrRecipientInfo[1]);
}
 
 
if(arrRecipientInfo[2] != '') {
g_form.setValue('variables.yourdepartmentvariablename', arrRecipientInfo[2]);
}
 
 
 
}
 
 
Script include:
 
Script Include - var autopopulate = Class.create();
autopopulate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
recipientInformation: function() {
 
var userInfo = "User not found";
var user_sys_id = this.getParameter('sysparm_user_sysId');
var grUser = new GlideRecord('sys_user');
 
if(grUser.get(user_sys_id)) {
 
userInfo = grUser.manger; 
userInfo+= '!#!'+grUser.location; 
userInfo += '!#!'+grUser.department.getDisplayValue(); 
 
 
 
}
 
 
return userInfo;
},
});
 
 
 
Mark this answer as Correct and Helpful if helps.
 
Regards,
Naveen