Show Variable choices based on logged in user's business unit

si21
Tera Guru

Hi experts,

We have requirement to show variable (select box) choices based on logged in user's business unit. Business unit is a string field on user table

ex: We have a variable with a,b,c,d as choices. If user's business unit is 'US' ,display all the 4 choices else show only a and b choices. How can we achieve this?

TIA.

1 REPLY 1

Vishal Birajdar
Giga Sage

Hi @si21 

 

You can write script include to check logged in users Business unit & return that BU to client script & then show/hide option in client script

 

Step 1 : Create client callable script include

Name - userUtils

Function name : getUserDetails

 

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


    getUserDetails: function() {

        /*1. Declare thevariable to return the value to client script */
        var result;

        /*2. Get the user from client script */
        var user = this.getParameter('sysparm_user');

        /*3. Glide record on User table to get values */

        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', user);
        grUser.query();

        if (grUser.next()) {
            /*4. If user present then store Buisness unit values in result */
              result = grUser.getValue("business_unit");   //use your backend value
           
        }

		/*5. return result*/
        return result;
    },

    type: 'userUtils'
});

 

 

Step 2 : onLoad client script

 

function onLoad() {
 /*1. get logged in user */  
    var user = g_user.userID;
/*2. Call glideajax script include */
    var ga = new GlideAjax('userUtils');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_user', user);

    ga.getXMLAnswer(callBackFun);

    function callBackFun(answer) {
        //here you will get the result from script include

        if (answer == 'US') {
            //You can add choices or remove choices depending upon your needlike this
            g_form.addOption('variable_name', 'a', 'a');
            g_form.addOption('variable_name', 'b', 'b');

        }else (answer == 'UK'){
            g_form.removeOption('variable_name','a');
		}  
   //& so on
    }


}

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates