I'm running a background script to update the old records of a sys_id requested_for column to the custom column u_opened_for and it's returning this message when I run the script and it just updates some records

1231
Tera Contributor

var gr = new GlideRecord('sc_req_item');
    gr.addEncodedQuery('u_opened_for=NULL^ORDERBYu_opened_for')
    gr.query();
    while (gr.next()){
    gr.u_opened_for = gr.variables.requested_for.getDisplayValue();
    gr. setWorkflow ( false ) ;
    gr.update();

    }

    

 

find_real_file.png

7 REPLIES 7

Tony Chatfield1
Kilo Patron

Hi, without details of your configuration and the value in the error message I would guess the issue is that 'u_opened_for' is a reference to the sys_user table and your variable is not a unique sys_id for a sys_user record?
You should be able to add a lookup of 'sys_user' based on your variable content (assuming it's unique).
Perhaps something like


 while (gr.next()){
 var userCheck = new GlideRecord('sys_user');
 //guessing the variable display value is a name?
 if(userCheck.get('name', gr.variables.requested_for.getDisplayValue())) {
	gr.u_opened_for = userCheck.sys_id;
	gr. setWorkflow(false);
    gr.update();
	} else {
	gs.info("No match found for " + gr.variables.requested_for.getDisplayValue());
	}
}

 

 

Nikhil65
Mega Guru

Hi 123,

It seems your u_opened_by is a reference field for sys_user table.

When you are passing gr.u_opened_for = gr.variables.requested_for.getDisplayValue();

This wont work. For this to work, you will have to pass sys_id.

If you want the display value to be displayed then maybe try the following code:

var gr = new GlideRecord('sc_req_item'); gr.addEncodedQuery('u_opened_for=NULL^ORDERBYu_opened_for');

gr.query();

while (gr.next())

{

gr.u_opened_for.set.setDisplayValue(gr.variables.requested_for.getDisplayValue());

gr.setWorkflow(false);

gr.update();

}

1231
Tera Contributor

I ran this script, but it didn't return any information even though I gave gs.print

A small correction in the above code: var gr = new GlideRecord('sc_req_item'); gr.addEncodedQuery('u_opened_for=NULL^ORDERBYu_opened_for'); gr.query(); while (gr.next()) { gr.u_opened_for.setDisplayValue(gr.variables.requested_for.getDisplayValue()); //here was a small mistake. gr.setWorkflow(false); gr.update(); } Also please confirm if u_opened_for is a reference field or not. If u_opened_for is a reference field only then the above code will work. If it doesn't work, then let me know, will give you a tested code by eod. But let me know the fieldtype of u_requested_for