- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 06:28 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2025 08:04 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2025 04:47 AM
when cart API is used it triggers workflow or flows
So it might be causing issue and hence it doesn't work for the next iteration
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2025 04:55 AM