Check role for the user selected on Catalog form and generate a pop-up message if user has itil role

Uttam Sai
Tera Contributor

Hi Team,

 

We have a "requested user" field on Catalog form which is a reference field (sys_user). Could you please help if we can check whether the selected user has itil role and generate a pop-up message "User has itil role" .

 

Thank you

2 ACCEPTED SOLUTIONS

Rajesh Chopade1
Mega Sage

hi @Uttam Sai 

You need to create 'onChange' catalog client script on variable "requested_user":

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

    // GlideAjax call to Script Include to check the user's roles
    var ga = new GlideAjax('UserRoleCheck');
    ga.addParam('sys_id', newValue); // Pass the sys_id of the selected user
    ga.getXMLAnswer(function(response) {
        if (response == 'true') {
            // Display an alert if the user has the ITIL role
            alert('User has ITIL role');
        }
    });
}

 

Script include to check user's role:

var UserRoleCheck = Class.create();
UserRoleCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    checkITILRole: function() {
        var userSysId = this.getParameter('sys_id'); // Get the user sys_id from the GlideAjax call
        var user = new GlideRecord('sys_user');
        user.get(userSysId);
        
        // Check if the user has the ITIL role
        if (gs.hasRole('itil', userSysId)) {
            return 'true';
        } else {
            return 'false';
        }
    }
});

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh

View solution in original post

Collin Romeijn
Kilo Guru

Hi,

 

to achieve this you need a onChange Catalog client script that uses GlideAjax to fetch the needed information form the backend through a new made Script Include.

Example for the Catalog Client Scipt - OnChange field must be the "requested user" field:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return; // Do nothing if the form is loading or the new value is empty
    }

    // Make a GlideAjax call to the Script Include
    var ga = new GlideAjax('RoleChecker'); // Name of the Script Include
    ga.addParam('sysparm_name', 'hasITILRole'); // Call the function inside the Script Include
    ga.addParam('sysparm_user_id', newValue);  // Pass the selected user ID

    ga.getXMLAnswer(function(response) {
        var hasRole = response;
        
        if (hasRole == 'true') {
            g_form.addInfoMessage('The selected user has the ITIL role.');
        } else {
            g_form.addErrorMessage('The selected user does NOT have the ITIL role.');
        }
    });
}

 

 
Then make a new Script Include in this example "RoleChecker" :

var RoleChecker = Class.create();
RoleChecker.prototype = {
    initialize: function() {},
    
    hasITILRole: function(userId) {
        // Check if the user has the 'itil' role
        var hasRole = false;
        var gr = new GlideRecord('sys_user_has_role');
        gr.addQuery('user', userId);
        gr.addQuery('role.name', 'itil'); // Filter for 'itil' role
        gr.query();
        if (gr.next()) {
            hasRole = true;
        }
        return hasRole;
    },
    
    type: 'RoleChecker'
};

 

Hopefully this will guide you in the right direction. Als look at the GlideAjax docs page for further info.: GlideAjax - Client (servicenow.com)

 

Kind Regards,

Collin

View solution in original post

3 REPLIES 3

Rajesh Chopade1
Mega Sage

hi @Uttam Sai 

You need to create 'onChange' catalog client script on variable "requested_user":

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

    // GlideAjax call to Script Include to check the user's roles
    var ga = new GlideAjax('UserRoleCheck');
    ga.addParam('sys_id', newValue); // Pass the sys_id of the selected user
    ga.getXMLAnswer(function(response) {
        if (response == 'true') {
            // Display an alert if the user has the ITIL role
            alert('User has ITIL role');
        }
    });
}

 

Script include to check user's role:

var UserRoleCheck = Class.create();
UserRoleCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    checkITILRole: function() {
        var userSysId = this.getParameter('sys_id'); // Get the user sys_id from the GlideAjax call
        var user = new GlideRecord('sys_user');
        user.get(userSysId);
        
        // Check if the user has the ITIL role
        if (gs.hasRole('itil', userSysId)) {
            return 'true';
        } else {
            return 'false';
        }
    }
});

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh

Collin Romeijn
Kilo Guru

Hi,

 

to achieve this you need a onChange Catalog client script that uses GlideAjax to fetch the needed information form the backend through a new made Script Include.

Example for the Catalog Client Scipt - OnChange field must be the "requested user" field:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return; // Do nothing if the form is loading or the new value is empty
    }

    // Make a GlideAjax call to the Script Include
    var ga = new GlideAjax('RoleChecker'); // Name of the Script Include
    ga.addParam('sysparm_name', 'hasITILRole'); // Call the function inside the Script Include
    ga.addParam('sysparm_user_id', newValue);  // Pass the selected user ID

    ga.getXMLAnswer(function(response) {
        var hasRole = response;
        
        if (hasRole == 'true') {
            g_form.addInfoMessage('The selected user has the ITIL role.');
        } else {
            g_form.addErrorMessage('The selected user does NOT have the ITIL role.');
        }
    });
}

 

 
Then make a new Script Include in this example "RoleChecker" :

var RoleChecker = Class.create();
RoleChecker.prototype = {
    initialize: function() {},
    
    hasITILRole: function(userId) {
        // Check if the user has the 'itil' role
        var hasRole = false;
        var gr = new GlideRecord('sys_user_has_role');
        gr.addQuery('user', userId);
        gr.addQuery('role.name', 'itil'); // Filter for 'itil' role
        gr.query();
        if (gr.next()) {
            hasRole = true;
        }
        return hasRole;
    },
    
    type: 'RoleChecker'
};

 

Hopefully this will guide you in the right direction. Als look at the GlideAjax docs page for further info.: GlideAjax - Client (servicenow.com)

 

Kind Regards,

Collin

Sai_Charan_K
Kilo Sage

Hi @Uttam Sai ,

Please create script include and onchange client script as directed below to achieve this.

Script include : Screenshot is attached, please follow accordingly. 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue != "") {
        var ga = new GlideAjax('global.Test_script_include');
        ga.addParam('sysparm_name', 'getRole');
        ga.addParam('sysparm_sys_id', newValue);
        ga.getXML(CheckRole);

        function CheckRole(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            console.log(answer);
            if (answer == false) {
                alert("User does not have ITIL role");
            }
            else if (answer == true) {
                alert("User has ITIL role");
            }
        }
    }
    //Type appropriate comment here, and begin script below

}

 


Onchange Client script : Screenshot is attached, please follow accordingly.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue != "") {
        var ga = new GlideAjax('global.Test_script_include');
        ga.addParam('sysparm_name', 'getRole');
        ga.addParam('sysparm_sys_id', newValue);
        ga.getXML(CheckRole);

        function CheckRole(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            console.log(answer);
            if (answer == false) {
                alert("User does not have ITIL role");
            }
            else if (answer == true) {
                alert("User has ITIL role");
            }
        }
    }
    //Type appropriate comment here, and begin script below

}

 


Explanation:

By checking the change in the user details from the "requested user" field of your catalog form we are passing the sys_id to the back end to the script include using glide ajax to verify whether the user is having the role or not and return to the form to display the alert message. The alert message screenshot is attached for reference.

Please mark my answer "Helpful" and "Correct" if you feel that it has helped you in any way.

Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India