Catalog item_catalog client script is not working

shaikkhasim
Tera Contributor

Hi Team

i have created a catalog item, 

My Requirement is , once user Name is selected . email filed should be fill automatically of that persons manager 

 

i used the catalog client script but it is not working , pls find the attached images 

 

Regards

khasim

khasim
10 REPLIES 10

Hi Ankur

My version it Tokyo, however auto population field is not visible in my instance 

we can achieve these with script include and ajax & auto populated filed which was available in Utah version.. but my question is i want to achieve through catalog client script only, is it possible or not

 

Regards

Khasim

 

khasim

@shaikkhasim 

yes it's possible

please share the script here and not the screenshots of the script

what debugging have you done so far?

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

Hi Ankur

 

pls check the below code

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var mangager1=g_form.getValue("user_name");
var t1=manager1.manager;
g_form.setValue("email","t1");
   //Type appropriate comment here, and begin script below
   
}
khasim

Hi Ankur 

pls help in these case

 

Regards

Khasim

khasim

Danish Bhairag2
Tera Sage
Tera Sage

Hi @shaikkhasim ,

 

Note: Use proper backend name of ur variables wherever required.

 

Here is the solution to this -

Create a new client callable script include with following function 

 

var UserDetails = Class.create();
UserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInfo: function() {
var details = {};
var userId = this.getParameter('sysparm_user_id');
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id', userId);
userObj.query();
if (userObj.next()) {
details.email= userObj.email_id.toString();
}
return JSON.stringify(details);
},
type: 'UserDetails'
});

 

Create a new catalog client script on the record producer/request form

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

var ajax = new GlideAjax('UserDetails');
ajax.addParam('sysparm_name', 'getUserInfo');
ajax.addParam('sysparm_user_id', g_form.getValue('employee_name')); // change variable name here
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = JSON.parse(answer);
g_form.setValue('var_email_id', answers.email.toString()); // change variable name here
}
}

 

Thanks,

Danish