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

How to populate custom field on Request table with value from Requested Item table?

jaredholm
Kilo Contributor

I would like to populate a custom string field (could be changed to a reference field if that is more appropriate?) called "u_catalog_item" I have created on the Request (sc_request) table with the value of the Item (cat_item) field on the Requested Items (sc_req_items) table. I need to filter REQs based on the Catalog Item from which they were created. For example, I have a catalog item "Request to Pull Email". The only place the name of this catalog item appears is on the RITM table. So, I have to populate the field on the REQ table with the "name" value on the RITM table. This requires matching the records based on REQ### and then populating the custom field. I have created a business rule to automatically populate the custom field, but am getting stuck on the GlideRecord query to grab the Request numbers and match them to copy the name data. I currently have an after Insert or Update BR that has a script that looks like this:

     var cat_item = current.u_catalog_item;
     var gs = new GlideRecord('sc_req_item');
     gs.addQuery('request');
     gs.query();
     while (request == sc_request.request){
          cat_item = sc_req_item.name;
     }

Can somebody please tell me what is wrong with this script? Much appreciated

15 REPLIES 15

That's strange, shouldn't take that amount of time.

Try adding some logs to the script to see what values you actually get

var reqID = current.getUniqueValue()
gs.log('Request SYS ID: ' + reqID);
 
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request', reqID);
gr.query();
gs.log('BEFORE IF');
if (gr.next()){
   gs.log('ITEM NAME: ' + gr.getDisplayValue('cat_item'))
   current.setValue('u_catalog_item', gr.getDisplayValue('cat_item'));
}

Do you have something else that triggers on the request that could cause this?

Another issue you have is if you have more than 1 request item related to a request, but that might not be the case for you?