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

asifnoor
Kilo Patron

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 Roethof
Tera Patron
Tera Patron

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

LinkedIn

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

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

LinkedIn