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

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

 

DirkRedeker
Mega Sage
Hi You can go the no code way, I guess. Create an AFTER UPDATE Business Rule on the Child table and and set the values by dotwalking to the parent table. BR Dirk