user's managers check

bhanukota09
Tera Contributor

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”

please help to achive this requriemnt 

1 ACCEPTED SOLUTION

vaishali231
Kilo Sage

Hey @bhanukota09 


You can achieve this requirement in a Catalog Item using a combination of:

Client Callable Script Include
Catalog Client Script (onSubmit)

 

Create Client callable Script 
Script

var ValidateRequestedForManager = Class.create();
ValidateRequestedForManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   checkManager: function() {
       var requestedFor = this.getParameter('sysparm_requested_for');
       if (!requestedFor)
           return 'false';
       //  use Manager sys_ids  here
       var allowedManagers = [
           '6816f79cc0a8016401c5a33be04be441',
           '62826bf03710200044e0bfc8bcbe5df1',
           '5c4c13d0dbb12300f1234567899619ab'
       ];
       var userGR = new GlideRecord('sys_user');
       if (userGR.get(requestedFor)) {
           // Check if Requested For user has manager
           if (userGR.manager) {
               var managerSysId = userGR.manager.toString();
               // Validate manager
               if (allowedManagers.indexOf(managerSysId) > -1) {
                   return 'true';
               }
           }
       }
       return 'false';
   }
});


Step 2: Create Catalog Client Script type onSubmit

Script

function onSubmit() {
   // Prevent recursive submission
   if (window.submittingForm)
       return true;
   var requestedFor = g_form.getValue('requested_for');
   if (!requestedFor) {
       g_form.addErrorMessage('Requested For field is mandatory');
       return false;
   }
   var ga = new GlideAjax('ValidateRequestedForManager');
   ga.addParam('sysparm_name', 'checkManager');
   ga.addParam('sysparm_requested_for', requestedFor);
   ga.getXMLAnswer(function(answer) {
       if (answer == 'true') {
           // Allow form submission
           window.submittingForm = true;
           g_form.submit();
       } else {
           // Block submission
           g_form.addErrorMessage(
               'You are not eligible to submit this request, please read the message at top of the form'
           );
       }
   });
   return false;
}

***********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb

 

View solution in original post

2 REPLIES 2

vaishali231
Kilo Sage

Hey @bhanukota09 


You can achieve this requirement in a Catalog Item using a combination of:

Client Callable Script Include
Catalog Client Script (onSubmit)

 

Create Client callable Script 
Script

var ValidateRequestedForManager = Class.create();
ValidateRequestedForManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   checkManager: function() {
       var requestedFor = this.getParameter('sysparm_requested_for');
       if (!requestedFor)
           return 'false';
       //  use Manager sys_ids  here
       var allowedManagers = [
           '6816f79cc0a8016401c5a33be04be441',
           '62826bf03710200044e0bfc8bcbe5df1',
           '5c4c13d0dbb12300f1234567899619ab'
       ];
       var userGR = new GlideRecord('sys_user');
       if (userGR.get(requestedFor)) {
           // Check if Requested For user has manager
           if (userGR.manager) {
               var managerSysId = userGR.manager.toString();
               // Validate manager
               if (allowedManagers.indexOf(managerSysId) > -1) {
                   return 'true';
               }
           }
       }
       return 'false';
   }
});


Step 2: Create Catalog Client Script type onSubmit

Script

function onSubmit() {
   // Prevent recursive submission
   if (window.submittingForm)
       return true;
   var requestedFor = g_form.getValue('requested_for');
   if (!requestedFor) {
       g_form.addErrorMessage('Requested For field is mandatory');
       return false;
   }
   var ga = new GlideAjax('ValidateRequestedForManager');
   ga.addParam('sysparm_name', 'checkManager');
   ga.addParam('sysparm_requested_for', requestedFor);
   ga.getXMLAnswer(function(answer) {
       if (answer == 'true') {
           // Allow form submission
           window.submittingForm = true;
           g_form.submit();
       } else {
           // Block submission
           g_form.addErrorMessage(
               'You are not eligible to submit this request, please read the message at top of the form'
           );
       }
   });
   return false;
}

***********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb

 

Ankur Bawiskar
Tera Patron

@bhanukota09 

this is for catalog item and the item's visibility?

share screenshots of which form you are referring?

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