Updating the child table if the parent table field changes

Csaba B_rsics
Tera Contributor

I have to update 3 fields on my child table if the same fields are changed on the parent. I made a business rule for the child table, but I don't know how to update the fields after query:

 

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

 

var read_wo = new GlideRecord("x_parent_table");
read_wo.addQuery('parent_table', current.sys_id); // if the parent table and the child table belongs to each other
read_wo.query();

current.field1=

current.field2=

current.field3=


current.update();
current.setAbortAction(true);

}(current, previous);

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

Hi You need to write After update BR on Parent table

script:

var read_wo = new GlideRecord("childtable");
read_wo.addQuery('parent', current.sys_id); // if the parent table and the child table belongs to each other
read_wo.query();
while(read_wo.next())
{


read_wo.field1= current.field1;

read_wo.field2= current.field2;

read_wo.field3=current.field3;


read_wo.update(); //update child table
}

Regards
Harish

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

You need an After Update business rule that runs on Parent table with condtion as

Field 1 | changes

OR

Field 2 | changes

OR

Field 3 | changes

Script  as below

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

 var read_wo = new GlideRecord("table_name");//replace table_name with parent table name
read_wo.addQuery('sys_id', current.parent); // replace parent with field that stores parent record on child table
read_wo.query();
while(read_wo.next())
{
current.field1=read_wo.field1;//replace field1 with correct field name

current.field2=read_wo.field2;

current.field3=read_wo.field3;
}

current.update();

}(current, previous);

OlaN
Giga Sage
Giga Sage

Hi,

Updating another record after some values changes is kind of straightforward.
You might not even need to script it.
Have you considered using Flow designer for this task?

Example:

find_real_file.png

Harish KM
Kilo Patron
Kilo Patron

Hi You need to write After update BR on Parent table

script:

var read_wo = new GlideRecord("childtable");
read_wo.addQuery('parent', current.sys_id); // if the parent table and the child table belongs to each other
read_wo.query();
while(read_wo.next())
{


read_wo.field1= current.field1;

read_wo.field2= current.field2;

read_wo.field3=current.field3;


read_wo.update(); //update child table
}

Regards
Harish

SumanthDosapati
Mega Sage
Mega Sage

Hi,

Business rule should be on Parent table.

Type : After Update business rule

Condition : field1 changes OR field2 changes OR field3 changes

Script :

var gr = new GlideRecord("child_table");
gr.addQuery('parent', current.sys_id); // check if field name is 'parent' or not
gr.query();

gr.field1=current.field1;
gr.field2=current.field2;
gr.field3=current.field3;

gr.update();

 

Mark as correct and helpful if it solved your query.

Regards,

Sumanth