[Tips & Tricks] Populate Department name based on selected User name

[Tips & Tricks] Populate Department name based on selected User name

Scenario: 

A particular incident template has 2 additional fields "User name" and "Department name". When users submit a ticket with this specific template, while typing the name ( on the user name additional field), it should populate the appropriate user names list within the ServiceDesk Plus. On selecting a user name, ServiceDesk Plus should automatically populate the selected user's department on the department name field.

Solution: 

  • Create a new 'Single Line' additional fields for "UserField" and "DeptField", add them in a particular request template. 

  • Modify the following script and replace the highlight lines in blue with UDF names of additional fields  "UserField" and "DeptField"

var userField = "WorkOrder_Fields_UDF_CHAR2";
var deptField = "WorkOrder_Fields_UDF_CHAR1";
$CS.referField(userField, "users", {
    ajax: {
        url: "api/v3/users",
        cache: false,
        data: function(params) {
            var searchValue = params;
            var input_data = $CS.toJSONString({
                "list_info": { //NO I18N
                    "row_count": "100", //NO I18N
                    "fields_required": ["name","department"], //NO I18N
                    "search_fields": { //NO I18N
                        "name": searchValue //NO I18N
                    }
                }
            });
            return { "input_data": input_data };
        },
        results: function(res) {
            return {results: res["users"] };
        }
    },
    formatResult: function(object) {
        object.text =object.name;
        object.id = object.name;
        return object.text;
    }
});
$CS.element(userField).on("change", function() {
    var data = jQuery(this).select2("data");
    var dept = "";
    if(data.department != null){
        dept = data.department.name;
    }
    $CS.setValue(deptField, dept);
});   

Note - Steps to find the UDF name of fields "UserField" and "DeptField"

Go to Admin > Incident Templates > Select a particular template > Field and Form Rules > "On Form Load"  > Actions - Execute Script and click on the Write Custom Script option.  Type the name of the field in the "Refer Template Fields" text box and click on the name of the particular additional field, once a match is found.  This will show the Field Label, Field Name, and Type in the right pane. The value provided against 'Field Label' is the UDF name which will be used in the javascript.



Sample: deptField = WorkOrder_Fields_UDF_CHAR1

  • Go to Admin > Incident Templates > Select a particular template > Field and Form Rules > "On Form Load"  > Actions - Execute Script - Copy Paste the javascript and save the rule with a name.











































  • On request submission, type the user name. 


  • The Department name of the selected user name gets auto-populated 


Note: This script works on 11000 and above builds

With ESM (11000) release, requesters can be given access for GET and GET ALL users API operations to achieve certain requirements. This will be disabled by default and needs to be enabled by executing the below query in the database.


update globalconfig set paramvalue='true' where parameter = 'AllowUserAPIInRequesterLogin';


Refer to this Article for the instructions to connect to the database.


Refer to this community post for further information.


Our previous article was on Best Practices for Password Management



                New to ADManager Plus?

                  New to ADSelfService Plus?