Need Business Rule to Update Parent State

sarahyelton
Tera Expert

I really do not understand why this has been so difficult to achieve but I need to update the state of a parent record when the state of a child record is updated.  I have two custom tables extended from task.  I want table A to be updated when table B is updated.  Table B references table A via OOB parent field.  I created a few custom state choices for table A.  When the state change to closed complete (3) on table B, I want the state on table A to updated to Access Granted (20).  When the state changes to closed incomplete (4) on table B, I want the state on table A to be updated to Access Rejected (22).  I would like to add a condition where u_request_type on table B is 'Add Access'.  For the love of god, please help.  🐵

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You can create an onAfter Business Rule for this on Table B.

with condition Request Type == Add Access && state Changes to Close Complete OR state Changes to Closed Incomplete

 

var a = new GlideRecord('A');

a.addQuery('sys_id',current.parent);

a.query();

 

if (a.next())

{

if (current.state==3)

a.state = 20;

else if (current.state==4)

a.state = 22;

 

a.update();

}


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

4 REPLIES 4

Erik Gunther2
Kilo Guru

Summarizing your question, you are asking how to use a Business Rule of a child table to update a parent table? The child table has a Parent field. If this is correct then here's my response.

 

My guess is you are trying to dot walk from the Parent field in the Actions tab, but you are not able to. The Business Rule Actions tab doesn't support dot walking.

 

Therefore, I would suggest clicking the Advanced check box and write a script. Details:

  1. Check Advanced checkbox
  2. When to Run tab:
    1. When = After (use After when updating another table, use Before when updating the table the business rule is based upon)
    2. Check Update
  3. Advanced tab:
    1. Include the following script (modify it to suit your specific scenario) within the function:

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

// Add your code here
current.parent.state = 'x';
current.parent.update();

})(current, previous);

 

If you are saying that you are not able to reference a field via the Parent dot walking, that would be because the Parent field is specifically based on Task. You may need to create another reference field to your parent table that references the extended table (not the task table).

 

Those are the 2 approached that come to mind based on your question. I hope this helps!

sayali udgave
Kilo Guru

Hi

sarahyelton

 

Please Check Out This

 

var btb = new GlideRecord('Parent Table');

if(btb.get(current.Parent Table))

{

      var acc = new GlideRecord('Child Table');

      acc.addQuery('Parent Table',   current.Parent Table);

      acc.query();


        var summary = '';

      while (acc.next())

            summary += acc.getValue('Field which you want yo update') + '\n\n';


btb.Parent table = summary;

btb.update();

}
 
 
Hope this will help you
 
Thanks & Regards
Sayali Anil Udgave

 

SanjivMeher
Kilo Patron
Kilo Patron

You can create an onAfter Business Rule for this on Table B.

with condition Request Type == Add Access && state Changes to Close Complete OR state Changes to Closed Incomplete

 

var a = new GlideRecord('A');

a.addQuery('sys_id',current.parent);

a.query();

 

if (a.next())

{

if (current.state==3)

a.state = 20;

else if (current.state==4)

a.state = 22;

 

a.update();

}


Please mark this response as correct or helpful if it assisted you with your question.

Hello Sanjiv,

I have a similar situation the difference is that I am not extending to any table. I have two tables and one is related via a reference field in the child table (B) to relate to main table (A). The state fields on both are custom and the values are approve or reject. I tried the solution above and what the BR on the child table does is to write the value set as a new value into the choice list of the childs status and does not sets the status value of the main record status. Is there something I need to do different on this BR?

 

Will greatly appreciate your help.