The CreatorCon Call for Content is officially open! Get started here.

Problems with updating queried records in a while/if loop

Ricky S Larsson
Tera Guru

I'm having some problems with my script where I want to update records that I query, but some of the doesn't update.

 

So what I am trying to do is that I want to reprocess transactions of Catalog orders where the items have been shipped but the orders haven’t been closed, because of an earlier bug.

The process is as follows: when an item is shipped a Delivery Note is sent from the vendor to SNOW. The Delivery note have a reference to the Request (RQ) . Then the Catalog item tasks (RQT) for that RQ automatically gets closed complete and pending status goes to shipped/delivered.

I made a script that check for pending RQTs that have a Delivery Note referring to their Request (RQ) but haven’t been closed correctly. Then the script reprocesses them. Voila, closed and done!

// Query RQTs that are Vendor orders and pending
var task = new GlideRecord('sc_task');
task.addEncodedQuery("correlation_idLIKEpending^assignment_group=9b664a1437a15a087898ab2943990ec4");
task.query();
var i = 0; // Counter

// Query all related RCMS Delivery Notes
while(task.next()){
  var rcms = new GlideRecord('u_rcms_transaction');
  rcms.addQuery('u_task', task.request);
  rcms.addQuery('u_type', 'DeliveryNote');
  rcms.query();

  if (rcms.next()) {
    // Reprocess Transaction on all related Delivery Notes
    rcms.u_state = 'Ready';
    rcms.update();

    // Count REQs
    i++;
    gs.print(task.number);
    gs.print(task.request.getDisplayValue());
  }

}
gs.print("Number of Records: " + i);

Problem is, when there are more than 1 delivery note per Request, it doesn’t reprocess the 2nd delivery note. I try to run the script again but it only queries them, print them out but doesn’t reprocess them, only the 1st one.

What do I have to add / change in my script so that ALL of the delivery notes get reprocessed, not only one?

1 ACCEPTED SOLUTION

As a debug statement, I would add right the rcms.query() statement,

gs.info(rcms.getRowCount() + ' records read');

That will tell you if your query is right or wrong. If it's wrong, check the addQuery() statement closely. are the u_ fields correct? Are the values you are sending to them correct? Do you want task.request (the parent of the parent)? All these things can cause your query to awry.

View solution in original post

5 REPLIES 5

Chuck Tomasi
Tera Patron

Try changin the line

  if (rcms.next()) {

to

  while (rcms.next()) {

If will get the first one, while will iterate through all.

With the While loop, now I get even more records in my query but still only 1 delivery note is reprocessed

As a debug statement, I would add right the rcms.query() statement,

gs.info(rcms.getRowCount() + ' records read');

That will tell you if your query is right or wrong. If it's wrong, check the addQuery() statement closely. are the u_ fields correct? Are the values you are sending to them correct? Do you want task.request (the parent of the parent)? All these things can cause your query to awry.

Thank you Chuck, I could now see there were some troubles with querying the RCMS. Also found out that some old requests couldn't simply be re-processed, even manually, so there is a bigger problem involved outside of this script. This is something I have to take with the developer behind the RCMS transactions.

I re-wrote the script with the second loop in a function, since there were some hiccups in running the loops correctly. Now I can find requests with more than 1 delivery note at least.

// Query RQTs that are Vendor orders and pending
var task = new GlideRecord('sc_task');
task.addEncodedQuery("correlation_idLIKEpending^assignment_group=9b664a1437a15a087898ab2943990ec4");
task.query();

// Loop through RCMS, print out tasks numbers and run Reprocess Function on relevant requests
while(task.next()){
  gs.print(task.number);
  checkRCMS(task.request.toString());
}

// Reprocess Function
function checkRCMS(task){
  // Query RCMSs that have pending RQTs with Delivery Notes
  var rcms = new GlideRecord('u_rcms_transaction');
  rcms.addQuery('u_task', task);
  rcms.addQuery('u_type', 'DeliveryNote');
  rcms.query();

  while(rcms.next()) {
    // Reprocess Transaction on all related Delivery Notes
    rcms.u_state = 'Ready';
    rcms.update();
    gs.print('---' + rcms.u_number);
  }
}