ccajohnson
Kilo Sage

You may be asked to populate a field or Variable from a selected User record. I have seen many different solutions that do this, but I have found that an AJAX call method is the most efficient way to pull the information from the user record.

1. Create the usrUtilsAjax script include.

Name: usrUtilsAjax
API Name: global.usrUtilsAjax
Client callable: true
Application: Global
Accessible from: All application scopes
Description: Custom script include containing client callable User related methods
Script:

var usrUtilsAjax = Class.create();
usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    /**
     * Gets field values from a User record on the User table
     *
     * @param      {string}  sysparm_usr     The sys_id of the user record to get.
     * @param      {string}  sysparm_fields  Comma deliminated list of fields.
     *                                       Use -d to indicate getting the display
     *                                       value of the field instead.
     * @return     {JSON}    Encoded JSON String
     */
    getUserFieldValues: function() {
        var usrID = this.getParameter('sysparm_usr');
        var fString = this.getParameter('sysparm_fields');
        fArray = fString.split(',');
        var usrJSONReturn = {};
        var usrObj = new GlideRecord('sys_user');
        if(usrObj.get(usrID)) {
            var fName = '';
            for(var i = 0; i < fArray.length; i++) {
                var fValue = '';
                if (fArray[i].toString().indexOf('-') > -1) {
                    dArray = fArray[i].split('-');
                    fName = dArray[0] + '';
                    fValue = usrObj[fName].getDisplayValue();
                } else {
                    fName = fArray[i] + '';
                    fValue = usrObj[fName];
                }
                usrJSONReturn[fName] = fValue + '';
            }
        }
        var encJSONReturn = new JSON().encode(usrJSONReturn);
        return encJSONReturn;
    },

    type: 'usrUtilsAjax'
});

2. Use Case:

For this use case we want to populate two variables: Department [usr_department], Email [usr_email] when the User [usr_requested_for] variable changes.

3. Solution:

Create a catalog client script that is triggered when the User variable changes that encorporates the newly added script include. Since the department is a Reference type field and the variable we are copying to is a Single Line Text type variable, we will want to encorporate the -d switch to indicate we want the display value of the field and not the value (which would be the sys_id and not desired).

Name: Set User Details onChange
UI Type: All
Type: onChange
Variable name: usr_requested_for
Applies on a Catalog Item view
Script

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

    if (newValue == '') {
        g_form.setValue('usr_department', '');
        g_form.setValue('usr_email', '');
        return;
    }

    var fieldsToGet = 'department-d,email'; 
    var ga = new GlideAjax('global.usrUtilsAjax');
    ga.addParam('sysparm_name', 'getUserFieldValues');
    ga.addParam('sysparm_usr', newValue);
    ga.addParam('sysparm_fields', fieldsToGet);
    ga.getXML(setVariables);

    function setVariables(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        var userDataObj = JSON.parse(answer);
        g_form.setValue('usr_department', userDataObj.department);
        g_form.setValue('usr_email', userDataObj.email);
    }
}

:This will handle all of the fields that are found on the user table. If there are others, you can always add additional functions to the script include to serve your purpose.

Comments
JohnQ1
Tera Contributor

Chris, Thanks for sharing!  Nice write-up and documentation. 

JohnQ

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Because you mention catalog client script in your article, why not going for a no-code solution? Why script?Catalog Data Lookup Definitions.

Also curious, why using getXML, and not getXMLAnswer?

And department is a reference? So within your setValue, you should use the value + displayValue. Any reason for not doing so?

Kind regards,
Mark

Version history
Last update:
‎08-06-2021 11:10 AM
Updated by: