Autopopulating a catalog item variable based on another

DTrishman
Tera Contributor

Good morning,

 

I have created a catalog item which has a Requested For variable allowing the caller to select the user the request is meant for. I would like to have a read only variable field auto-populate the "sys_user.u_employee_id" of the user selected in the Request For field. How should I build this variable? My attempt is below.

DTrishman_0-1696511237300.png

 

6 REPLIES 6

Hi ,

Please write the script include and client script to populate the required details of the employee.

Miroslaw M
Tera Contributor

Hi this should be pretty simple. 

 

Screen Shot 2023-10-05 at 2.21.16 PM.png

1. create the variable of "employee id" under the requested for.

2. set a ui policy with no conditions to always gray out (read only) the employee id field.

3. create an AJAX script that in client script takes the "requested for" variable and based on who that person is goes to the script include and brings over the users employee id.

 

HERE IS AN EXAMPLE OF HOW I AM DOING THIS BUT FOR OTHER FIELDS:

 

Based on requested for i populate department (along with other fields)

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        return;
    }
 
    var ga = new GlideAjax('AjaxSecReq');
    ga.addParam('sysparm_name', 'getCatVariables'); //function name getCatVariables
    ga.addParam('sysparm_parameter', newValue); // passing in u_requester
    ga.getXML(RetrieveData);
 
    function RetrieveData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var myObject = JSON.parse(answer);
        g_form.setValue('requester_phone_number', myObject.phone);
        g_form.setValue('requester_email', myObject.email);
        g_form.setValue('department_name', myObject.depName);
g_form.setValue('manager', myObject.manager);
    }
}
 
 
SCRIPT INCLUDE:
 
var AjaxSecReq = Class.create();
AjaxSecReq.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
getCatVariables: function(){
var requester_sys_id = this.getParameter('sysparm_parameter');
var userInformationObject = {
'phone':'',
'email':'',
'manager':'',
'depName':''
};
 
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", requester_sys_id);
gr.query();
if (gr.next()) {
userInformationObject.email = gr.email.toString();
userInformationObject.phone = gr.phone.toString();
userInformationObject.depName = gr.department.toString();
userInformationObject.manager = gr.manager.toString();
}
return JSON.stringify(userInformationObject);
},
 
    type: 'AjaxSecReq'
});