
- 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 01:45 AM
Hi,
AS you said there are reference fields, do not use displayValue
just mention the column
bu.setValue("u_hr_business_partner",current.u_hr_business_partner);
Mark the comment as a correct answer and helpful once worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 01:46 AM
Hi there,
Line 4, u_hr_business_unit.sys_id. Don't think you need the .sys_id. u_hr_business_unit is already a sys_id? Unnecessary dotwalking to a sys_id can cause performance issues.
Line 9, did that get logged? If not, your query might not be oke already.
Line 10, the .getDisplayValue() is not needed. If u_hr_business_partner is already the sys_user reference, then this is already a sys_id. Seeing what you wrote, u_hr_business_partner is also a reference to sys_user. So no need for the .getDisplayValue().
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:43 AM
Hi Mark, thanks for taking the time to respond.
I have updated the script to:
function executeRule(current, previous /*null when async*/) {
var bu = new GlideRecord('dl_hr_assignment');
bu.addQuery('u_hr_business_unit', current.sys_id);
bu.query();
while (bu.next() ) {
gs.log("Match found - updating " + bu.getDisplayValue());
bu.setValue('u_hr_business_partner',current.u_hr_business_partner);
bu.update();
}
}(current, previous);
However, it still doesnt work. Its not even registering the log statement, so as you say, something must be amiss with the query

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2019 02:47 AM
Can you simulate your query thru the list? Then right mouse click on the breadcrumb, copy query. How does the query look like? Hopefully something like:
u_hr_business_unit=8564af3adb810410d58ea345ca9619dd
Is the table name also correct? Maybe it comes with HR plugin I don't know, though maybe dl_u_hr_assignment?
Also, do you have conditions on your business rule? Maybe start your business rule for example with a dummy log statement, even before your GlideRecord query, just to see if the business rule is triggered at all.
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