Populate fields on record with data from another table

Fred Abram III
Tera Expert

I am trying to populate some Requested Item records blank fields with data from my Task table with a Fix Script. When I run the Fix Script only one record is updated and there is no data in the field. Any suggestions???

 

FredAbramIII_0-1671820333147.png

 

1 ACCEPTED SOLUTION

Dylan Mann1
Giga Guru

Because you're saying if(grReqItem.next()) it's only going to run one time for the first RITM returned in your query. If you want to update each RITM, replace if with a while loop. 

 

Also, I assume you're going to update the SC Task query because you're currently querying every single task and using the first record returned to update each RITM. 

 

Let me know if this helps. 

 

Cheers,

Dylan

View solution in original post

4 REPLIES 4

Dylan Mann1
Giga Guru

Because you're saying if(grReqItem.next()) it's only going to run one time for the first RITM returned in your query. If you want to update each RITM, replace if with a while loop. 

 

Also, I assume you're going to update the SC Task query because you're currently querying every single task and using the first record returned to update each RITM. 

 

Let me know if this helps. 

 

Cheers,

Dylan

That helps with one record being processed but now it seems that I get stuck in a loop and the records are still not updating. I tried the advice to use setValue & getValue but still no luck. I think this is happening because some RITM have multiple Task assigned this means I have multiple numbers, short descriptions, & assignment groups on one RITM ultimately causing the script to stall

OlaN
Giga Sage
Giga Sage

Hi,

I've spotted 3 pointers.

First, you use an if-statement after the sc_req_item query, that means you only process the first record (if any are returned from the query).
Second, use setValue and getValue methods whenever possible (example below)

Third, when you do the sc_task query, you haven't got any limiters at all, so all records are returned, but again, only the first record found is used, and this record might not have any values that you are looking to fill. Maybe you should narrow the query  a bit more?

var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addEncodedQuery('your_query_here);
ritmGR.query();

while (ritmGR.next()) {   // loop through all records found

  var taskGR = new GlideRecord('sc_task');
  taskGR.addQuery('some_field', 'some_value');
  taskGR.query();
  if (taskGR.next()) {  // only processes the first record
    var someData = taskGR.getValue('some_field');  // using getValue
    ritmGR.setValue('some_field', someData);   // using setValue
    ritmGR.update();
  }

}

Thanks for the support, please mark my response as helpful since we seem to be on the same page.