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


1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @Abishek1998 

 

Try with this:

 

1: Create a Script Include

  • Navigate to System Definition > Script Includes > click New.
  • Name it: checkUserJobDetails
  • Check the Client callable box.

 

var checkUserJobDetails = Class.create();

checkUserJobDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getUserDetails: function() {

        var userId = gs.getUserID();

        var result = {

            isManager: false,

            isAccounting: false

        };

 

         var hrGr = new GlideRecord('sn_hr_core_profile');

        hrGr.addQuery('user', userId);

        hrGr.query();

        if (hrGr.next()) {

            result.isManager = hrGr.getValue('is_manager') == 'true';

             var jobGr = hrGr.job_profile.getRefRecord();

            if (jobGr.isValid()) {

                var jobFamilyGroup = jobGr.job_family_groups.getDisplayValue();

                if (jobFamilyGroup == 'Accounting') {

                    result.isAccounting = true;

                }

            }

        }

        return JSON.stringify(result);

    },

    type: 'checkUserJobDetails'

});

 

 

2: Create a Catalog Client Script

  • Navigate to Service Catalog > Catalog Administration > Catalog Client Scripts > click New.
  • Name: Hide Option for Non-Manager Accountants
  • Applies to: A Record Producer
  • Record Producer: Select your specific record producer
  • UI Type: All
  • Type: onLoad

 

function onLoad() {

    var ga = new GlideAjax('checkUserJobDetails');

    ga.addParam('sysparm_name', 'getUserDetails');

    ga.getXMLAnswer(function(response) {

        if (response) {

            var userDetails = JSON.parse(response);

            if (userDetails.isAccounting && !userDetails.isManager) {

                g_form.removeOption('variable_name', 'option_value'); //update variable_name, option_value with your actual value

            }

        }

    });

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

5 REPLIES 5

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