
- 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-28-2019 07:25 PM
Thank you Allen, I definitely appreciate your help and input.
I'm new to JavaScript, so it confusing piecing it together at times.
Unfortunately, I haven't got it working completely as I thought I did, as it was causing a run-time delay - looking at all of the records. So I'm trying to look for a better unique identifier between the two tables.
Cheers,
Anna

- 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-25-2019 02:12 PM
Thanks Paul, it's also helpful to have a bit more context, much appreciated!