I cannot pass KB search result via Scripted rest API

naxis alx
Tera Contributor

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;
}
4 REPLIES 4

Kieran Anson
Kilo Patron

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. 

@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?

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

KieranAnson_0-1715088366510.png

 

@Kieran Anson thanks again.

I tried but no chance.

Maybe I should also check the ACL on the KB table

Screenshot 2024-05-08 at 16.28.36.png