AISASearch-based refineQuery not filtering results in Relationship script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi all,
I’m trying to enhance the “Similar Tickets” functionality in our custom app (incident) by replacing the contextual search (IR_OR_QUERY) with AISASearch for better and more relevant results.
The goal is:
When a user opens a incident record, the relationship should display other cases with similar short descriptions — basically, AI-based contextual matching.
Table: incident
Relationship Type: Defined via Relationship Script (refineQuery)
Applies To Table: incident
Script Include: AISASearchUtil (standard ServiceNow AI Search)
Expected behavior: Display top 10 relevant incident records based on AISASearch results.
Issue: With AISASearch, logs are printed correctly (sys_ids found, encodedQuery applied) but the related list shows no records.
(function refineQuery(current, parent) {
var searchTerm = parent.short_description || '';
var targetTable = current.getTableName();
gs.log('*** refineQuery started for parent: ' + parent.number + ', searchTerm: ' + searchTerm + ', targetTable: ' + targetTable);
var data = {
rpSysId: '5da8bd1267a21010b3d782f45685ef97', //aisa_rp_config - Record Producer Configurations (Sys_id of Search Results)
searchContextConfigId: 'f32b42a447017e1089cd8311236d43b5', //sys_search_context_config - Search Applications Configuration
searchTerm: searchTerm
};
var result = new global.AISASearchUtil().search(data);
var sysIds = [];
if (result && result.data && result.data.search && result.data.search.searchResults) {
var results = result.data.search.searchResults;
gs.log('*** refineQuery | total results from AISASearch: ' + results.length);
for (var i = 0; i < results.length; i++) {
var item = results[i];
var id = (item.sysId || '').toString().trim();
var tbl = (item.table || '').toString().toLowerCase().trim();
if (id && (tbl === '' || tbl === targetTable.toLowerCase())) {
sysIds.push(id);
}
}
}
sysIds = sysIds.filter(function (x, i, a) { return x && a.indexOf(x) === i; });
gs.log('*** refineQuery | valid sysIds: ' + sysIds.join(','));
if (sysIds.length > 0) {
// Force GlideRecord evaluation before applying
var tempGR = new GlideRecord(targetTable);
tempGR.addEncodedQuery('sys_idIN' + sysIds.join(','));
tempGR.query();
var validIds = [];
while (tempGR.next()) {
validIds.push(tempGR.sys_id.toString());
}
if (validIds.length > 0) {
var encoded = 'sys_idIN' + validIds.join(',');
current.addEncodedQuery(encoded);
gs.log('*** refineQuery | Applied FINAL encodedQuery after force-eval: ' + encoded);
} else {
gs.log('*** refineQuery | No validIds found after force evaluation');
current.addEncodedQuery('sys_idISEMPTY');
}
} else {
gs.log('*** refineQuery | No valid sysIds found, returning empty result');
current.addEncodedQuery('sys_idISEMPTY');
}
})(current, parent);Logs:-
*** refineQuery started for parent: INC0747059, searchTerm: FR and RT check - 7, targetTable: incident
*** refineQuery | total results from AISASearch: 10
*** refineQuery | valid sysIds: 3afcf2c71bdca650b0b95244604bcb1e,161db6c71bdca650b0b95244604bcbbb,...
*** refineQuery | Applied encodedQuery: sys_idIN3afcf2c71bdca650b0b95244604bcb1e,161db6c71bdca650b0b95244604bcbbb,...
