How to prevent submission of Catalog form for particular users?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
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 -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago - last edited 2 weeks ago
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:
- 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'
});
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
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 .