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

Jaspal Singh
Mega Patron
Mega Patron

Hi Richard,

 

 

Try passing in the system name for type instead of system label once. Also, as Chuck suggested the while() loop should make things work. 

rcms.addQuery('u_type', 'DeliveryNote');

 

Thanks,

Jaspal Singh