How to create multiple test RITM records

Natahlie Nativi
Tera Contributor

I need to create 200+ RITM to test a functionality of our portal. 
What is the best way to do it? 
I think using a script might be a bit hard since we need to set values for the variables. 

1 ACCEPTED SOLUTION

@Natahlie Nativi 

the user who is running the script i.e. Admin will be populated in requested_for field

You can update the requested_for field on REQ as well

try {
    var count = 5;
    //user sysID
    var arr = ['62826bf03710200044e0bfc8bcbe5df1', 'a8f98bb0eb32010045e1a5115206fe3a', '0a826bf03710200044e0bfc8bcbe5d7a', '22826bf03710200044e0bfc8bcbe5dec', 'a2826bf03710200044e0bfc8bcbe5ded'];
    for (var i = 0; i < count; i++) {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        //add your requested item to the cart by sys_id of the catalog item
        var item = cart.addItem('c7eb778f97d2415021983d1e6253af7b', 1);

        //fill in the variables on the request item form
        // asset is name of variable and type is reference so give sysId
        cart.setVariable(item, "select_group_which_needs_to_be_cloned", "d625dccec0a8016700a222a0f7900d06");
        cart.setVariable(item, "business_justification", "test");
        var rc = cart.placeOrder();
        gs.info(rc.number);

        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("request.number", rc.number.toString());
        gr.query();
        if (gr.next()) {
            gr.requested_for = arr[count];
            gr.update();
                        // update REQ record as well
			var req = gr.request.getRefRecord();
			req.requested_for = arr[count];
			req.update();
        }
    }

} catch (ex) {
    gs.info(ex);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

17 REPLIES 17

Ankur Bawiskar
Tera Patron
Tera Patron

@Natahlie Nativi 

you can use this sample script in background or fix script for 5 and then verify if it works fine and then repeat it for 200 times

try {
    var count = 5;
    for (var i = 0; i < count; i++) {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        //add your requested item to the cart by sys_id of the catalog item
        var item = cart.addItem('0336c34407d0d010540bf2508c1ed096', 1);

        //fill in the variables on the request item form
        // asset is name of variable and type is reference so give sysId
        cart.setVariable(item, "asset", "00a96c0d3790200044e0bfc8bcbe5dc3");
        var rc = cart.placeOrder();
        gs.info(rc.number);
    }

} catch (ex) {
    gs.info(ex);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 
Can we add a line here to set the requested_for to different users? 

@Natahlie Nativi 

requested_for is a variable within your catalog item?

If variable then I already gave syntax how to set variable value similar to asset, it's just that you will have to store those user sysIds in array and use it like this

try {
    var count = 5;
	var arr = ['sysId1','sysId2','sysId3','sysId4','sysId5'];
    for (var i = 0; i < count; i++) {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        //add your requested item to the cart by sys_id of the catalog item
        var item = cart.addItem('0336c34407d0d010540bf2508c1ed096', 1);

        //fill in the variables on the request item form
        // asset is name of variable and type is reference so give sysId
        cart.setVariable(item, "asset", arr[count]);
        var rc = cart.placeOrder();
        gs.info(rc.number);
    }

} catch (ex) {
    gs.info(ex);
}

Or you are talking about requested_for field on RITM table?

If yes then you can get the REQ number from rc.number. query sc_req_item with this REQ number and update it

try {
    var count = 5;
    var arr = ['sysId1', 'sysId2', 'sysId3', 'sysId4', 'sysId5'];
    for (var i = 0; i < count; i++) {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        //add your requested item to the cart by sys_id of the catalog item
        var item = cart.addItem('0336c34407d0d010540bf2508c1ed096', 1);

        //fill in the variables on the request item form
        // asset is name of variable and type is reference so give sysId
        cart.setVariable(item, "asset", "someSysId");
        var rc = cart.placeOrder();
        gs.info(rc.number);

        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("request.number", rc.number);
        gr.query();
        if (gr.next()) {
            gr.requested_for = arr[count];
            gr.update();
        }
    }

} catch (ex) {
    gs.info(ex);
}

Note: I believe I covered all aspects from your original question and subsequent question and you can take it from here based on your developer skills.

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 
I tried doing this in my PDI and it created 5 new RITMs but the requested for is the system administrator. 

try {
    var count = 5;
    //user sysID
    var arr = ['62826bf03710200044e0bfc8bcbe5df1', 'a8f98bb0eb32010045e1a5115206fe3a', '0a826bf03710200044e0bfc8bcbe5d7a', '22826bf03710200044e0bfc8bcbe5dec', 'a2826bf03710200044e0bfc8bcbe5ded'];
    for (var i = 0; i < count; i++) {
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        //add your requested item to the cart by sys_id of the catalog item
        var item = cart.addItem('c7eb778f97d2415021983d1e6253af7b', 1);

        //fill in the variables on the request item form
        // asset is name of variable and type is reference so give sysId
        cart.setVariable(item, "select_group_which_needs_to_be_cloned", "d625dccec0a8016700a222a0f7900d06");
        cart.setVariable(item, "business_justification", "test");
        var rc = cart.placeOrder();
        gs.info(rc.number);

        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("request.number", rc.number);
        gr.query();
        if (gr.next()) {
            gr.requested_for = arr[count];
            gr.update();
        }
    }

} catch (ex) {
    gs.info(ex);
}