Problem updating a reference field with gliderecord

aansell
Mega Expert

Hi,

We are having an issue with the  following code - which runs but does not update the reference field we are targeting. Can anyone advise on the code below and why it isn't working?

The code is based on the  following page  http://wiki.servicenow.com/index.php?title=Inserting/Updating_GlideRecord_with_References#gsc.tab=0 but perhaps it has not been correctly understood.  Any help would be much appreciated!

Thanks,

Arthur

 

 

--------------

//var asset_room = current.u_asset_room;

asset_room = 'S602';

//var asset_id = current.asset_tag;

var asset_id = 'test001';

var count = 0;

 

var computers = new GlideRecord('cmdb_ci');

//

// Add the asset_tag to our query to find the record we want in cmdb_ci

computers.addQuery('asset_tag', '=', asset_id );

//  

//

computers.query();

//

//Find the matching record in cmdb_ci , update the CI room field

//

while (computers.next()) {

   computers.u_room.setDisplayValue(asset_room);

   count ++;

   computers.updateWithReferences();

}

//

gs.log('Number of cmdb_ci records processed for room change is ' + count + '- ' + computers.u_room  + "\n" + "Asset room = " + asset_room + "\n" + "Asset ID = " + asset_id);

///

1 ACCEPTED SOLUTION

andrewpilachows
Kilo Guru

//var asset_room = current.u_asset_room;


asset_room = 'S602';


//var asset_id = current.asset_tag;


var asset_id = 'test001';


var count = 0;



var computers = new GlideRecord('cmdb_ci');


//


// Add the asset_tag to our query to find the record we want in cmdb_ci


computers.addQuery('asset_tag', '=', asset_id );


//


//


computers.query();


//


//Find the matching record in cmdb_ci , update the CI room field


//


while (computers.next()) {


    computers.u_room.setDisplayValue(asset_room);


    count ++;


    computers.updateWithReferences();


}


//


gs.log('Number of cmdb_ci records processed for room change is ' + count + '- ' + computers.u_room   + "\n" + "Asset room = " + asset_room + "\n" + "Asset ID = " + asset_id);


///



If this is copied directly, you are missing "var" on line 2 which would make your variable on line 18 undefined.   There is also a space on line 19 that may throw an error and stop script processing, "count++".



To make sure I understand what you are trying to do in the code, are you trying to update a value IN the room record, or are you trying to relocate the CI to a different room?   Is u_room a reference field or a string field?   Line 18 would set the display value of the u_room reference field on the CI record to "S602", and line 20 would save any changes to the reference records.   If you are not trying to change any values in the room record, just use "computers.update()".   Also make sure that the room record "S602" exists in the room table.


*What I mean is, you can set a reference field two ways, by setting the sys_id to the record you want the field to reference, or using the display value to look up the record.   This will not update any field values on the reference record.   The wiki you linked is aiming to change values in a record that is not the current or queried glide record.



Another question, does the u_room field exist on the cmdb_ci table, or the cmdb_ci_computer table?   You may need to change your glide record to query a different table.



In the end, are you going to have the room static in the script or keyed off the current record?   If you are going to eventually use u_asset_room to update the CIs, then at that time, assign the value directly, as long as u_asset_room is also a reference field to the same table and not a string field.   Reference fields store the sys_id of the associated record, whereas setDisplayValue will do a lookup for the associated record name.


var asset_room = current.u_asset_room; // As long as u_asset_room is a reference field/sys_id and not string


...


...


while (computers.next()) {


  computers.u_room = asset_room;


  count++;


  computers.update();


}


...



I also recommend using the docs site instead of the wiki as it will have more up-to-date information Product Documentation | ServiceNow


View solution in original post

8 REPLIES 8

Subhajit1
Giga Guru

Please use the following:-


while (computers.next()) {


    computers.u_room.{field element name of the referenced table} = asset_room;


    count ++;


    computers.update();


}



Thanks,


Subhajit


Hi Subhajit,


Thanks for the advice. I have updated the code but unfortunately it still does not update. The code is now as follows. The field 'u_location_reference' is the column name of the field in the table 'rooms' that the 'cmdb_ci.u_room' field is referencing.   - perhaps I misunderstood your comment or maybe my syntax incorrect?  


Thanks,


Arthur


------------------------------------------------------------------------


while (computers.next()) {


    computers.u_room.u_location_reference = asset_room;


    count ++;


    computers.update();


}


At the top of your code, can you please write:-


var asset_room = 'S602';


I think you missed out the var there and hence it is not updating. It should update with the lower bit of code that you have.


Just to confirm, the u_location_reference is a string field, right?



Thanks,


Subhajit


Hi,


Sadly, still no update the code is now. I can confirm that the field u_location_reference a string field. I have been discussing with a colleague and we noticed the 'Display = True' if that is relevant?


Thanks,


Arthur




var asset_room = 'S602';


var asset_id = 'test001';


var count = 0;



var computers = new GlideRecord('cmdb_ci');


// Add the asset_tag to our query to find the record we want in cmdb_ci


computers.addQuery('asset_tag', '=', asset_id );


computers.query();


//


//Find the matching record in cmdb_ci , update the CI room field


while (computers.next()) {


    computers.u_room.u_location_reference = asset_room;


    count ++;


    computers.update();


}