Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Prevent Incident Resolution without KB attached or draft created

Alex Saager1
Tera Contributor

Hi community,

 

I'm trying to develop/test a step in our incident management process where an incident cannot be resolved unless a KB article is attached or a draft created and I've tried this using a business rule, but the incident is still resolving and no message is displayed, can anyone advise where I'm going wrong?

 

Many thanks,

Alex

 

Here is the business rule condition:

 

AlexSaager1_0-1744804159429.png

 

Script:

 

(function executeRule(current, gSNC, gUser, gSNCSession) {
    // Define the state value for "Resolved" (adjust if necessary)
    var RESOLVED = '6';
   
    // Check if the state is changing to "Resolved"
    if (current.state.changesTo(RESOLVED)) {
       
        // Check if a Knowledge Article is attached via the m2m relationship
        var hasKnowledgeAttached = false;
        var gr = new GlideRecord('m2m_kb_task'); // This links KBs to tasks like incidents
        gr.addQuery('task', current.sys_id);
        gr.query();
       
        // If a knowledge article is attached, allow resolution
        if (gr.hasNext()) {
            hasKnowledgeAttached = true;
        }
       
        // Alternatively, check if there is a draft KB article
        var hasDraftKB = false;
        var draftGr = new GlideRecord('kb_knowledge'); // Check for draft knowledge articles
        draftGr.addQuery('state', 'draft'); // Assuming 'draft' state is used for drafts
        draftGr.addQuery('incident', current.sys_id); // Link to incident, adjust if necessary
        draftGr.query();
       
        if (draftGr.hasNext()) {
            hasDraftKB = true;
        }

        // If neither a KB nor a draft article exists, prevent resolution and show an error
        if (!hasKnowledgeAttached && !hasDraftKB) {
            gs.addErrorMessage('You must attach a Knowledge Article or create a draft before resolving this incident.');
            current.setAbortAction(true);
        }
    }
})(current, gSNC, gUser, gSNCSession);

 

1 REPLY 1

Robert H
Mega Sage

Hello @Alex Saager1 ,

 

I'm not sure if you are using any additional plugins, but OOTB a Knowledge Article does not have a field named "state". Please replace this with "workflow_state".

An article also does not have an "incident" field. The only OOTB field that can reference a Task record is called "source".

 

You're also using unusual IIFE parameters in your script: typically a BR script looks like this:

 

(function executeRule(current, previous /*null when async*/) {

...

})(current, previous);

 

Regards,

Robert