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

Omkar Mone
Mega Sage

Hi 

Firstly you gave used gs which is the Global object provided by servicenow for GlideSystem.

Don't use that and use gr or any other name you wish to.

And if you want to retirve all the requests the remove the addQuery and just use query.

then

 

var gr = new GlideRecord('sc_req_item');

gr.query();

while(gr.next())

{

if(//here check you conditions)

{

 sc_req_item.name = cat_item;

}

}

 

Mark correct if it helps.

 

Regards,

Omkar Mone

www.dxsherpa.com

Hi Omkar,

I currently have this and it still is not working:

var cat_item = current.u_catalog_item;
var num = current.number; 

var gr = new GlideRecord('sc_req_item');
gr.query();
while (gr.next){
     if (sc_req_item.request == num){
          cat_item = sc_req_item.name;
     }
}

Is there anything else I should be doing?

Hi 

  if (sc_req_item.request == num){
          cat_item = sc_req_item.name;
     }

This is wrong. You want the request feild value from sc_req_table??

the use gr.request and gr.name.

Thanks.

 var cat_item = current.u_catalog_item;
 var num = current.number;
 
 var gr = new GlideRecord('sc_req_item');
 gr.query();
 while (gr.next){
  if (gr.request == num){
   cat_item = gr.cat_item.name;
  }
 }

This query ran for over 4 minutes and then did not even update the field.