onSubmit catalog client script using gr.addEncodedQuery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 03:06 PM
Hello, I am a new to ServiceNow and I need some assistance with creating an onSubmit catalog client script. The requirement is that the catalog item should prevent users from requesting the same item more than ten times within ten business days. However, I have encountered some errors where "gr.addEncodedQuery" is not allowed in my script. I would appreciate any help in resolving these issues. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2024 09:57 PM
Using GlideRecord in client script is not recommended approach, you can use script include and can call it from client script, However if you wanna use it, please use below code:
function onSubmit() {
//Prevents a user from being the requested for on this catalog item more than 10 times within 10 business days.
var requestFor = g_form.getValue('who_is_this_request_for');
var catalogItemSysID = "785339fa872842d0477964a90cbb35b7";
// Query to count requests by current user for the current catalog item in the last 10 business days
var gr = new GlideRecord('sc_req_item');
gr.addQuery('requested_for', requestFor);
gr.addQuery('sc_cat_item', catalogItemSysID);
// Construct date range condition using gs.beginningOfLast10BusinessDays() and gs.endOfLast10BusinessDays()
gr.addQuery('opened_at', '>=javascript:gs.beginningOfLast10BusinessDays()');
gr.addQuery('opened_at', '<=javascript:gs.endOfLast10BusinessDays()');
gr.query();
var requestCount = 0;
while (gr.next()) {
requestCount++;
}
// If request count exceeds 10, abort submission and display error message
if (requestCount >= 10) {
g_form.addErrorMessage('You cannot request this item more than 10 times within the last 10 business days.');
return false; // Abort submission
}
return true;
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks