Service Portal - AutoPopulate Fields from a Reference Field (sys_users table)

JersonSk
Tera Contributor

I am creating a Service Portal with a reference field.
This field is Selecting the User from the User table.

I have 6 fields that I am adding to extract information from the user selected in the first reference field

  • employee_id
  • title
  • manager
  • company
  • department
  • location

 

I have created this Script Include:

    getEmployeeInfo: function() {
            var userSysId = this.getParameter('sysparm_userSysId');
            if (!userSysId) {
                return '';
            }

            var userGR = new GlideRecord('sys_user');
            if (userGR.get(userSysId)) 
                var obj = {};
                obj.locSysId = userGR.getValue('location');
                obj.manSysId = userGR.getValue('manager');
                obj.titSysId = userGR.getValue('title');
                obj.compSysId = userGR.getValue('company');
                obj.deptSysId = userGR.getValue('department');
                obj.employeeIdSysId = userGR.getValue('employee_id');

                var json = new JSON();
                var data = json.encode(obj);
                return data;
            }
    },
    type: 'GetDepartment'
});
 
 
 
and created this Client Script onChange:
 
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
try{
    var ga = new GlideAjax('GetDepartment');
    ga.addParam('sysparm_name', 'getEmployeeInfo');
    ga.addParam('sysparm_userSysId', newValue);
    ga.getXMLAnswer(function(response) {
        var data = response;
        var employeeInfo = data.evalJSON();

        if (employeeInfo) {
            g_form.setValue('department', employeeInfo.deptSysId);
            g_form.setValue('location', employeeInfo.locSysId);
            g_form.setValue('manager', employeeInfo.manSysId);
            g_form.setValue('company', employeeInfo.compSysId);
            g_form.setValue('title', employeeInfo.titSysId);
            g_form.setValue('employee_id', employeeInfo.employeeIdSysId)
        }
    });
}
 
 
Could someone assist me to populate these 6 fields in the Service Portal? 
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Are you creating a Catalog Item with variables, to use in the Service Portal?  If so you can do this in the Utah and later versions without any scripts using the variable Auto-populate feature.  On each of six variables, select the user reference as the dependent question, then select the field you want from the Dot walk field.

 

If this is not a Catalog Item, or you are not yet on Utah or later, Is your Script Include Client callable with 

...Object.extendsObject(AbstractAjaxProcessor, {

on line 2?  If so, add some gs.info lines to log if the script is running, and to see how far it is getting, what it is returning, etc.

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Are you creating a Catalog Item with variables, to use in the Service Portal?  If so you can do this in the Utah and later versions without any scripts using the variable Auto-populate feature.  On each of six variables, select the user reference as the dependent question, then select the field you want from the Dot walk field.

 

If this is not a Catalog Item, or you are not yet on Utah or later, Is your Script Include Client callable with 

...Object.extendsObject(AbstractAjaxProcessor, {

on line 2?  If so, add some gs.info lines to log if the script is running, and to see how far it is getting, what it is returning, etc.

Thank you for your answer. I am still learning, and I quite forgot to use Auto-populate and use scripts

You are welcome.  I'm happy to help!