service catalog

abhisek
Tera Contributor

I have 3 catalog variables 'select ', 'What would you like' and 'do you already have'. If the value of 'select ' is abc and the value of 'do you already have' is no and the person raising the request is a member of the group xyz then the option 'Edit table' under the catalog variable 'What would you like' should be shown. 'What would you like' has some other values as well like 'create table' and 'delete table'. 

I have written a script include and on change catalog client script but it is not working.

Can you please help me out.

1 ACCEPTED SOLUTION

Hello @abhisek  

Here is the updated Client Script:

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

    // Get values of catalog variables
    var platform = g_form.getValue('select');
    var accountStatus = g_form.getValue('do_you_already_have');

    if (platform === 'abc' && accountStatus === 'no') {
        var ga = new GlideAjax('CheckUserGroup'); // Call the Script Include
        ga.addParam('sysparm_name', 'checkMembership'); // Specify the method to call

        ga.getXMLAnswer(function (response) {
            // Directly handle the response
            if (response === 'true') {
                g_form.removeOption('what_would_you_like', 'Edit table'); // Remove if it exists
                g_form.addOption('what_would_you_like', 'Edit table', 'Edit table', 2); // Add option
            } else {
                g_form.removeOption('what_would_you_like', 'Edit table'); // Remove option
            }
        });
    } else {
        // Remove the option if conditions aren't met
        g_form.removeOption('what_would_you_like', 'Edit table');
    }
}

Script Include:

var CheckUserGroup = Class.create();
CheckUserGroup .prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkMembership: function () {
        var groupSysId = '12343'; // Replace with your actual group sys_id
        var userSysId = gs.getUserID(); // Get the current user's Sys ID directly

        var userGr = new GlideRecord('sys_user_grmember');
        userGr.addQuery('user', userSysId);
        userGr.addQuery('group', groupSysId);
        userGr.query();

        return userGr.hasNext() ? 'true' : 'false'; // Return as a string
    },

    type: 'CheckUserGroup'
)};

Note: Script include should be client callable. If a Script include is created without making it client callable then a fresh new script include needs to be created, marking it as client callable.

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@abhisek  

please share your script include and client script here

Also are you sure your script include is client callable?

are you using correct choice values to check?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

client callable script include:

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = {
initialize: function() {
},


checkMembership: function(userSysId) {
var user = new GlideRecord('sys_user');
if (user.get(userSysId)) {
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id','12343');
group.query();
if (group.next()) {
return user.isMemberOf(group); 
}
}
return false; 
},


type: 'CheckUserGroup'
};

 

On change catalog client script where variable name is select:

 

function onChange(control, oldValue, newValue, isLoading) {
    
    if (isLoading || newValue == '') {
return;
}
    
    var platform = g_form.getValue('select');
    var accountStatus = g_form.getValue('do_you_already_have');
 
 
    if (platform === 'abc' && accountStatus === 'no') {
        
        var ga = new GlideAjax('CheckUserGroup');
        ga.addParam('sys_id', g_user.sys_id);
        ga.getXMLAnswer(function(response) {
            var isMember = response.responseXML.documentElement.getAttribute("answer");
            
          
            if (isMember === 'true') {
              
                g_form.setOption('what_would_you_like', 'Edit table', true); 
            } else {
                
                g_form.setOption('what_would_you_like', 'Edit table', false); 
            }
        });
    } else {
       
        g_form.setOption('what_would_you_like', 'Edit table', false); 
    }
}

 

Hello @abhisek  

Here is the updated Client Script:

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

    // Get values of catalog variables
    var platform = g_form.getValue('select');
    var accountStatus = g_form.getValue('do_you_already_have');

    if (platform === 'abc' && accountStatus === 'no') {
        var ga = new GlideAjax('CheckUserGroup'); // Call the Script Include
        ga.addParam('sysparm_name', 'checkMembership'); // Specify the method to call

        ga.getXMLAnswer(function (response) {
            // Directly handle the response
            if (response === 'true') {
                g_form.removeOption('what_would_you_like', 'Edit table'); // Remove if it exists
                g_form.addOption('what_would_you_like', 'Edit table', 'Edit table', 2); // Add option
            } else {
                g_form.removeOption('what_would_you_like', 'Edit table'); // Remove option
            }
        });
    } else {
        // Remove the option if conditions aren't met
        g_form.removeOption('what_would_you_like', 'Edit table');
    }
}

Script Include:

var CheckUserGroup = Class.create();
CheckUserGroup .prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkMembership: function () {
        var groupSysId = '12343'; // Replace with your actual group sys_id
        var userSysId = gs.getUserID(); // Get the current user's Sys ID directly

        var userGr = new GlideRecord('sys_user_grmember');
        userGr.addQuery('user', userSysId);
        userGr.addQuery('group', groupSysId);
        userGr.query();

        return userGr.hasNext() ? 'true' : 'false'; // Return as a string
    },

    type: 'CheckUserGroup'
)};

Note: Script include should be client callable. If a Script include is created without making it client callable then a fresh new script include needs to be created, marking it as client callable.

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Hi @Juhi Poddar 

Thanks a lot for your help and time.

It is working now. just had to update 1 line of your client script:

g_form.addOption('what_would_you_like', 'edit_table', 'edit table');

 

Regards,

Abhisek Chattaraj.