Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script to check for an existing RITM with a particular string, if none existing then create a request.

Michael Lee1
Tera Expert

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!

1 ACCEPTED SOLUTION

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();

View solution in original post

9 REPLIES 9

Elijah Aromola
Mega Sage

Try the following script and insert your modifications as necessary.

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("short_description=insert short desc"); 
// ritm.addEncodedQuery("short_descriptionCONTAINSinsert short desc"); 
// you can also use this if you want records that CONTAIN it
ritm.query();
if (ritm.getRowCount() == 0) {
   var newRitm = new GlideRecord('sc_req_item');
   newRitm.initialize();
   // newRitm.field = value; set your values
   newRitm.insert(); //create ritm
}

Awesome thanks! I will try this. I never know about the getRowCount() method. 

If you're wanting to order an item in the event that the ritm doesn't exist you'd have to use the cart api.

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("short_description=insert short desc"); 
// ritm.addEncodedQuery("short_descriptionCONTAINSinsert short desc"); 
// you can also use this if you want records that CONTAIN it
ritm.query();
if (ritm.getRowCount() == 0) {
   // order item
   var cartId = GlideGuid.generate(null);
   var cart = new Cart(cartId);
   var item = cart.addItem('item sys_id here');
   var rc = cart.placeOrder(); 
}

Please mark this as correct/helpful if it resolved your issue!

I'm aware of that part, thank you. It was just the logic for creating 1 request as opposed to many as it iterated through all the records. One other thing I should've mentioned though. This is a catalog item variable and not a variable that lives on the RITM table directly. Is it possible to add cat item variables to the query conditions of a Glide query? I don't think it is and so this would have to be constructed differently.