Update Parent record State

Lean S
Mega Expert

Hi All,

I want to update Parent record state when one or more child records state changed to Rework state then I need to update parent state also Rework,

Like wise I want to do it for 2 states : Rework and Reinstall. 

Kindly help me how to achieve this.

Parent is Test Case table and Child is Test case steps table.

Thanks,

Lean S

1 ACCEPTED SOLUTION

Sourav16
Kilo Guru

Hi,

Create an After Update BR

Filter conditions :-

State changes to Rework  OR

state changes to Reinstall AND

parent is not empty

Try this code :-

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

    var recGa = new GlideAggregate('Table Name'); //GlideAggregate for getting the child records count
    recGa.addQuery('parent', current.getValue('parent')) // quering the records having same parent
    recGa.addQuery('state', current.getValue('state')); // quering state of the current child with other childs
    recGa.addAggregate('COUNT');
    recGa.query();
    if (recGa.next()) {

        var childCount = parseInt(recGa.getAggregate('COUNT'), 10); // getting the count of child records
        if(childCount >= 1) { //checking if child count is greater than or equal to 1

            var parentGr = current.parent.getRefRecord(); // getting the GlideRecord object of the parent record
            parentGr.setValue('state', current.getValue('state')); //setting the state value
            parentGr.update(); //updating the parent record

        }
    }

})(current,previous);

 

Note : please make changes in the code if field names are different.

 

Thanks,

Sourav

 

View solution in original post

6 REPLIES 6

Ct111
Giga Sage

Hello check response of ankur in below  

 

Example

 

Mark my ANSWER as CORRECT and HELPFUL if it helps

mr18
Tera Guru
Tera Guru

Hi Lean,

Create After Update Business Rule on Test case Step table

Set the condition as State Changes

var gr = new GlideRecord('<Table name of test case table>');
gr.addQuery('sys_id', current.parent);
gr.query();
if(gr.next()) {
if(current.state.ChangesTo(<choice value of Rework>)
gr.state = <choice value of Rework>;
if(current.state.ChangesTo('<choice value of Reinstall>)
gr.state = <choice value of Reinstall>;
gr.update();
}

Hi,

I have used the code, but did not work for me , below is as saved in my script:

 

var gr = new GlideRecord('tm_test_instance');
gr.addQuery('sys_id', current.parent);
gr.query();
if(gr.next()) {
if(current.state.ChangesTo('rework'))
gr.state = 'rework';
if(current.state.ChangesTo('reinstall'))
gr.state = 'reinstall';
gr.update();
}

Thanks,

Lean S

Hi Lean,

You have to check the second line of the code.
Check which field on test case step table is linking to test case table. There would be any reference field on test case step table that must be referring to test case table.

In the second line of the code, instead of current.parent you have to write current.<field name referring to test case table>