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

@abhisek  

You are most welcome!

Glad that the script worked for you.

 

Thank You
Juhi Poddar

Hi @Ankur Bawiskar 

Could you please let me know if any change needs to be done in the script ?

Thanks&Regards,

Abhisek Chattaraj.

@abhisek  

your script include is not client callable

Also setOption() is not a valid function. to set the value you need to use g_form.setValue()

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

Ravi Gaurav
Giga Sage
Giga Sage

Hi @abhisek  ,

 

Please share your script include if you have written so than we can correct. Blindly following chatgpt cut past won't help. I hope you understand

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/