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

Upender Kumar
Mega Sage

Hi 

Try below script:

 

 var gr = new GlideRecord('sc_req_item');

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

   gr.query();

   while (gr.next()){

if(gr.requested_for!=gr.opened_by){

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

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

gr.setSysAuto(false);// this will not update sys fields i.e. update by and updated date

gr.setWorkflow(false);// will not execute any workflow

   gr.requested_for =gr.opened_by;// reference field stores sys_id

gr.update();

}

}

Thanks

 

Please Hit Correct, ️Helpful depending on the impact of the response

   

Sergio24
Kilo Expert

Hello,

 

I think you need to be careful when you do the matching between the 'Opened by' and the 'Requested for'.

The 'Opened by' is just a text field so to find that person in the users table and copy it in the 'Requested for' you can only use that name. However you might have more than one person with the same name so you might end up copying the wrong one.

 

Sergio

 

"opened_by" is a Reference field back to the "sys_user" table and "sys_created_by" is a String field that contains the User ID of the user that created the record.

Lisa_Wiegmann
Giga Guru

It still is not working.  I believe it is because the requested_for field is on the RITM and the Request.  I am trying a slightly different script with everyone's feedback.  It returns the sys_id for the Request number and sys_id of the u_requester field (on the RITM).  It doesn't set the requested_for field.  I think one problem is because the query for the sc_request.

  updateRITM();

function updateRITM()

{

                var gr = new GlideRecord('sc_req_item'); 

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

               gr.query();

                            

                while (gr.next())  

                                                {           

                         var reqNum = gr.request;

                        var reqName = gr.u_requester;

                       gs.print("Number " +reqNum + " Name " +reqName); 

                     }

               

function getNumber(req){ 

                var rnum = new GlideRecord('sc_request'); 

                rnum.addQuery('number',gr.request); 

                rnum.query();

                       if (rnum.next())

                                                {

                        rnum.requested_for = gr.u_requester;

                rnum.update();

                                               

                                                                                                                                               

                                }

}

}

Did you try this script?

 

updateRITM();

function updateRITM()

{

   var gr = new GlideRecord('sc_req_item');

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

   gr.query();

   while (gr.next())

{

   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.