Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

How to Retrieve KB sys_id from AI Search in SNVA for Custom Processing?

karanrautel
Tera Contributor

I have a script that accepts a Knowledge Base (KB) article sys_id as input, generates an AI-powered summary of the KB article, and returns the summarized output.

I want to integrate this functionality with the prebuilt ServiceNow Virtual Agent (SNVA). My objective is:

  1. When a user submits a query through the Virtual Agent, allow the built-in AI Search to identify the most relevant KB article.

  2. Retrieve the sys_id of the KB article which was selected by the user among the KB's returned by AI Search.

  3. Call my script in the pre existing flow of the AI search after retreiving the sys_id.

  4. Generate an AI summary of the KB article using my existing script.

  5. Display the generated summary as part of the Genius Results response in the Virtual Agent, instead of (or alongside) the default KB content.

I am looking for the best approach to capture the KB sys_id from the AI Search results within the prebuilt SNVA flow and invoke my custom summarization process before rendering the final response in Genius Results.

1 REPLY 1

Abhishek Pal
Mega Guru

Hi @karanrautel ,

Do not modify the OOB AI Search Fallback topic or the Run AI Search topic block.

The supported approach depends on whether you want to summarize the top-ranked Knowledge article or the article that the user explicitly selects.

Option 1: Summarize the top-ranked Knowledge article before displaying the Genius Result

Create a custom Genius Result configuration and use an AI Search Response Processor.

The response processor runs after AI Search calculates the results. You can retrieve the matched documents by using:

context.getMatchingDocuments()

Each returned document contains values such as:

sys_id
.table
.title
.url
.score

The sys_id property is the sys_id of the matched Knowledge article.

Configuration steps:

1. Navigate to:

AI Search > Search Experience > Search Profiles

2. Open the Search Profile used by the Virtual Agent chat experience.

3. In the Genius Results related list, select:

Create and Link

4. Configure:

Name:
Custom KB Summary Genius Result

Trigger condition:
Always

Active:
True

5. Add the following AI Search Response Processor script:

function process(context) {
var answer = new sn_ais.GeniusResultAnswer();
var documents = context.getMatchingDocuments() || [];

for (var i = 0; i < Math.min(documents.length, 3); i++) {
var document = documents[i];

if (!document.sys_id)
continue;

var kb = new GlideRecordSecure('kb_knowledge');

if (!kb.get(document.sys_id) || !kb.canRead())
continue;

// Replace this with your existing Script Include.
var summary = new x_your_scope.KBArticleSummary()
.summarize(kb.getUniqueValue());

if (!summary)
continue;

answer.addDataMap({
title: String(
document['.title'] ||
kb.getDisplayValue('short_description')
),
summary: String(summary),
url: String(document['.url'] || ''),
sys_id: String(kb.getUniqueValue()),
table: 'kb_knowledge'
});

// Return only the highest-ranked readable KB article.
break;
}

return answer;
}

Replace:

x_your_scope.KBArticleSummary

with the name of your existing scoped Script Include.

6. Configure Return fields as:

title,summary,url,sys_id,table

7. Configure or copy the relevant Virtual Agent EVAM view configuration and map:

Title:
title

Body or description:
summary

Link:
url

This allows the generated summary and source article link to appear on the Genius Result card.

Important limitation:

The AI Search Response Processor runs before the user clicks or selects a search result.

Therefore, this approach summarizes the highest-ranked Knowledge article returned by AI Search. It cannot determine which result the user will select later.

Option 2: Summarize the article explicitly selected by the user

If the requirement is to summarize the exact article selected from multiple results, create a custom Virtual Agent topic or topic block.

The flow should be:

User query
-> Execute search
-> Display Knowledge results
-> User selects an article
-> Store the selected KB sys_id in a topic variable
-> Call the summary Script Include or Flow Action
-> Display the generated summary
-> Provide a link to the complete article

The OOB Run AI Search topic block does not provide a documented output containing the sys_id of the result clicked by the user. Do not use DOM manipulation or modify the OOB AI Search topic to intercept the click.

If a custom topic block is created, define an output variable such as:

selected_kb_sys_id

Pass that value to your Script Action or Flow Action:

(function execute() {
var kbSysId = vaVars.selected_kb_sys_id;

return new x_your_scope.KBArticleSummary()
.summarize(kbSysId);
})();

Security and performance considerations:

- Process only Knowledge records returned by AI Search.
- Use GlideRecordSecure for subsequent record access.
- Test with Knowledge Base user criteria and non-admin users.
- Do not expose article content that the current user cannot read.
- Ensure the summary Script Include is accessible from the Genius Result configuration scope.
- Avoid long synchronous external REST or LLM calls inside the response processor because they can slow down every search.
- Consider caching summaries when article content has not changed.
- Verify any Generative AI licensing and data-governance requirements.

Before building the custom summarization, also review Now Assist Q&A Genius Results or Multi-Content Response Genius Results. These OOB capabilities already generate grounded answers from relevant Knowledge content and may satisfy the requirement without custom scripting, subject to licensing.

Recommended decision:

Summarize the highest-ranked KB automatically
-> Custom Genius Result Response Processor

Summarize the exact KB selected by the user
-> Custom Virtual Agent topic/topic block with selected_kb_sys_id output

Do not customize the OOB AI Search Fallback topic or Run AI Search topic block unless there is no supported extension option.

Official references:

GeniusResultContext API:
https://www.servicenow.com/docs/r/api-reference/server-api-reference/GeniusResultContextScopedAPI.ht...

GeniusResultAnswer API:
https://www.servicenow.com/docs/r/api-reference/server-api-reference/GeniusResultAnswerScopedAPI.htm...

Create a Genius Result configuration:
https://www.servicenow.com/docs/r/platform-administration/ai-search/create-genius-results-config-ais...

Virtual Agent and AI Search:
https://www.servicenow.com/docs/r/conversational-interfaces/virtual-agent/va-ai-search.html

Now Assist in AI Search:
https://www.servicenow.com/docs/r/platform-administration/ai-search/now-assist-ais.html

Hope this helps!

If this response helped, please mark it as Helpful.
If it resolves your issue, please Accept it as Solution.

Kind Regards,
Abhishek Pal