
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 04:53 PM
Hi there,
I'm trying to update a field 'u_fixed_asset_id' on the hardware asset table [alm_hardware] whenever it is updated on the fixed asset table. This is what I have so far:
Table: Fixed Assets [alm_fixed_assets]
When to run 'Before' 'insert' 'update'
Filter condition 'Fixed Asset ID' changes
function executeRule(current, previous /*null when async*/) {
// Add your code here
var fixed_asset = new GlideRecord('alm_hardware');
fixed_asset.addQuery('u_fixed_asset_id_ref', '');
fixed_asset.addQuery('u_fixed_asset_id', '');
fixed_asset.query();
fixed_asset.next();
gs.log("This is the " + fixed_asset);
fixed_asset.update(current.u_fixed_asset_id, 'u_fixed_asset_id');
})(current, previous);
It is not updating the fixed asset ID from the fixed asset table to the hardware asset table. Any ideas what I have missed?
Cheers,
Anna
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2019 05:18 PM
You'll need to make a few changes to your Business Rule also.
Firstly, when updating a related record, always use an After business rule.
This is because a data policy or business rule can abort the save, and if your business rule is before, the related record may still get updated.
Otherwise, my solution is identical to Allens, with a while loop added to update multiple matches
Table: Fixed Asset [alm_fixed_assets]
Condition: Fixed Asset ID changes
After Update/Insert Business Rule
function executeRule(current, previous /*null when async*/) {
var fixed_asset = new GlideRecord('alm_hardware');
fixed_asset.addQuery('u_fixed_asset_id_ref', current.u_fixed_asset );
fixed_asset.query();
while ( fixed_asset.next() ) {
gs.log("Match found - updating " + fixed_asset.getDisplayValue());
fixed_asset.setValue('u_fixed_asset_id',current.u_fixed_asset_id);
fixed_asset.update();
}
})(current, previous);
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 05:18 PM
The corrected code below.
Please make sure your business rule is after update/insert
function executeRule(current, previous /*null when async*/) {
// Please refer to https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideRecordAPI
var fixed_asset = new GlideRecord('alm_hardware');
fixed_asset.addQuery('u_fixed_asset_id_ref', '');
fixed_asset.addQuery('u_fixed_asset_id', '');
fixed_asset.query();
//fixed_asset.next(); - This returns true if the record is found. You need to check for this in code
if( fixed_asset.next() ) {
gs.log("This is the " + fixed_asset.getDisplayValue());
// fixed_asset.update(current.u_fixed_asset_id, 'u_fixed_asset_id'); - this is incorrect usage.
// update only takes one param, string reason
fixed_asset.setValue('u_fixed_asset_id',current.u_fixed_asset_id);
fixed_asset.update();
}
})(current, previous);
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 06:09 PM
Thanks Paul, I've changed it to an after business rule and updated the code as recommended. The gs.log statement is only returning the string 'this is the' - So the query / returning values of the 'u_fixed_asset_id' is still incorrect somewhere. Any ideas what I've missed there?
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var fixed_asset = new GlideRecord('alm_hardware');
fixed_asset.addQuery('u_fixed_asset_id_ref', '');
fixed_asset.addQuery('u_fixed_asset_id', '');
fixed_asset.query();
if( fixed_asset.next() ) {
gs.log("This is the " + fixed_asset.getDisplayValue());
fixed_asset.setValue('u_fixed_asset_id',current.u_fixed_asset_id);
fixed_asset.update();
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 06:42 PM
Ugh...this is a mess..lol...
as Skyler mentioned...the query started out ok...and then your log statement isn't going to show anything because you're not getting the display value of anything...with that line...
I'm having a tough time tracking what you're trying to do...and then the tweaks you did with Paul's help sort of made things more confusing...sorry!
Maybe I'm OCD but I really dislike this:
if( fixed_asset.next() ) {
please change to:
if (fixed_asset.next()) {
So looks like your end lines need to be something very easy like this:
fixed_asset.u_fixed_asset_id = current.u_fixed_asset_id;
fixed_asset.update();
So this ending here...is going to assume that you want the u_fixed_asset_id field ON the query'd record you just found...to match the u_fixed_asset_id on the CURRENT record on the Business Rule table this is running on...
I'm still not fully sure what record you're trying to query for as that is basically saying you want to update the first record that is found where the u_fixed_asset_id_ref is null/blank and the u_fixed_asset_id is null/blank on the 'alm_hardware' table...so hopefully it's finding what you want...
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 06:28 PM
So clearly it's finding something in your query... Otherwise, your gs.log won't work. Could you try this instead in your log statement?
fixed_asset.sys_id.getDisplayValue()