The CreatorCon Call for Content is officially open! Get started here.

Copying a user from a reference field on one table to another reference field on another table

matt_a
Kilo Guru

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:

find_real_file.png

Can anyone point me in the right direction? and show me what im doing wrong?

Many thanks

1 ACCEPTED SOLUTION

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

LinkedIn

View solution in original post

9 REPLIES 9

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

LinkedIn

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:

find_real_file.png

But there is no record of the business rule triggering in the logs!

 

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

LinkedIn

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

LinkedIn

This worked! I had missed out the () at the start and end. Many thanks for all your help on this