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

I meant to say "and not a variable that lives on the RITM table directly." 

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

Okay that works because this is the only cat item with this variable. I will try this. Thanks!

Okay this worked just fine. Thanks again for your help!

Maverick E
Tera Guru

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!