How to prevent submission of Catalog form for particular users?

KS86
Tera Contributor

Hi all,

 

I have 'Requested For' field on catalog form. If Requested for user is belongs to 'HR' department, I need to prevent submission of catalog form. I am using the below script. But it is not submitting form for Non HR users on portal side, but it is working in native side. Let me know what I need to fix in this script to work in both native and portal side? 

 

Client Script - 

 

var validationComplete = false;

function onSubmit() {

    if (validationComplete) {
        return true;
    }

    var gaUser = new GlideAjax('getDepartment');
    gaUser.addParam('sysparm_name', 'getUserDepartment');
    gaUser.addParam('sysparm_user', g_form.getValue('requested_for'));

    gaUser.getXMLAnswer(function(response) {

        if (response == 'false') { // User is NOT HR
            validationComplete = true;
            g_form.orderNow();
        } else { // User is HR
            g_form.addErrorMessage('Catalog submission is not allowed for HR users.');
        }

    });

    return false;
}
 
Script include -
var getDepartment = Class.create();
getDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDepartment: function() {
        var user = this.getParameter('sysparm_user');
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', user);
        grUser.query();

        if (grUser.next()) {
            if (grUser.department.name == "HR") {
                return true;
            } else return false;
        }
    },

    type: 'getDepartment'
});
 

 

 

7 REPLIES 7

KS86
Tera Contributor

If I use g_form.submit(), I am getting this error 'Error MessageThe g_form.submit function has no meaning on a catlog item. Perhaps you mean g_form.addToCart() or g_form.orderNow() instead' in native view. 

 

In portal form is not submitting only(It has to submit Non HR users)

Tanushree Maiti
Tera Patron

Hi @KS86 

 

Using g_form.OrderNow() directly inside an onSubmit()Catalog Client Script causes an infinite loop because the method triggers the submission process all over again.

 

Try This:

 

  1. Create a script onSubmit Catalog Client script

 

  • Navigate to Service Catalog > Catalog Policy > Catalog Client Scripts > click New.
  • Name: Prevent HR Submission
  • UI Type: All
  • Type: onSubmit
  • Applies on: Check both A Catalog Item and Requested For

 

function onSubmit() {

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

        if (!requestedFor) {

        requestedFor = g_user.userID;

    }

    var dept = new GlideAjax('CheckDepartment');

    dept.addParam('sysparm_name', 'checkHRDept');

    dept.addParam('sysparm_user_id', requestedFor);

     dept.getXMLWait();   

    var isHR = dept.getAnswer();

    if (isHR == 'true') {

        alert('Catalog submission is not allowed for HR users.');

        return false;

    }

}

 

2. Create the Script Include

Navigate to System Definition > Script Includes > click New.

Name: CheckDepartment

Client callable: true

 

var CheckDepartment = Class.create();

CheckDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkHRDept: function() {

        var userID = this.getParameter('sysparm_user_id');

        var userGr = new GlideRecord('sys_user');

            if (userGr.get(userID)) {

             var dept = userGr.getDisplayValue('department');

            if (dept == 'HR' ) {

                return 'true';

            }

        }

        return 'false';

    },

    type: 'CheckDepartment'

});

 

 

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

Hi Tanushree, using getXMLWait() in client script is not a good practice. It might issues on portal side.

But I have tried this also, It is not aborting the form submission on both cases on portal .