user's manager check

bhanukota09
Tera Contributor

Hello All 

: For the below requirement, I used the attached script, and it is working fine. However, there is a 10-second delay when submitting the request. While blocking the submission gives a quick response, the validation takes around 10 seconds, and the user needs to click the submit button again.

If ā€˜Requested Forā€ rolls up to the following manager’s: allow the user to continue updating the form.

  • Abel tuter
  • ALexnder
  • Ajay chandra

If not, don’t allow the user to update form and pop-up below error message:

ā€œYou are not eligible to submit this request, please read the message at top of the formā€
Script include 

 

 

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

    checkManager: function() {

        var requestedFor = this.getParameter('sysparm_requested_for');
        if (!requestedFor)
            return 'false';

        // Allowed Manager sys_ids
        var allowedManagers = [
            '80eae25b3b4bae9c8150438e53e45a8c',
            '27afd845dba7b91c77bca8dad3961926'
        ];

        var userGR = new GlideRecord('sys_user');

        if (userGR.get(requestedFor)) {

            var manager = userGR.manager;

            while (manager) {

                var managerID = manager.toString();
                if (allowedManagers.indexOf(managerID) > -1) {
                    return 'true';
                }
                var mgrGr = new GlideRecord('sys_user');
                if (mgrGr.get(managerID)) {
                    manager = mgrGr.manager;
                } else {
                    break;
                }
            }
        }

        return 'false';
    }
});
 
Client script 

function onSubmit() {

    // Prevent recursive or duplicate submissions
    if (window.submittingForm)
        return true;

    var requestedFor = g_form.getValue('requested_for1');

    if (!requestedFor) {
        g_form.addErrorMessage('Requested For field is mandatory');
        return false;
    }

    // Optional: Disable submit to prevent multiple clicks
   // g_form.addInfoMessage('Validating manager hierarchy, please wait...');

    var ga = new GlideAjax('ValidateVPChain');
    ga.addParam('sysparm_name', 'checkManager');
    ga.addParam('sysparm_requested_for', requestedFor);

    ga.getXMLAnswer(function(answer) {

        if (answer === 'true') {
            window.submittingForm = true;
          //  g_form.submit();

        } else {
            g_form.addErrorMessage(
                'You are not eligible to submit this request. Please read the message at the top of the form.'
            );
        }
    });

    return false;
}

 

please validate script and provide the suggestions please 
1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @bhanukota09 

 

1. Just try with Onchange Client script:

 

  • Type: onChange
  • Variable: requested_for
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') return;
    var ga = new GlideAjax('ValidateRequestedForManager');
    ga.addParam('sysparm_name', 'checkManager');
    ga.addParam('sysparm_req_for', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer !== 'true') {
            alert("You are not eligible to submit this request, please read the message at top of the form");
            g_form.clearValue('requested_for');
        }
    });
}

2.  Create a script include

  • Navigate to System Definition > Script Includes and click New.
  • Name: ValidateRequestedForManager
  • Client callable: Check the box
var ValidateRequestedForManager = Class.create();
ValidateRequestedForManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkManager: function() {
        var reqFor = this.getParameter('sysparm_req_for');
        if (!reqFor) return 'false';
          var allowedManagers = ['<sys_id_1>', '<sys_id_2>', '<sys_id_3>'];  // Replace with actual sys_ids for Abel, Alexander, Ajay
        var user = new GlideRecord('sys_user');
        if (user.get(reqFor) && allowedManagers.indexOf(user.manager.toString()) > -1) {
            return 'true';
        }
        return 'false';
    },
    type: 'ValidateRequestedForManager'});
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

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@bhanukota09 

why not do this validation using onChange and show error message near that field/variable?

Don't use onSubmit for this

šŸ’” If my response helped, please mark it as correct āœ… and close the thread šŸ”’ā€” this helps future readers find the solution faster! šŸ™

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

My field in the variable set, suppose I'm using on change, is unable to select a variable, right? 

 

There is no error showing,just take time for submission. It's checking the manager hierarchy. I need an immediate response

Tanushree Maiti
Tera Patron

Hi @bhanukota09 

 

1. Just try with Onchange Client script:

 

  • Type: onChange
  • Variable: requested_for
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') return;
    var ga = new GlideAjax('ValidateRequestedForManager');
    ga.addParam('sysparm_name', 'checkManager');
    ga.addParam('sysparm_req_for', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer !== 'true') {
            alert("You are not eligible to submit this request, please read the message at top of the form");
            g_form.clearValue('requested_for');
        }
    });
}

2.  Create a script include

  • Navigate to System Definition > Script Includes and click New.
  • Name: ValidateRequestedForManager
  • Client callable: Check the box
var ValidateRequestedForManager = Class.create();
ValidateRequestedForManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkManager: function() {
        var reqFor = this.getParameter('sysparm_req_for');
        if (!reqFor) return 'false';
          var allowedManagers = ['<sys_id_1>', '<sys_id_2>', '<sys_id_3>'];  // Replace with actual sys_ids for Abel, Alexander, Ajay
        var user = new GlideRecord('sys_user');
        if (user.get(reqFor) && allowedManagers.indexOf(user.manager.toString()) > -1) {
            return 'true';
        }
        return 'false';
    },
    type: 'ValidateRequestedForManager'});
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