Catalog item field visibility based on the Req_for is belong to a particular department

Mounika30
Kilo Sage

Hi 

I have requirement that

The field should be visible when the req_for is belong to the particular department on the catalog item.

1 REPLY 1

sejal1107
Tera Guru

Hi @Mounika30 

 

You can create script include with client callable true and client script 

 

//script include
GetRequestedForDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Function to get department of the 'Requested For' user
    getDepartment: function() {
        var requestedForSysId = this.getParameter('sys_id'); // Get sys_id from the client-side call
        if (!requestedForSysId) {
            return ''; // Return empty if sys_id is not provided
        }

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(requestedForSysId)) {
            // Get the department name
            var department = userGR.department;
            return department ? department.name : ''; // Return the department name if found
        }

        return ''; // Return empty if user is not found
    }
});
function onLoad() {
    var requestedForSysId = g_form.getValue('req_for');  // Get the sys_id of the requested for user

    if (requestedForSysId) {
        // Create a GlideAjax object to call the Script Include
        var ga = new GlideAjax('GetRequestedForDepartment');  // Name of the Script Include
      ga.addParam('sysparm_name', 'getDepartment');
        ga.addParam('sys_id', requestedForSysId);  // Add parameter to pass sys_id
        ga.getXMLAnswer(function(response) {
            var departmentName = response.responseXML.documentElement.getAttribute('answer');
            
            if (departmentName) {
                 // You can now use the department name in your logic (e.g., show/hide fields)
                if (departmentName === 'IT') {
                    g_form.setVisible('u_special_field', true);  // Show field if department is IT
                } else {
                    g_form.setVisible('u_special_field', false);  // Hide field otherwise
                }
            }
        });
    }
}

 

 Please Mark Helpful and Correct if it really helps you.
Regards,
Sejal Chavan

Please check and Mark Helpful and Correct if it really helped you.
Thanks & Regards
Sejal Chavan