has anyone implemented sn_search.ScriptableSearchAPI?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
we are looking into enabling a form of search via API and i ran into sn_search.ScriptableSearchAPI from this post.
was wondering if anyone has done something similar to what we are looking for.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@spaceman2023 As per my understanding, here is my thoughts:
Yes, sn_search.ScriptableSearchAPI is the recommended OOB API if you want to leverage ServiceNow AI/Search framework programmatically instead of querying individual tables.
It works well for searching across indexed sources such as Knowledge, Catalog, and other configured searchable tables, while respecting ACLs and search configurations.
If your requirement is to expose search externally via an API, a common approach is:
-
Create a Scripted REST API
-
Invoke
sn_search.ScriptableSearchAPIwithin the resource script -
Return the search results in the desired JSON format
If your use case is simply searching records from a specific table, a standard Table API or Scripted REST API using GlideRecord/GlideQuery is often simpler and more performant.
Recommendation: Use ScriptableSearchAPI only when you need the platform's unified search capabilities (relevance ranking, indexing, ACL-aware results). Otherwise, use the Table API for targeted table searches.
Hope this helps.
✅ Issue resolved? → Mark as Correct
Found value? → Mark as Helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You can pretty much copy the ai search assist api which uses the AISASearchUtil script include.
You can test this in a bg script, it's essentially copied from the si and uses the oob default sp search config.
var aisScriptableAPI = new sn_search.ScriptableSearchAPI();
var req = {
searchContextConfigId: '00731b9d5b231010d9a5ce1a8581c7dd',
searchTerm: 'outlook',
paginationToken: '',
disableSpellCheck: false,
facetFilters: [],
searchFilters: [],
requestedFields: {}
};
var aisResponse = aisScriptableAPI.search(req.searchContextConfigId, req.searchTerm, req.paginationToken, req.disableSpellCheck, req.facetFilters, req.searchFilters, req.requestedFields);
var searchResultObj = {};
if (aisResponse) {
searchResultObj.term = aisResponse.getTerm();
var searchResults = aisResponse.getSearchResults();
var resultsArr = [];
for (var i = 0; i < searchResults.length; i++) {
var currResultTable = searchResults[i].getTable();
if (currResultTable === "sys_attachment")
continue;
var singleResult = {};
singleResult["sysId"] = searchResults[i].getSysId();
singleResult["table"] = currResultTable;
singleResult["text"] = searchResults[i].getText();
singleResult["title"] = searchResults[i].getTitle();
var columnsArr = searchResults[i].getColumns();
var columns = [];
for (var j = 0; j < columnsArr.length; j++) {
var singleColumn = {};
singleColumn["fieldName"] = columnsArr[j].getFieldName();
singleColumn["label"] = columnsArr[j].getLabel();
singleColumn["value"] = columnsArr[j].getValue();
singleColumn["displayValue"] = columnsArr[j].getDisplayValue();
columns.push(singleColumn);
}
singleResult["columns"] = columns;
resultsArr.push(singleResult);
}
searchResultObj.searchResults = resultsArr;
searchResultObj.count = resultsArr.length;
}
gs.info(JSON.stringify(searchResultObj, null, 2));