Hide field in catalog item

shaik17
Tera Contributor

Hi all,

i have a requirement where i want to hide a field in catalog item, when SMS opt-in field is check in user's profile.

i added a function in existing script include to fetch the userID and a onload client script as per my client requirement, But i it is still showing the field. Please help me on this.

Script include:

    verifySMSOptIn: function() {
        var userId = this.getParameter('user_Id'); // Get the 'requested_for' user ID
        if (!userId) {
            return false;
        }

        var userGR = new GlideRecord('sys_user');
        if (userGR.get('sys_id', userId)) {  // Query using 'sys_id' to match the 'requested_for' field
            return userGR.u_sms_opt_in == true; // Check if 'u_sms_opt_in' is true
        }
        return false;
    },

 

Onload client script:

 

function onLoad() {
   // var userId = g_form.getValue('requested_for');
    var userId = g_user.userID;
    if (userId) {
        var ga = new GlideAjax('ePlusCatalogUtils');
        ga.addParam('sysparm_name', 'verifySMSOptIn'); 
        ga.addParam('sysparam_userID', userId); 
        ga.getXMLAnswer(function(response) {
            var smsOptIn = response; 

            if (smsOptIn === 'true') {
                g_form.setDisplay('send_to_code', true); 
            } else {
                g_form.setDisplay('send_to_code', false); 
            }
        });
    } else {
        g_form.setDisplay('send_to_code', false); // If no user, hide the field by default
    }
}

 

i have added screen shots of catalog item below:

shaik17_0-1726835118260.png

SMS opt-in field in users profile:

shaik17_1-1726835167376.png

@Ankur Bawiskar  @Community Alums 

2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @shaik17 

 

did you try with Catalog UI policy? and how this user is coming on catalog? is the requestor? you can do dot walk.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Brad Bowman
Kilo Patron
Kilo Patron

For starters, when you retrieve the parameter passed in from the client, you need to use the same name, and your return is not valid.  To prevent confusion, make the returns a string as that is what is needed to pass back to the client.

verifySMSOptIn: function() {
    var userId = this.getParameter('sysparm_userID'); // Get the 'requested_for' user ID
    if (userId) {
        var userGR = new GlideRecord('sys_user');
        if (userGR.get('sys_id', userId)) {  // Query using 'sys_id' to match the 'requested_for' field
            if (userGR.u_sms_opt_in == true) { // Check if 'u_sms_opt_in' is true
                return 'true';        
            }
        }
    } 
    return 'false';
},

Beyond this, ensure the Script Include is Client callable, then add some logs to confirm it is running, a record retrieved in the GlideRecord, etc