
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 01:42 AM
I have a reference field on one table that I want to copy to a reference field on another table.
The fields both contain users.
I am wanting these fields to mirror so that when its updated on the master, it updates on the other table.
I am thinking that the best way to do this would be using a before business rule on an update or insert.
This is what I have tried, but its not working:
Can anyone point me in the right direction? and show me what im doing wrong?
Many thanks
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 11:45 AM
Could you also check the first and the last line in your script? Out-of-the-box, the default code starts with a (
And the last line needs an additional )
Not sure if this is the issue for not triggering, didn't test this myself.
(function executeRule(current, previous /*null when async*/) {
})(current, previous);
So:
(function executeRule(current, previous /*null when async*/ ) {
gs.info("is this triggered");
var bu = new GlideRecord('dl_hr_assignment');
bu.addQuery('u_hr_business_unit', current.sys_id);
bu.query();
gs.info("Match found - updating " + bu.getDisplayValue());
bu.setValue('u_hr_business_partner', current.u_hr_business_partner);
bu.updateMultiple();
})(current, previous);
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 02:52 AM
Also looking at your GlideRecord query again. Because you are just updating 1 or more records with exactly the same value, consider using updateMultiple() instead of a while loop with update(). updateMultiple gives you great performance advantages.
Something like:
var gr = new GlideRecord('table_name');
gr.addQuery('field_name', 'field_value');
gr._query();
gr.setValue('field_name', 'field_value');
gr.updateMultiple();
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 04:35 AM
I simulated the query and all seemed ok. I updated my script to
function executeRule(current, previous /*null when async*/ ) {
gs.log("is this triggered");
var bu = new GlideRecord('dl_hr_assignment');
bu.addQuery('u_hr_business_unit', current.sys_id);
bu.query();
gs.log("Match found - updating " + bu.getDisplayValue());
bu.setValue('u_hr_business_partner', current.u_hr_business_partner);
bu.updateMultiple();
}(current, previous);
I also set the rule to:
But there is no record of the business rule triggering in the logs!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 11:40 AM
Interesting, your Business Rule just not triggered at all.
Not sure if insert + the HR Business Partner Changes would work. It's an insert, so nothing changes?
For Update it should work I guess, HR Business Partner could change.
Do change the Business Rule to run After. This is best practice for when you are updating records on other tables.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 11:45 AM
Could you also check the first and the last line in your script? Out-of-the-box, the default code starts with a (
And the last line needs an additional )
Not sure if this is the issue for not triggering, didn't test this myself.
(function executeRule(current, previous /*null when async*/) {
})(current, previous);
So:
(function executeRule(current, previous /*null when async*/ ) {
gs.info("is this triggered");
var bu = new GlideRecord('dl_hr_assignment');
bu.addQuery('u_hr_business_unit', current.sys_id);
bu.query();
gs.info("Match found - updating " + bu.getDisplayValue());
bu.setValue('u_hr_business_partner', current.u_hr_business_partner);
bu.updateMultiple();
})(current, previous);
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-19-2019 07:44 AM
This worked! I had missed out the () at the start and end. Many thanks for all your help on this