Business rule to update field on another table / GlideRecord help

Anna Gausel
Giga Expert

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

1 ACCEPTED SOLUTION

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

View solution in original post

12 REPLIES 12

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

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

Thanks Paul, it's also helpful to have a bit more context, much appreciated!