Need a background script to update the RITMs requested_for field with the opened_by value

Lisa_Wiegmann
Giga Guru

We have a certain type of Service Request that wasn't having the "requested_for" field being set.  I've been trying to write a background script to update one record.  After I get one working, I'll write a fix script to update the rest. I can get ahold of the opened_by name on the RITM, but I can't get the requested_for field to populate.  Here is one of my attempts:

updateRITM();

function updateRITM()

{

   var gr = new GlideRecord('sc_req_item');

   gr.addQuery('number', 'RITM0128093'); 

   gr.query();

   while (gr.next()){

   var openName = gr.getDisplayValue('opened_by'); //I get the person's name

   gs.print(openName);   //It does print the person's name

   gr.requested_for = openName;  //When I try writing to the log the value is null

    gr.update();

}   

}

 

9 REPLIES 9

Mike-D
Tera Expert

Try passing the sysID value from openName (gr.opened_by), rather than the display value. requested_for is looking for a sys_id since it's a reference.

 

 

function updateRITM()

{

   var gr = new GlideRecord('sc_req_item');

   gr.addQuery('number', 'RITM0128093'); 

   gr.query();

   while (gr.next()){

   var openName = gr.getDisplayValue('opened_by'); //I get the person's name

   gs.print(openName);   //It does print the person's name

   gr.requested_for = gr.opened_by;  //When I try writing to the log the value is null

    gr.update();

}   

}

But don't forget that the "requested_for" is actually on the Request table.  You would need to get to the Request record to update it there.

 

So you should replace the "gr.requested_for = gr.opened_by" line with:

var request = gr.opened_by.getRefRecord();
if (request) {
    request.requested_by = gr.getValue("opened_by");
    request.update();
}

SanjivMeher
Kilo Patron
Kilo Patron

Do you have a requested_for field on RITM. I think it is on the request.

Try the below code.

 

updateRITM();

function updateRITM()

{

   var gr = new GlideRecord('sc_req_item');

   gr.addQuery('number', 'RITM0128093'); 

   gr.query();

   while (gr.next())

{

   var openName = gr.getDisplayValue('opened_by'); //I get the person's name

   var gr1 = new GlideRecord('sc_request');

gr1.addQuery('sys_id',gr.request)

gr1.query();

if (gr1.next){

   gr1.requested_for = gr.opened_by;  //When I try writing to the log the value is null

    gr1.update();

}

}   

}


Please mark this response as correct or helpful if it assisted you with your question.

vinothkumar
Tera Guru

There is no field requested for in RITM table, If it is a custom field, it may starts with u_field name. 

 

If the field is there, you have to do something similar as below.

 

You have to update the requested table requested for

 

updateRITM();

function updateRITM()

{

   var gr = new GlideRecord('sc_req_item');

   gr.addQuery('number', 'RITM0128093'); 

   gr.query();

   while (gr.next()){

   //var openName = gr.getDisplayValue('opened_by'); //I get the person's name

  // gs.print(openName);   //It does print the person's name

   gr.requested_for = gr.opened_by;

    gr.update();

}   

}