How to create KB article by using Now Assist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi All,
When P1 or P2 incidents gets resolved automatically KB article should be created By using custom prompt from Business rule in Zurich.
Kindly share some suggestions/idea.
We have created some BR to achieve this, unlucky this BR not working .
Kindly share with your suggestions/inputs on this.
Table : Incident
Condition : after
State changed To Resolved
Priority is P1 or P2
(function executeRule(current, previous) {
try {
// ----------------------------------------
// 1. PREVENT DUPLICATE KB CREATION
// ----------------------------------------
if (!gs.nil(current.u_kb_article)) {
gs.info('[KB Auto-Gen] KB already exists for: ' + current.getValue('number'));
return;
}
// ----------------------------------------
// 2. FETCH FRESH INCIDENT VIA GLIDERECORD
// ----------------------------------------
var sysId = current.getValue('sys_id');
var incGR = new GlideRecord('incident');
incGR.addQuery('sys_id', sysId);
incGR.query();
if (!incGR.next()) {
gs.error('[KB Auto-Gen] GlideRecord fetch failed for sys_id: ' + sysId);
return;
}
gs.info('[KB Auto-Gen] Fetched incident: ' + incGR.getValue('number'));
// ----------------------------------------
// 3. BUILD PROMPT INPUT PAYLOAD
// ----------------------------------------
var promptInput = {
number: incGR.getValue('number') || '',
priority: incGR.priority.getDisplayValue() || '',
short_description: incGR.getValue('short_description') || '',
description: incGR.getValue('description') || '',
close_notes: incGR.getValue('close_notes') || '',
resolved_at: incGR.resolved_at.getDisplayValue() || '',
cmdb_ci: incGR.cmdb_ci.nil() ? '' : incGR.cmdb_ci.getDisplayValue(),
assignment_group: incGR.assignment_group.nil() ? '' : incGR.assignment_group.getDisplayValue(),
resolved_by: incGR.resolved_by.nil() ? '' : incGR.resolved_by.getDisplayValue()
};
gs.info('[KB Auto-Gen] Prompt input: ' + JSON.stringify(promptInput));
// ----------------------------------------
// 4. LOOK UP NOW ASSIST PROMPT BY NAME
// ----------------------------------------
var promptGR = new GlideRecord('sys_hub_action_type_definition');
promptGR.addQuery('name', 'KB Generation P1 Change Incident');
promptGR.setLimit(1);
promptGR.query();
if (!promptGR.next()) {
gs.error('[KB Auto-Gen] Now Assist Prompt not found: "KB Generation P1 Change Incident"');
return;
}
var capabilityId = promptGR.getValue('sys_id');
gs.info('[KB Auto-Gen] Prompt sys_id (capabilityId): ' + capabilityId);
// ----------------------------------------
// 5. CALL NOW ASSIST SKILL
// ----------------------------------------
var request = {
executionRequests: [{
payload: promptInput,
capabilityId: capabilityId
}],
mode: 'sync'
};
var response = sn_one_extend.OneExtendUtil.execute(request);
gs.info('[KB Auto-Gen] Raw skill response: ' + JSON.stringify(response));
// ----------------------------------------
// 6. VALIDATE RESPONSE
// ----------------------------------------
if (!response || !response.capabilities || !response.capabilities[capabilityId]) {
gs.error('[KB Auto-Gen] Invalid or missing capability in response for: ' + incGR.getValue('number'));
return;
}
var aiResponse = response.capabilities[capabilityId].response;
gs.info('[KB Auto-Gen] AI response: ' + aiResponse);
if (gs.nil(aiResponse)) {
gs.error('[KB Auto-Gen] Empty AI response for: ' + incGR.getValue('number'));
return;
}
// ----------------------------------------
// 7. PARSE AI RESPONSE (handles JSON wrap)
// ----------------------------------------
var kbText = aiResponse;
try {
var parsed = JSON.parse(aiResponse);
if (parsed && parsed.model_output) {
kbText = parsed.model_output;
}
} catch (parseErr) {
// Plain text response — use as-is
}
gs.info('[KB Auto-Gen] KB text (first 300): ' + kbText.substring(0, 300));
// ----------------------------------------
// 8. CREATE KB ARTICLE
// ----------------------------------------
var kb = new GlideRecord('kb_knowledge');
kb.initialize();
kb.setValue('short_description', 'P1 RCA - ' + promptInput.short_description);
kb.setValue('text', kbText);
kb.setValue('kb_knowledge_base', 'c7190f9d87384710158b62083cbb3583'); // your KB base sys_id
kb.setValue('workflow_state', 'draft');
kb.setValue('author', gs.getUserID());
var kbSysId = kb.insert();
if (!kbSysId) {
gs.error('[KB Auto-Gen] KB insert failed for: ' + promptInput.number +
' — check ACLs and mandatory fields');
return;
}
gs.info('[KB Auto-Gen] KB article created: ' + kbSysId);
// ----------------------------------------
// 9. UPDATE INCIDENT WORK NOTES
// ----------------------------------------
var kbGR = new GlideRecord('kb_knowledge');
if (kbGR.get(kbSysId)) {
current.work_notes =
'KB Article Auto-Generated:\n' +
'KB Number: ' + kbGR.getValue('number') + '\n' +
'Title: ' + kbGR.getValue('short_description') + '\n' +
'State: ' + kbGR.getValue('workflow_state') + '\n' +
'Link: /kb_view.do?sys_kb_id=' + kbSysId;
}
current.setWorkflow(false);
current.update();
gs.info('[KB Auto-Gen] Incident updated. Done.');
} catch (ex) {
gs.error('[KB Auto-Gen] Exception: ' + ex.message + ' | Stack: ' + ex.stack);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @dkishore090
You have put lots of gs info and error message . So share us what error /error msg it is throwing/showing.
what is last info message it has printed.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @dkishore090 ,
I hope you're going through the ServiceNow documentation.
You don't need to pass the inputs from the Business Rule script. You can leverage the out-of-the-box (OOB) Now Assist KB Generation skill and invoke that skill directly from your Business Rule based on the required conditions.
Before integrating the skill with your Business Rule, make sure to test it thoroughly within the Skill Kit itself. Verify that the skill is generating the expected response and returning the correct output. Once you've confirmed that the skill is working as expected, you can proceed with calling it from the Business Rule.
Please Refer this doc: https://www.servicenow.com/docs/r/servicenow-platform/now-assist-in-knowledge-management/Now-Assist-...
Thanks,
Akash Desai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
For this use case, I would not recommend creating the KB article directly from a synchronous Business Rule.
Better approach:
- Business Rule should only detect the condition
- Trigger an Event or Flow
- Flow/Script Action should create the KB article asynchronously
Condition:
State changes to Resolved
Priority is 1 or 2
Recommended Approach
Create an After Business Rule on Incident:
if (current.state.changesTo('6') && (current.priority == '1' || current.priority == '2')) {
gs.eventQueue('incident.create.kb.article', current, current.sys_id, current.number);
}
Then create a Script Action or Flow Designer flow for this event and create the KB article there.
Example KB creation logic:
var kb = new GlideRecord('kb_knowledge');
kb.initialize();
kb.short_description = 'Knowledge article for ' + current.number + ' - ' + current.short_description;
kb.text = 'Issue: ' + current.short_description +
'\n\nDescription: ' + current.description +
'\n\nResolution: ' + current.close_notes;
kb.kb_knowledge_base = '<knowledge_base_sys_id>';
kb.kb_category = '<category_sys_id>';
kb.workflow_state = 'draft';
kb.insert();
Note: For P1/P2 incidents, keep human review before publishing because incident data may contain sensitive or incomplete RCA details.
✅ Issue resolved? → Mark as Correct
Found value? → Mark as Helpful