- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2022 08:33 PM
Hi there
i have a requirement to restrict a catalog item to just one time request per user, that is once its requested and state pending or completed it cannot be requested again.
This is requested via the servicenow portal.
the attached script does not seem to work it always comes up with the message when i change user 'duplicate request cannot be submitted'
Thanks
Levino
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('restrictduplicateitem');
ga.addParam('sysparm_name', 'restrictduplication');
ga.addParam('sysparm_reqfor', newValue);
ga.getXML(alertUser);
function alertUser(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.showFieldMsg('requested_for', 'Duplicate request can not be submitted', 'Error');
g_form.setValue('hidden_variable', true);
} else {
g_form.hideFieldMsg('hidden_variable', true);
}
}
}
var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
restrictduplication: function() {
var user = this.getParameter('sysparm_user');
var cat_item_id = this.getParameter('sysparm_catitem');
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + req_for);
gr.query();
if (gr.next())
return true;
else
return false;
},
type: 'restrictduplicateitem'
});
function onSubmit() {
//Type appropriate comment here, and begin script below
if (g_form.getValue('hidden_variable') == 'true') {
return false;
}
}
Thanks
Levino
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2022 12:24 AM
Hi,
you don't require onSubmit
if the validation fails then clear the requested for and show error so that unless user selects correct user form cannot be submitted
try this
var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
restrictduplication: function() {
var user = this.getParameter('sysparm_user');
var cat_item_id = this.getParameter('sysparm_catitem');
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + user);
gr.query();
return gr.hasNext();
},
type: 'restrictduplicateitem'
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('requested_for', true);
if(oldValue != newValue){
var ga = new GlideAjax('restrictduplicateitem');
ga.addParam('sysparm_name', 'restrictduplication');
ga.addParam('sysparm_reqfor', newValue);
ga.getXML(alertUser);
function alertUser(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer.toString() == 'true') {
g_form.clearValue('requested_for', true);
g_form.showFieldMsg('requested_for', 'Duplicate request can not be submitted', 'Error');
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2022 10:06 PM
i have tried this still no luck
var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
restrictduplication: function() {
var user = this.getParameter('sysparm_user');
var cat_item_id = this.getParameter('sysparm_catitem');
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + user);
gr.query();
if (gr.next())
return true;
else
return false;
},
type: 'restrictduplicateitem'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2022 11:23 PM
Hey Levino.
What I will suggest you to check, if your values that you are passing from the client script, if the values are getting passed to the script include are correct or not by putting gs.info statements in Script include for variables
Also, try to check if your addencodedquery whatever you have written try to put same values in the list layout to see if it is giving any result
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2022 12:34 AM
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2022 12:24 AM
Hi,
you don't require onSubmit
if the validation fails then clear the requested for and show error so that unless user selects correct user form cannot be submitted
try this
var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
restrictduplication: function() {
var user = this.getParameter('sysparm_user');
var cat_item_id = this.getParameter('sysparm_catitem');
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + user);
gr.query();
return gr.hasNext();
},
type: 'restrictduplicateitem'
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('requested_for', true);
if(oldValue != newValue){
var ga = new GlideAjax('restrictduplicateitem');
ga.addParam('sysparm_name', 'restrictduplication');
ga.addParam('sysparm_reqfor', newValue);
ga.getXML(alertUser);
function alertUser(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer.toString() == 'true') {
g_form.clearValue('requested_for', true);
g_form.showFieldMsg('requested_for', 'Duplicate request can not be submitted', 'Error');
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2022 04:36 AM
Thanks Ankur
seen Hitoshi comment, is BR the way to go?