- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 11:04 AM
Can someone please help me with the logic for a script that will loop through the RITM table and check the records for any that exist for example with a short description variable of "insert short description here" and if it one exists, the script should do nothing, but if one does not exist it should create 1 request?
I don't need the entire script from you. I already know how to script ordering a catalog item and I already know how to script a GlideRecord query. I just can't get the loop down correctly so that it doesn't order an item for every RITM it finds that does not have the particular string. I just need it to create 1 request if there is no existing RITM with the string value after it loops through them all.
Thanks!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 11:28 AM
You can but it will find other RITMS that have that same variable name. For instance:
var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("variable_name=value"); //this works but will check against ANY RITMS have have this variable name
ritm.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 11:25 AM
I meant to say "and not a variable that lives on the RITM table directly."

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 11:28 AM
You can but it will find other RITMS that have that same variable name. For instance:
var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("variable_name=value"); //this works but will check against ANY RITMS have have this variable name
ritm.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 11:38 AM
Okay that works because this is the only cat item with this variable. I will try this. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 01:56 PM
Okay this worked just fine. Thanks again for your help!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2019 11:48 AM
Hello Michael Lee,
As per your description, I've written this untested snippet for you:
This code is only IF the "short description" is an actual variable on the catalog item.
var rg = new GlideRecord('sc_req_item');
rg.addActiveQuery();
rg.query();
var count = 0;
while (rg.next()) {
//change stringVariable to your variable name
var string = rg.variables.stringVariable;
//check if the string exists
if (string.indexOf("insert string here") > -1) {
return;
} else {
//if RITM does not have the string, add 1 to count
count++;
}
}
if (count > 0) {
gs.log("string not found for " + count + " item(s). creating item..");
//use cart API
var cart = new cart();
//put the sys_id of the catalog item here
var item = cart.addItem('sys_id');
//copy & paste this line for all the variables you want to set in the catalog item
cart.setVariable(item, 'variableName', 'value you want to set');
//submit catalog item
var rc = cart.placeOrder();
}
P.S. I just noticed Elijah beat me to it. Just wanted to share my version of this. I also didn't know you could query variables. cool!