- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2020 08:44 AM
I have a request where they want to check if the opened requested item exists for the current Requested For. If yes, don't generate another RITM.
I am assuming this is a business rule but I don't know how to execute this request. Any help would be much appreciated
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2020 12:01 PM
If you want to prevent the submission of the request, the below code will work - assuming there is a variable that is populated when submitting on behalf of someone else. When the Order Now or Add to Cart buttons are clicked an alert will pop up if an active RITM for the same requested for and catalog item exists.
onSubmit catalog client script
function onSubmit(){
var url = document.URL.parseQuery();
var itemsysid = decodeURI(url['sysparm_id']); //gets the sysid of this catalog item
var ajax = new GlideAjax('BKBDupeCheck'); //name of your script include
ajax.addParam('sysparm_name', 'checkDupeRITM'); //name of the function in the script include
ajax.addParam('sysparm_user', g_form.getValue('v_requested_for')); //name of the variable to submit on behalf of someone else
ajax.addParam('sysparm_sysid', itemsysid); //pass in the catalog item
ajax.getXMLWait(); // process the server script and wait for a response
var answer = ajax.getAnswer(); // return value from server script
if(answer == 'true'){
alert("There is already and active request of this type for this user");
return false; //prevent submission of the request
}
}
script include - be sure Client callable is checked
var BKBDupeCheck = Class.create();
BKBDupeCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDupeRITM: function () {
var answer = 'false';
var usr = this.getParameter('sysparm_user');
var catitem = this.getParameter('sysparm_sysid');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item', catitem);
gr.addQuery('request.requested_for', usr);
gr.addQuery('active', true);
gr.query();
if(gr.next()){
answer = 'true';
}
return answer;
},
type: 'BKBDupeCheck'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2020 11:41 AM
Hi
i suggest not to use a business rule (which would be possible as well, but I recommend to implement a "run script" workflow Activity as one of the first steps in the workflow of the request.
There I would check if there is another RITM like the one in your current request.
If yes, just drop the request by NOT approving it and set it to be "Rejected" (and End the workflow).
That way, I think you have a good documentation about what happened (you can add some notes in the script / workflow to document what happened, and why).
Maybe you can also implement that script on the workflow of the RITM.
That way, your code runs specificly only to Catalog Items you want to.
Also, this code runs server side and cannot be by-passed.
Let me know, if that answers your question and mark my answer as correct and helpful, please.
BR
Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2020 12:01 PM
If you want to prevent the submission of the request, the below code will work - assuming there is a variable that is populated when submitting on behalf of someone else. When the Order Now or Add to Cart buttons are clicked an alert will pop up if an active RITM for the same requested for and catalog item exists.
onSubmit catalog client script
function onSubmit(){
var url = document.URL.parseQuery();
var itemsysid = decodeURI(url['sysparm_id']); //gets the sysid of this catalog item
var ajax = new GlideAjax('BKBDupeCheck'); //name of your script include
ajax.addParam('sysparm_name', 'checkDupeRITM'); //name of the function in the script include
ajax.addParam('sysparm_user', g_form.getValue('v_requested_for')); //name of the variable to submit on behalf of someone else
ajax.addParam('sysparm_sysid', itemsysid); //pass in the catalog item
ajax.getXMLWait(); // process the server script and wait for a response
var answer = ajax.getAnswer(); // return value from server script
if(answer == 'true'){
alert("There is already and active request of this type for this user");
return false; //prevent submission of the request
}
}
script include - be sure Client callable is checked
var BKBDupeCheck = Class.create();
BKBDupeCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDupeRITM: function () {
var answer = 'false';
var usr = this.getParameter('sysparm_user');
var catitem = this.getParameter('sysparm_sysid');
var gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item', catitem);
gr.addQuery('request.requested_for', usr);
gr.addQuery('active', true);
gr.query();
if(gr.next()){
answer = 'true';
}
return answer;
},
type: 'BKBDupeCheck'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2020 02:08 PM
thank you so much for your help. i mean it. youre a life saver
i have one other question. i havent worked much with ajax but when i try to submit, on any given user i get this error in the console
Uncaught ReferenceError: func is not defined
ive triple checked all the variables and everything looks correct. do you know why this is happening?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2020 05:13 AM
Hi
Please give some honor to good answers by marking as correct, and open a new question for different topics.
So, if Brad answered your question correct, you can make him happy by marking his answer as correct.
Thanks a lot
BR
Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2020 05:15 AM
You're seeing that console error and it is not preventing submission when it should? Do you also see that error if this client script is inactivated? I didn't use 'func' anywhere, so it seems like it would be in another client script, script include called, or business rule, or maybe that's what happens when the name of the script include or function used in the client script doesn't match the script include or function name.