- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2023 09:00 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2023 10:07 AM
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:
Replace "current.state" with "current.getValue('state')" to get the current state value from the child table.
Replace "a.state" with "a.setValue('state', new_state)" to set the state value in the parent table.
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2023 09:07 AM
It looks like you did not declare pending or reject. But I'm going to assume they were supposed to be strings so you should have.
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();
}
//Another way to do this is
if(!current.parent_field)
return;
var a = current.parent_field.getRefRecord();
if (current.state == "pending"){
a.state = "approve";
} else if (current.state == "reject"){
a.state = "reject";
}
a.update();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2023 09:09 AM
Oh and if you still see a new value in the state field on the parent record that would mean that the value you used ("approved", "reject") do not exist as choice values for the field on the parent record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2023 09:31 AM
Hello Drew,
Both status fields are choice fields. I tried both solutions and what it updates is the child table (B) and adds the Approve value as a new choice in the list as it does not have that choice which it does but it should be updating the parent record instead of the child record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2023 11:21 AM
Sounds like you are not setting the values correctly. You need to check the choice field values to make sure that you are using the right VALUE, not the LABEL. As @Amit Gujarathi suggested the values are probably numbers and not strings like you have. If the wrong record is being updated then I would say you have something wrong with the code and you are going to need to post all of the code in the BR for someone to help you because the code I posted is not going to update current.