Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Not able to Hide Custom attachment type field in service portal

mpriyman
Tera Contributor

i am configuring the single form for both employees and managers . variables are shown based on the is manager field in the sys_user table , we have a field u_is_manager if it is true means user is manager if it is false means user is the employee . onload  i want to show/hide fields based on the u_is-manager field . custom attachment type field i am not able to hide them in the form using the g_form.setDisplay if the u_is_manager is false .

 

 function onLoad() {
    var openedFor = g_form.getValue('opened_for');
    console.log('[onLoad] opened_for:', openedFor);
 
    if (!openedFor) {
        console.log('[onLoad] No opened_for selected, exiting.');
        return;
    }
 
    var ga = new GlideAjax('GetUserDetailsAjaxhr');
    ga.addParam('sysparm_name', 'getInfo');
    ga.addParam('sysparm_user_id', openedFor);
    ga.getXMLAnswer(function(answer) {
        var user;
        try {
            user = JSON.parse(answer);
        } catch (e) {
            console.error('[onLoad] JSON parse error:', e);
            return;
        }
        console.log('[onLoad] is_manager:', user.is_manager);
 
        if (user.is_manager === 'true') {
            // Manager scenario: show only 3 manager options, make editable
            g_form.setReadOnly('u_request_type', false);
            g_form.clearOptions('u_request_type');
g_form.addOption('u_request_type', '', '-- None --');
            g_form.addOption('u_request_type', 'New', 'New Position to Be Created (Priority High)');
            g_form.addOption('u_request_type', 'Vacant', 'Vacant Existing Job Review (Priority High)');
            g_form.addOption('u_request_type', 'existing', 'Existing Encumbered (Priority Medium)');
            g_form.setValue('u_request_type', '');
 
            // Hide employee-only fields
            [
                'employee_group',
                'why_are_you_making_this_request',
                'job_classification',
                'share_any_jobs',
                'include_any_additional_details'
            ].forEach(function(f) {
                g_form.setDisplay(f, false);
                console.log('[onLoad] Hiding employee-only field:', f);
            });
 
            // Show manager-specific fields
            [
                'organizational_chart_attachment',
                'job_description_attachment'
            ].forEach(function(f) {
                g_form.setDisplay(f, true);
                console.log('[onLoad] Showing manager-specific field:', f);
            });
 
        } else {
            // Employee scenario: only 1 option, read-only
            g_form.clearOptions('u_request_type');
            g_form.addOption('u_request_type', 'unionized_employee', 'Unionized Employee Initiated Review (Priority Medium)');
            g_form.setValue('u_request_type', 'unionized_employee');
            g_form.setReadOnly('u_request_type', true);
 
            // Hide manager-specific fields
            [
                'organizational_chart_attachment',
                'job_description_attachment'
            ].forEach(function(f) {
                g_form.setDisplay(f, false);
                console.log('[onLoad] Hiding manager-specific field:', f);
            });
 
            // Show employee-only fields
            [
                'employee_group',
                'why_are_you_making_this_request',
                'job_classification',
                'share_any_jobs',
                'include_any_additional_details'
            ].forEach(function(f) {
                g_form.setDisplay(f, true);
                console.log('[onLoad] Showing employee-only field:', f);
            });
        }
    });
}  script include code :
var GetUserDetailsAjaxhr = Class.create();
GetUserDetailsAjaxhr.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getInfo: function() {
        var obj = {};
        obj.is_manager = 'false'; // default

        var id = this.getParameter('sysparm_user_id');
        gs.info('[GetUserDetailsAjaxhr] Called with sysparm_user_id: ' + id);

        var gr = new GlideRecord('sys_user');
        if (gr.get(id)) {
            obj.is_manager = gr.u_is_manager == true ? 'true' : 'false';
            gs.info('[GetUserDetailsAjaxhr] User found. u_is_manager = ' + gr.u_is_manager + ' (will return: ' + obj.is_manager + ')');
        } else {
            gs.info('[GetUserDetailsAjaxhr] No user found for sys_id: ' + id);
        }
        gs.info('[GetUserDetailsAjaxhr] Returning: ' + JSON.stringify(obj));
        return JSON.stringify(obj);
    },
    type: 'GetUserDetailsAjaxhr'
});
0 REPLIES 0