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 
2 REPLIES 2

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