Updating another table field value using Business Rule

Mohammed Kemal
Tera Guru

Hi everyone,

I have a table 2 called (sn_compliance_control) where I have a custom string field(u_control_id). I would like this field to be updated from another table 1 (sn_compliance_policy_statement)'s field called "Reference"(reference).

N.B. the field reference is a string field found within the table

I wrote after BR in table 1 with

  • Condition set to (Reference is not empty),
  • When set to async, and
  • Advanced checked (insert, update, query)

Here is my script:

(function executeRule(current, previous /*null when async*/) {

	var referenceValue = new GlideRecord('sn_compliance_control');
	referenceValue.addQuery('u_control_id', current.reference);
	referenceValue.query();
	
	while (referenceValue.next()){
		
		referenceValue.setValue('u_control_id', current.reference);
		referenceValue.update();
	}

})(current, previous);

My goal is to update all control id in my table 2 with table one value. I need a second eye to my BR/code since it's not doing the way it supposed to do?

Thanks,

Mokemal!

1 ACCEPTED SOLUTION

Upender Kumar
Mega Sage

Thanks @mokemal 

 As parent record will not change after it is inserted. We created a scheduled job to update the Control ID field of the child tables.

If you need you can create a before BR on sn_compliance_control

var referenceValue = new GlideRecord('sn_compliance_policy_statement');
	if(referenceValue.get(curent.content)
	{
current.u_control_id=referenceValue.reference

	}

 

View solution in original post

23 REPLIES 23

Hi Mark, thanks for looking at it!

Yes, it's string.

The BR is in table one (control objective) table. The two table relates by Control Objective field referencing control objective from the control record.

Here is the updated code:

Let me know what's missing

(function executeRule(current, previous /*null when async*/) {

	var referenceValue = new GlideRecord('sn_compliance_control');
	//Content is the Control objective field refering the table 1
	//name is the field in table one for the reference
	referenceValue.addQuery('content', current.name);
	referenceValue.query();
	
	while (referenceValue.next()){
		
		referenceValue.setValue('u_control_id', current.reference);
		referenceValue.update();
	}

})(current, previous);

 

This does not look like the code that I shared? Can you try the code that I actually shared?

For example, this is different:

	referenceValue.addQuery('content', current.name);

You are now querying on content (like I mentioned), though using the name. Why? content is a reference field, name is a string. That will not work.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Right, I added above line of code to link the two tables.

I have also tried your below code but there is no update:

(function executeRule(current, previous /*null when async*/) {

	var referenceValue = new GlideRecord('sn_compliance_control');
	referenceValue.addQuery('content', current.getUniqueValue());
	referenceValue.query();
	
	while(referenceValue.next()){
		referenceValue.setValue('u_control_id', current.reference);
		referenceValue.update();
	}

})(current, previous);

Can you add debugging? To see if the Business Rule is triggered, up to which point the code is working, etc..

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Just tested the code (with a record update), works fine here.

Businss Rule
sn_compliance_policy_statement
When = after
Insert = true
Update = true

Code:

(function executeRule(current, previous /*null when async*/) {

	var referenceValue = new GlideRecord('sn_compliance_control');
	referenceValue.addQuery('content', current.getUniqueValue());
	referenceValue.query();
	
	while(referenceValue.next()){
		referenceValue.setValue('u_control_id', current.reference);
		referenceValue.update();
	}

})(current, previous);

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn