SanjivMeher
Kilo Patron
Kilo Patron

As always, I would like to provide solutions to the most common use cases we work on in our projects. This time it is Service Catalog Use cases.

Use Case 1: Auto-populate Requested For variable
Use Case 2: Auto-populate Requested For's details
Use Case 3: Using reference qualifiers
Use Case 4: Using advance reference qualifier with script include
Use Case 5: Using advance reference qualifier in a Lookup Select Box

 

Please mark the blog helpful, if you find it helpful.

 

Use Case 1: Auto-populate Requested For variable

The most common use case we get is auto-populate the requested for field with current logged in user.

You can do that my setting the default value of the variable to javascript:gs.getUserID();

find_real_file.png

Use Case 2: Auto-populate Requested For's details

Now there could be cases, where based on selection of the user, you want to auto-populate the user's details, such as manager, email id, company etc. You can utilize this example in other scenarios as well where you have a reference variable and you would like to auto-populate additional information about that reference based on selection of the reference variable.

 

In this example, I have 3 variables on the request form, which is related to Requested for.

-Email is a string variable.

-Manager is a reference variable to sys_user table, since manager is a user record.

-Department is a reference variable to cmn_department table

 

I would like to auto-populate these field on selection of Requested For.

find_real_file.png

Client Script:

I will need an on-Change client script to run when the requested_for changes. This script calls a script include using glide-ajax method. The reason for using GlideAjax is, it is asynchronous and doesn't impact the UI performance. You shouldn't use GlideRecord in client script since it is not a best practice and could impact performance of the UI.

Type: onChange

Field: requested_for

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

    var ga = new GlideAjax('SCUserUtil');
    ga.addParam('sysparm_name', 'getDetails');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXML(responseParse);

    function responseParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		var res = JSON.parse(answer);
        g_form.setValue('email', res.email);
		g_form.setValue('manager', res.manager);
		g_form.setValue('department', res.department);
    }
}

 

Script Include:

Name: SCUserUtil

Client Callable: True

 

var SCUserUtil = Class.create();
SCUserUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var user_sys_id = this.getParameter('sysparm_user_id');
        var user = new GlideRecord('sys_user');

        var result = {
            email: "",
            manager: "",
            department: ""
        };

        if (user.get(user_sys_id)) {
            result.email = user.email.toString();
            result.manager = user.manager.toString();
            result.department = user.department.toString();
        }

        return JSON.stringify(result);

    },
    type: 'SCUserUtil'
});

 

And the result is, on selection of Requested for, Email, Manager and Department are auto-populated.

find_real_file.png

 

Use Case 3: Using reference qualifiers

You can use advance reference qualifier to add a filter to a Lookup Select Box or a Reference field. There are different types of reference qualifiers such as

Simple

Advance

An example for a simple reference qualifier is 'Show only users from company ACME South America'

find_real_file.png

 

An example for an advance reference qualifier is 'Show users based on another variable on the catalog form'. In below screenshot, I want to show all users based on a variable on the form called 'manager'. 

find_real_file.png

Use Case 4: Using advance reference qualifier with script include

You can also use a script include in advance reference qualifier to build a filter.

For ex, here I want to pull a list of groups, the requested for is part of.

 

So I will create a Reference variable 'group' with reference to Group Table with below reference qualifier. Here I am calling a script include 'SCUserUtil' and passing the 'requested_for' as parameter to the function 'getGroups'

javascript:'sys_idIN'+new global.SCUserUtil().getGroups(current.variables.requested_for)

 

Below is the script include which I have used Use Case 2 as well. I just added another function to it.

Name: SCUserUtil

Client Callable: True

var SCUserUtil = Class.create();
SCUserUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var user_sys_id = this.getParameter('sysparm_user_id');
        var user = new GlideRecord('sys_user');

        var result = {
            email: "",
            manager: "",
            department: ""
        };

        if (user.get(user_sys_id)) {
            result.email = user.email.toString();
            result.manager = user.manager.toString();
            result.department = user.department.toString();
        }

        return JSON.stringify(result);

    },
	
	getGroups: function(user_id) {
		var grpList = [];
		var grp = new GlideRecord('sys_user_grmember');
		grp.addQuery('user',user_id);
		grp.query();
		
		while(grp.next())
		{
			grpList.push(grp.getValue('group'));
		}
		return grpList.toString();
	},
    type: 'SCUserUtil'
});

 

Use Case 5: Using advance reference qualifier in a Lookup Select Box

The above filter in use case 4 works fine when the Group variable is a Reference type.

But when using a Lookup Select Box, the select box doesn't refresh with new values automatically. You need to add a variable attribute to refresh it automatically.

The variable attribute should be as below. You need to assign the variable name to the ref_qual_elements, based on which the Lookup Select box should refresh. For ex, here I want the Group variable list to refresh its list based on 'requested_for' variable.

ref_qual_elements=requested_for

find_real_file.png

Let me know, if you think any other use case can be added which could be helpful to everyone.

2 Comments