- 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-07-2022 06:38 AM
you didn't mention what's requested_for
If that's a reference variable on your catalog form; you can still use onChange script and onSubmit won't be required
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 03:41 PM
thanks Ankur
i will go with the onchange script, as BR we tend to avoid.
question on the below { bracket does it need to be after ga.getXML(alertUser);
getting error message stating function should be at the root
ga.getXML(alertUser);
function alertUser(response) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2022 10:50 PM
if its a reference variable does the script need to change?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2022 12:04 AM
Hi,
update as this and since it's reference no change required
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.getXMLAnswer(function(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-08-2022 07:35 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader