- 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 08:50 PM
Hey,
I don't see you passing catalog item from client script to script include.
Could you include that?
Also, can you confirm, on which field you have written this onChange script?
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 09:42 PM
Hi Aman
i will add this line
ga.addParam('sysparm_catitem', g_form.getUniqueValue());
onchange 'requested_for' variable
Thanks
Levino
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 09:44 PM
i think i need to do this as well
ga.addParam('sysparm_user', g_form.getValue('requested_for'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 10:05 PM
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('restrictduplicateitem');
ga.addParam('sysparm_name', 'restrictduplication');
ga.addParam('sysparm_user', g_form.getValue('requested_for'));
ga.addParam('sysparm_catitem', g_form.getUniqueValue());
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);
}
}
}