remove option only in choice field based on logged in user

Abishek1998
Tera Contributor

Hi all,

My requirement is if the logged in users Hr profile's isManager is false and the job profile.job family groups(Reference field) is Accounting. then I want to remove an option in the record producer. I tried but I am getting no where


4 REPLIES 4

Kieran Anson
Kilo Patron

What have you tried so far?

This is my client script 

function onLoad() {
    var ga = new GlideAjax('sn_hr_sp.checkHR');
    ga.addParam('sysparm_name', 'isManagerOfAnyUser');
    ga.getXMLAnswer(getAnswer);

    function getAnswer(response) {
        alert('Response: ' + response);
        if (response == 'true') {
            g_form.removeOption('u_what_is_your_payroll_inquiry_about', 'payroll_report_request');
        }
    }
}
and My script Include

var checkHR = Class.create();
checkHR.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {



    isManagerOfAnyUser: function() {
        var currentUserId = gs.getUserID();

        var hrProfileGR = new GlideRecord('sn_hr_core_profile');
        hrProfileGR.addQuery('user', currentUserId);
        hrProfileGR.query();

        if (hrProfileGR.next()) {
            var isManager = hrProfileGR.getValue('u_is_manager') == '1';

            var jobProfileId = hrProfileGR.getValue('u_job_profile');
            if (jobProfileId) {
                var jobProfileGR = new GlideRecord('sn_hr_core_job_profile');
                if (jobProfileGR.get(jobProfileId)) {
                    var jobFamilyGroup = jobProfileGR.getDisplayValue('u_job_family_group');
                    var allowed = ['Human Resources', 'Finance & Accounting'];
                    var jobFamilyAllowed = allowed.indexOf(jobFamilyGroup) !== -1;

                    // Remove only if NOT manager AND NOT allowed job family
                    if (!isManager && !jobFamilyAllowed) {
                        return 'true'; // remove option
                    }
                }
            }
        }

        return 'false'; // keep option
    },

    type: 'checkHR'
});

I am getting the response as null But if i replace this function in a different script include, Then it is working fine. 

Can you check if for your script include client callable is true or not

https://www.servicenow.com/community/developer-advocate-blog/client-callable-and-server-callable-scr...

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Aanchal