Programatically Create RITMs

Victor53
Tera Contributor

I'm querying some requested items in a scheduled job. For every item found, I need to create a different ritm. Apparently, using Cart, CartJS, or sn_sc.CartJS in a while loop, only generates a single ritm and the stops. Is there a way to create multiple ritms programatically ?

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Victor53 

long back I created a blog, you can have a check on that and enhance it further as per your requirement

Multiple RITMs in same Request Using CartAPI 

Also do share your script and what's not working

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

@Victor53 

Hope you are doing good.

Did my reply answer your question?

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

@Victor53 

Hope you are doing good.

Did my reply answer your question?

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

Gaurav Rathaur
Kilo Guru

Hi @Victor53 , you can try below script to create multiple ritms, using CartJS or sn_sc.CartJS() in a loop won't generate multiple RITMs, because those APIs are designed for a single cart checkout per session, which clears the cart after checkout. So in a loop, it only successfully processes the first iteration, then fails (or silently does nothing) in the next iterations.

(function executeRITMCreation() {
    var itemID = 'YOUR_CATALOG_ITEM_SYS_ID'; // Replace with your catalog item
    var userID = 'USER_SYS_ID'; // Requested for

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('active', true); // your query
    gr.query();

    while (gr.next()) {
        // Create a new REQ
        var request = new GlideRecord('sc_request');
        request.initialize();
        request.requested_for = userID;
        request.requested_by = userID;
        request.u_custom_field = gr.sys_id; // link to original record if needed
        request.insert();

        // Create a new RITM
        var item = new GlideRecord('sc_req_item');
        item.initialize();
        item.request = request.sys_id;
        item.cat_item = itemID;
        item.quantity = 1;
        item.requested_for = userID;
        item.short_description = "Created from scheduled job";
        item.insert();

        // Optional: populate variables
        var varGr = new GlideRecord('sc_item_option_mtom');
        varGr.initialize();
        varGr.request_item = item.sys_id;
        varGr.sc_item_option = 'VARIABLE_SYS_ID'; // reference to sc_item_option table
        varGr.insert();

        gs.info('Created RITM: ' + item.number);
    }
})();

 If this helped, please mark it as helpful or answered so others can find it easily too!