only Manager should be able to submit the catalog item

Riri
Tera Contributor

Hello Everyone,

 

Currently I'm working on a scenario where a Manager can only request for Catalog item and no other person can request.

 

Example: My name is John and my managers name is Mitchell, then in this case only my manager to whom I report should be the only one who can request for this item for me. Suppose there is one more Manager whose name is Lisa and I don't report to her, in that case even if she tries to submit for me, then it shouldn't happen. She should not be able to submit a request for me. 

 

for this I wrote Script Include & Catalog OnSubmit script:

 

SI:

var managerValidation = Class.create();
managerValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    isManager: function (){
        var requestedFor = this.getParameter('sysparm_requested_for');
        var currentUser = this.getParameter('sysparm_currentUser');
        gs.info(requestedFor);
        gs.info(currentUser);

        var userGR = new GlideRecord('sys_user');
        if (userGR.get(requestedFor)){
            var managerSysId = userGR.getValue('manager');
            gs.info(managerSysId);
            if (managerSysId == currentUser){
                return 'true';
            }
            else{
                return 'false';
            }
        }
    },
    type: 'managerValidation'
});
 
CS:
function onSubmit() {

    var answer;
    var requestedFor = g_form.getValue('requested_for');
    var currentUser = g_user.userID;
    alert(requestedFor);
    alert(currentUser);
    var ga = new GlideAjax('managerValidation');
    ga.addParam('sysparm_name', 'isManager');
    ga.addParam('sysparm_requested_for', requestedFor);
    ga.addParam('sysparm_currentUser', currentUser);

    ga.getXML(ajaxCallback);

    function ajaxCallback(response) {
        answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);



        if (answer == 'false') {
            alert('Only manager');
            return false;
        } else {
            alert('Success');
            return true;
        }
        // return true;
    }
}
 
So rn what is happening is it is checking for every alert associated with it but it is allowing other people to submit as well. Ideally it should just abort the action but the item is getting submitted.
8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Riri 

you should always write onChange catalog client script on variable "requested_for" and if validation fails throw error on that variable.

This ensures that form gets submitted with valid details.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Musab Rasheed
Tera Sage
Tera Sage

This is not the best approach, best approach is to make catalog item visible for managers and let them submit on behalf of their reportees, you can create simple user criteria and write below code.

answer = checkIfManager();

function checkIfManager(){
  var gr = new GlideRecord('sys_user');
  gr.addQuery('manager', gs.getUserID());
  gr.query();
  return gr.hasNext();
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

Ankur Bawiskar
Tera Patron
Tera Patron

@Riri 

then show only those users in requested_for users which are direct reportees of the logged in user

use advanced ref qualifier as this in that variable

javascript: 'manager=' +gs.getUserID();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Riri 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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