Update parent record status when the child status changes in scope app

lrossy31
Tera Expert

Hello,

I have a situation where I have two tables and one is related via a reference field in the child table (B) to relate to main table (A), and I am not extending to any table. The state fields on both are custom and the values are pending, approve, and reject. I tried the solution below 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(parent) record status. Is there something I need to do different on this BR?

 

created an onAfter Update Business Rule for this on Table B.

with condition state Changes to Approved

 

var a = new GlideRecord('A');

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

a.query();

 

if (a.next())

{

if (current.state==pending)

a.state = approve;

else if (current.state==reject)

a.state = reject;

 

a.update();

}

 

Will greatly appreciate any assistance.

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

Hi @lrossy31 ,
I trust you are doing great.

Based on the information you have provided, it appears that you are trying to update the state field in the parent table (A) based on changes to the state field in the child table (B). However, the code you provided is updating the state field in the child table rather than the parent table.

To update the state field in the parent table, you can modify your code as follows:

  1. Replace "current.state" with "current.getValue('state')" to get the current state value from the child table.

  2. Replace "a.state" with "a.setValue('state', new_state)" to set the state value in the parent table.

  3. Replace "approve" and "reject" with "2" and "3", respectively, since the state field in ServiceNow is a choice list and the values are stored as integers.

The updated code should look like this:

var parent = new GlideRecord('A');
parent.addQuery('sys_id', current.a_reference_field);
parent.query();

if (parent.next()) {
  var new_state = current.getValue('state');
  
  if (new_state == 'pending') {
    parent.setValue('state', 1 /* pending */);
  } else if (new_state == 'approve') {
    parent.setValue('state', 2 /* approve */);
  } else if (new_state == 'reject') {
    parent.setValue('state', 3 /* reject */);
  }
  
  parent.update();
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

8 REPLIES 8

Amit Gujarathi
Giga Sage
Giga Sage

Hi @lrossy31 ,
I trust you are doing great.

Based on the information you have provided, it appears that you are trying to update the state field in the parent table (A) based on changes to the state field in the child table (B). However, the code you provided is updating the state field in the child table rather than the parent table.

To update the state field in the parent table, you can modify your code as follows:

  1. Replace "current.state" with "current.getValue('state')" to get the current state value from the child table.

  2. Replace "a.state" with "a.setValue('state', new_state)" to set the state value in the parent table.

  3. Replace "approve" and "reject" with "2" and "3", respectively, since the state field in ServiceNow is a choice list and the values are stored as integers.

The updated code should look like this:

var parent = new GlideRecord('A');
parent.addQuery('sys_id', current.a_reference_field);
parent.query();

if (parent.next()) {
  var new_state = current.getValue('state');
  
  if (new_state == 'pending') {
    parent.setValue('state', 1 /* pending */);
  } else if (new_state == 'approve') {
    parent.setValue('state', 2 /* approve */);
  } else if (new_state == 'reject') {
    parent.setValue('state', 3 /* reject */);
  }
  
  parent.update();
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hello Amit,

 Thank you for sending me this. It is still not working and when I do a check this line comes up with a null.

 

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

 

I tried the reference field in the child table and also the number field from the parent table.

Is this the reference field in the child table that I have made the relationship to the parent table?

Never mind Amit. I got it working with what you sent me. Really appreciate it. I will set your answer as correct.

Hi @Amit Gujarathi ,

 

I have similar kind of requirement, I tried the above code but that's not working. Can you please have a look on the code and correct me.

 

Scenario: I need to put short description of Group Approval[sysapproval_group] into Approval[sysapproval_approver] comments.

 

Approval[sysapproval_approver] have 'group' field. And my BR is after insert on Approval[sysapproval_approver] table with below code.

 

 

 

function executeRule(current, previous /*null when async*/ ) {
    gs.addInfoMessage('start br', current.getValue('group'));
    var groupApprovalRec = new GlideRecord('sysapproval_group');
    groupApprovalRec.addQuery('sys_id', current.group);
    groupApprovalRec.query();

    if (groupApprovalRec.next()) {

        var groupShortDesc = groupApprovalRec.getValue('short_description');
        gs.addInfoMessage('short desc is ', groupShortDesc);
        current.setValue('comments', groupShortDesc);
        current.setValue('comments', "hello");
        current.update();
    }

})(current, previous);

 

 

 

I'm only getting the first message "start br" and not  current.getValue('group') value at all.

 

Note: both the tables are OOTB

 

 

Thanks in advance:)