I want to implement a case in which incident should not get saved unitl and unless a kb is attached

PriyanshuS64936
Giga Contributor
//This should be only when assignment group is service desk.
//Here's the code I have written but when using this , it is throwing the error even after attaching the kb
 
 
function onSubmit() {
var kb=g_form.getValue('knowledge_base');
var assignmentGroup = g_form.getDisplayValue('assignment_group');

if (assignmentGroup && assignmentGroup.toLowerCase() ==='service desk'){
if (!kb){
g_form.addErrorMessage('You must attach a <b>Knowledge Base</b> before saving this incident');
return false;
}
}
return true;
}
 
 
1 REPLY 1

Bhavya11
Kilo Patron
Kilo Patron

Hi @PriyanshuS64936 ,

 

Your logic to check if Assignment Group is Service Desk then check if KB is attached or not then  show error message is not quite right.

 

so try Business rule on incident table .

When to run: Before, Insert and Update true

Condition: Assignment Group is Service Desk or Assignment Group changes to Service Desk and State is No closed or Resolved

Bhavya11_0-1759134929871.png

 

Advance:

	var kbAttached = false;
        
        var grKB = new GlideRecord('m2m_kb_task');
        grKB.addQuery('task', current.sys_id);
        grKB.query();
        
        if (grKB.hasNext()) {
            kbAttached = true;
        }
        
        // If no KB is attached, abort the action and show an error message
        if (!kbAttached) {
            current.setAbortAction(true);
            gs.addErrorMessage('You must attach at least one Knowledge Article before setting the incident to ' + current.state.getDisplayValue() + '.');
        }
    

 

 

If this information proves useful, kindly mark it as helpful or accepted solution.

 

Thanks,

BK