I cannot pass KB search result via Scripted rest API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2024 07:27 PM
I wrote a script include to make a search on KB. I want to pass the result to my slack app via a scripted rest api.
the Slack blocks is fine when I execute in "All > scripts - Background" but the KB result part is empty when I pass it via Scripted rest api.
is there any restriction to pass KB feeds via API?
This is the code
searchKB: function(keyword, user){
// Search KB
var searchText = keyword;
var windowStart = 0;
var windowEnd = 10;
var additionalFields = ["short_description"];
//Additional filter conditions can be added to filter records.
var req = {
keyword: searchText,
resource: 'Knowledge',
start: windowStart,
end: windowEnd,
//order : "relevancy,true",
knowledge_fields: additionalFields,
};
var res = new KBPortalServiceImpl().getResultData(req);
var blocks = [];
blocks.push({
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hello " + user
}
});
blocks.push({
"type": "divider"
});
res.results.forEach(function(kb) {
//gs.debug(kb.meta.short_description.display_value);
blocks.push({
"type": "section",
"text": {
"type": "mrkdwn",
"text": kb.meta.short_description.display_value
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "open"
},
"url": "https://xxxxxxxxx.service-now.com/esc" + kb.link
}
});
blocks.push({"type": "divider"});
});
blocks = JSON.stringify(blocks);
return blocks;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2024 09:21 PM
Hi,
The issue will be with the following line as the underlining SNC.SearchRequest API runs as the user context by default. So when running via Scripted API, are you running as your admin user for testing or the Slack credentials?
var res = new KBPortalServiceImpl().getResultData(req);
If you want to search as a specific user, you'd have to create your own version of KBPortalServiceImp whereby the payload contains a key of meta.runAsUser with the sysId of the user to run as.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2024 11:29 PM
@Kieran Anson thank you.
I run as admin in scripts - background and it is OK but via Slack I call the Scripted rest api and it is empty?
How can I set the meta.runAsUser?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 06:26 AM
I run as admin in scripts - background and it is OK but via Slack I call the Scripted rest api and it is empty? - Yes as your admin users have different permissions
In the script include you can see an object variable (as below), you'd need to copy the logic from the script include and add in the addition key into the meta object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2024 12:54 AM