- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 09:42 AM
I am struggling to get dot walking in a query business rule.
For a given role, I'm trying to show only HR tasks when the parent.hr_service cases is a desired Service.
This is a little example of the syntax I tried but it doesn't appear to work in a query business rule. <sys_id> in my query is actually the sys_id of the HR Service.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 10:54 AM
Dot-walking doesn't seem to work, at least in this case. Here's a workaround for the Query Business Rule script:
function executeRule(current, previous /*null when async*/) {
var resultsArr = [];
var parGr = new GlideRecord('sn_hr_core_task');
parGr.query();
while (parGr.next()) {
if (parGr.parent.hr_service == '9e28cde49f331200d9011977677fcf00') {
resultsArr.push(parGr.sys_id.toString());
}
}
current.addQuery('sys_id', 'IN', resultsArr.join(','));
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 10:47 AM
Hi @abrahams Try below code and modify accordingly
var hrServiceSysId = current.sys_id;
var gr = new GlideRecord('hr_task');
gr.addQuery('parent.hr_service', hrServiceSysId);
gr.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 10:54 AM
Dot-walking doesn't seem to work, at least in this case. Here's a workaround for the Query Business Rule script:
function executeRule(current, previous /*null when async*/) {
var resultsArr = [];
var parGr = new GlideRecord('sn_hr_core_task');
parGr.query();
while (parGr.next()) {
if (parGr.parent.hr_service == '9e28cde49f331200d9011977677fcf00') {
resultsArr.push(parGr.sys_id.toString());
}
}
current.addQuery('sys_id', 'IN', resultsArr.join(','));
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 11:09 AM
For some reason, I didn't think we should use GlideRecord in a query business rule but this does work. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2024 11:01 AM
Hi @abrahams ,
In a Business Rule, you can use dot-walking to query related tables.
(function executeRule(current, previous /*null when async*/) {
// Instantiate a GlideRecord object for the HR table
var grHR = new GlideRecord('hr_case');
// Query the HR table with dot-walked conditions
grHR.addQuery('assigned_to', gs.getUserID()); // Assigned to me
grHR.addOrCondition(grHR.parent.hr_service=='xxxx'); // hr service sysid
grHR.query();
while (grHR.next()) {
// Process each record
gs.info('Found HR Case with number: ' + grHR.number);
}
})(current, previous);