How to create KB article by using Now Assist
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour 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);
}
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour 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.
Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti