How to correctly query the related list of a record?

Jack Hoblyn
Tera Contributor

I'm trying to iterate through each item in the related list of a record to check if a certain value is present but am getting stuck at the first hurdle

I have a related list of locations on the incident table.

 

find_real_file.png

This table seems to use a field called Task to reference the Incident.

 

find_real_file.png

 

I have a Business Rule which is trying to iterate through the list to query some of the columns and vales, but it doesn't seem to be outputting anything in the log.

 

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

gs.log('stage 1');
var gr = new GlideRecord('task_location');

gr.addQuery('task', current.sys_id);
gr.query();

if (gr.hasNext()) {
gs.info('stage 2');
gs.info(gr.location.sys_id);
gs.info(gr.incident.sys_id);
}

})(current, previous);

 

What seems to be the issue at hand here?

Is there a better way to go about this?

 

find_real_file.png

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hey,

Try updating it a little bit:


var gr = new GlideRecord('task_location');
gr.addQuery('task', current.getUniqueValue());
gr.query();
while (gr.next()) {
gs.info('stage 2');
gs.info(gr.getValue("location"));// this will give you sys_id of location, if you want name try gr.location.name.toString()
gs.info(gr.getValue("task"));// its a task field, not incident
}

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

View solution in original post

3 REPLIES 3

Aman Kumar S
Kilo Patron

Hey,

Try updating it a little bit:


var gr = new GlideRecord('task_location');
gr.addQuery('task', current.getUniqueValue());
gr.query();
while (gr.next()) {
gs.info('stage 2');
gs.info(gr.getValue("location"));// this will give you sys_id of location, if you want name try gr.location.name.toString()
gs.info(gr.getValue("task"));// its a task field, not incident
}

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Worked a charm!

 

Thanks 🙂 

Awesome 🙂

Best Regards
Aman Kumar