placeOrder() method in Cart api causing issue in while loop.

Aman Thakur
Tera Contributor

Hi everyone,

 

I have a schedule job that runs every day to query some records from a table. For each record found, there needs to be a specific catalog item ordered using the script using the Cart() API.

 

The issue is that, despite multiple records being returned by the query, the order is being placed only for the first record and script comes out of the While loop.

 

It turned out that the cart.placeorder() method is causing this issue , because when I am removing the statement, the while loop runs till all records are processed.

 

So, need your help in figuring out as to why the while loop is being terminated. There are no errors in the logs.

 

Thanks

1 ACCEPTED SOLUTION

@Aman Thakur 

try this

1) first gather all the records

2) then iterate over those

var gr = new GlideRecord("table_1");
gr.addEncodedQuery("end_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_status=In progress^u_related_ritm.cat_item=7557fae6dbd928105bdb20da1396191a");
gr.query();

var orders = [];

while (gr.next()) {
    var rel_ritm = new GlideRecord('sc_req_item');
    rel_ritm.addQuery('sys_id', gr.ritm);
    rel_ritm.query();
    if (rel_ritm.next()) {
        var req = new GlideRecord('sc_request');
        req.addQuery('sys_id', rel_ritm.request);
        req.query();
        if (req.next()) {
            orders.push({
                requested_for: req.requested_for,
                requested_by: rel_ritm.opened_by,
                original_ritm: gr.ritm
            });
        }
    }
}

// Place orders outside the loop
var cat_id = 'd8db28c6db3ca0d03a99467239963540';
orders.forEach(function(order) {
    var cart = new Cart();
    var item = cart.addItem(cat_id);
    cart.setVariable(item, 'requested_for', order.requested_for);
    cart.setVariable(item, 'requested_by', order.requested_by);
    cart.setVariable(item, 'original_ritm', order.original_ritm);

    var rc = cart.placeOrder();
    cart.deleteCart();
});

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

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Aman Thakur 

you didn't share the script and complete business requirement

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

Hi @Ankur Bawiskar ,

 

Actually, there is supposed to be a date field on these queried records. Which if matches the current date, a catalog item is supposed to be ordered for each of them.

 

This is the script:

var gr = new GlideRecord("table_1");
gr.addEncodedQuery("end_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_status=In progress^u_related_ritm.cat_item=7557fae6dbd928105bdb20da1396191a");

gr.query();
while (gr.next()) {

   
    var rel_ritm = new GlideRecord('sc_req_item');
    rel_ritm.addQuery('sys_id', gr.ritm);
    rel_ritm.query();
    if (rel_ritm.next()) {
        var req = new GlideRecord('sc_request');
        req.addQuery('sys_id', rel_ritm.request);
        req.query();
        if (req.next()) {

            var cat_id = 'd8db28c6db3ca0d03a99467239963540';
            var cart = new Cart();
            var item = cart.addItem(cat_id);
            //Set Variables in your Cart Item
            cart.setVariable(item, 'requested_for', req.requested_for);
            cart.setVariable(item, 'requested_by', rel_ritm.opened_by);
            cart.setVariable(item, 'original_ritm', gr.ritm);
 

            var rc = cart.placeOrder();
            cart.deleteCart();
        }
    }
}
 
Please ask
 
Thanks.

@Aman Thakur 

try this

1) first gather all the records

2) then iterate over those

var gr = new GlideRecord("table_1");
gr.addEncodedQuery("end_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^u_status=In progress^u_related_ritm.cat_item=7557fae6dbd928105bdb20da1396191a");
gr.query();

var orders = [];

while (gr.next()) {
    var rel_ritm = new GlideRecord('sc_req_item');
    rel_ritm.addQuery('sys_id', gr.ritm);
    rel_ritm.query();
    if (rel_ritm.next()) {
        var req = new GlideRecord('sc_request');
        req.addQuery('sys_id', rel_ritm.request);
        req.query();
        if (req.next()) {
            orders.push({
                requested_for: req.requested_for,
                requested_by: rel_ritm.opened_by,
                original_ritm: gr.ritm
            });
        }
    }
}

// Place orders outside the loop
var cat_id = 'd8db28c6db3ca0d03a99467239963540';
orders.forEach(function(order) {
    var cart = new Cart();
    var item = cart.addItem(cat_id);
    cart.setVariable(item, 'requested_for', order.requested_for);
    cart.setVariable(item, 'requested_by', order.requested_by);
    cart.setVariable(item, 'original_ritm', order.original_ritm);

    var rc = cart.placeOrder();
    cart.deleteCart();
});

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

Hi @Ankur Bawiskar ,

 

Thank you very much for your help. It is working now.

Can you please also explain, why the placeorder() method was not working inside the while loop?

 

Thanks.