How to know if related list is not empty and automatically update the field on the parent form

ss123
Tera Contributor

Hi! 

We have a requirement where we want to query the related list in Table_1 and if there' at least one record in Related_List_1, the Select Box field value will be changed in Table_1 form

 

Sample Details:

table_1 = is the current form

relate_list_1 = is the related list on the Table_1

Type = is the select box field (Choices: One , Two)

 

Scenario:

If the related_list_1 has at least 1 record in table_1 form, the Select box = 'One'

 

Thank you in Advance!

13 REPLIES 13

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @ss123 

You can take the reference of below code:- In that code I'm checking if incident has any incident task associated with it or not from related list. incident task is related list. 

Business Rule : Before Update

Condition as per your conditions

(function executeRule(current, previous /*null when async*/) {
// Add your code here
var incTask = new GlideRecord('incident_task');
incTask.addQuery('parent', current.sys_id);
incTask.query();
if(incTask.next()){
current.select_box_name='One'; //Change field backend name and value
//gs.addErrorMessage('Not allowed');
//current.setAbortAction(true);
}
})(current, previous);  

  


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi @Gunjan Kiratkar thanks. I modified the BR Script but it seems the if statement is not working?

 

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

var vtbcard = new GlideRecord('vtb_card');
vtbcard.addQuery('parent', current.sys_id);
vtbcard.query();
if (vtbcard.next()) {
var servtask = current.u_choice_2;
servtask = 'no';
} else
{
servtask = 'yes'
}
//current.setAbortAction(true);

//Change field backend name and value
//gs.addErrorMessage('Not allowed');
//current.setAbortAction(true);


})(current, previous);

 

The reason behind this is we want to update the Select box field (u_choice_2)

If vtb_card related list has any record, the (u_choice_2) = No

else if vtb_card related list has NO record, the (u_choice_2) = Yes

 

Hi @ss123 ,

You are not setting the values to u_choice_2 in your code.

Try below code:- Before Update BR

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

var vtbcard = new GlideRecord('vtb_card');
vtbcard.addQuery('parent', current.sys_id);
vtbcard.query();
if (vtbcard.next()) {
current.setValue('u_choice_2','no');

} else
{
current.setValue('u_choice_2','yes');
}
})(current, previous);

  


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi @Gunjan Kiratkar 

 

Further details is that the related list has a field called "Task" and that field references the current form number. How do we update the BR Script ?

 

I modified the script you provided and it only changes to "Yes"

 

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

var vtbcard = new GlideRecord('vtb_card');
vtbcard.addQuery('task', current.sys_id);
vtbcard.query();
if (vtbcard.next()) {
current.setValue('u_choice_2', 'no');

} else {
current.setValue('u_choice_2', 'yes');
}
})(current, previous);

 

SabrinaSalazar_0-1668570821907.pngSabrinaSalazar_1-1668570867243.png

 

Thanks.