- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 07:30 AM
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);
///
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 08:50 AM
//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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 08:36 AM
Hello Arthur,
Please try the below script once:
var asset_room='LTP123456';
var asset_id='P1000226';
var count=0;
var gr=new GlideRecord('cmdb_ci');
gr.addQuery('asset_tag', asset_id); //asset_tag is a field on cmdb_ci table
gr.query();
if(gr.next()){
gr.u_room=asset_room; //set the serial number
gr.update();
count++;
}
Hope this helps.
Please hit Like or mark Helpful or Correct based on the impact.
Thanks,
Rajshekhar Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 08:50 AM
//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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 01:56 AM
Hi Andrew,
Many thanks for your review of the code and taking the time to investigate and advise on the issue the issue. The code is now corrected (spotting the extra space before the ++ was a great help!) we have got it to update the field 'cmdb_ci.U_rooms' as planned. I have answered your questions below as a way of documenting the various points raised and to help anyone in the future with a similar issue.
btw: I am interested what you used to inserted the nicely formatted code examples?
Best regards,
Arthur
Q: 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?
A: We want to relocate the CI to a different room.
Q: Is u_room a reference field or a string field?
A: u_room is a reference field.
Q: If you are not trying to change any values in the room record, just use "computers.update()".
A: We are not changing the values in the room record so have reverted to computers.update()
Q: 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.
A: The field is in cmdb_ci so the glide record is correct.
Q: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.
A: Yes we will be using the current record. The next step is use this code as a basis for a business rule, with the variables set from the current record.
The working code is as follows:
var asset_room = 'S603';
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.update();
}
//
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 02:00 PM