
- 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 07:22 PM
Thanks Skyler - now it is returning the sys_id rather than display value

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2019 07:27 PM
If you want this to update all records that match your query, change this line
if( fixed_asset.next() ) {
to this
while( fixed_asset.next() ) {
Please mark as correct if this has solved your issue 🙂
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 07:47 PM
Thanks all for you help - this is what I'm trying to achieve:
1. Table: Fixed Asset [alm_fixed_assets]
2. Field: fixed_asset (string)
3. Field: u_fixed_asset_id (string)
4. Table: Hardware Asset [alm_hardware]
5. Field: u_fixed_asset_id_ref (references alm_fixed_assets)
6. Field: u_fixed_asset_id (string)
when 3 is updated it should update 6, based on 2 and 5 matching.
Hardware Assets fixed asset ID is automatically updated with the fixed as ID in the fixed assets table, based on the associated fixed asset.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2019 05:04 AM
(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', current.fixed_asset);
fixed_asset.query();
if(fixed_asset.next()) {
fixed_asset.u_fixed_asset_id = current.u_fixed_asset_id;
fixed_asset.update();
}
})(current, previous);
This searching the alm_hardware table for a match between the current record's fixed_asset table and matching it to this custom u_fixed_asset_id_ref field you have on this table.
Once the match is found, it will update the custom u_fixed_asset_id field with the current record's custom u_fixed_asset_id field.
Please mark reply as Helpful/Correct. 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-26-2019 05:40 AM
Glad you got it figured out. I'd say as far as providing context and what it does, this was provided on my end as well. On this post alone you had two people who actually try to help explain things in addition to providing a solution. On other threads you won't see as much detail.
Take care!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!