"Create problem ticket when multiple incident created with same issue"

mujahidhussain
Tera Contributor

Dear all,

 

i have a requirement "Create problem ticket (automation) when multiple incident created with same issue".

 

i tried working with business rule, after 'Insert' or 'updated'

with the following code but it is not working.

 

(function executeRule(current, previous) {


var category = current.category.getDisplayValue();
var subcategory = current.subcategory.getDisplayValue();

 

var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.addQuery('category', category);
gr.addQuery('subcategory', subcategory);
gr.addQuery('sys_id', '!=', current.sys_id); // Exclude the current incident from the query
gr.query();

 

var similarIncidentCount = gr.getRowCount();

if (similarIncidentCount >= 3) { // Modify the threshold as per your requirement


// Create a problem ticket
var problemGr = new GlideRecord('problem');
problemGr.initialize();
problemGr.short_description = 'Multiple incidents with the same category and subcategory';
problemGr.description = 'Multiple incidents with the same category and subcategory have been reported.';
var problemSysId = problemGr.insert();

if (problemSysId) {
// Link incidents to the problem ticket
while (gr.next()) {
gr.problem_id = problemSysId;
gr.update();
}

 

gs.info('Problem ticket created: ' + problemSysId);
} else {
gs.error('Failed to create problem ticket');
}
}
})(current, previous);

2 REPLIES 2

VS
Giga Guru

Hi,

 

Let me know if this works:

(function executeRule(current, previous) {
    var category = current.category.getDisplayValue();
    var subcategory = current.subcategory.getDisplayValue();

    var gr = new GlideRecord('incident');
    gr.addActiveQuery();
    gr.addQuery('category', category);
    gr.addQuery('subcategory', subcategory);
    gr.addQuery('sys_id', '!=', current.sys_id);
    gr.query();

    var similarIncidentCount = gr.getRowCount();
    var threshold = 3;

    if (similarIncidentCount >= threshold) {
        var problemGr = new GlideRecord('problem');
        problemGr.addQuery('category', category);
        problemGr.addQuery('subcategory', subcategory);
        problemGr.query();

        if (!problemGr.next()) {
            problemGr.initialize();
            problemGr.short_description = 'Multiple incidents with the same category and subcategory';
            problemGr.description = 'Multiple incidents with the same category and subcategory have been reported.';
            var problemSysId = problemGr.insert();

            if (problemSysId) {
                while (gr.next()) {
                    gr.problem_id = problemSysId;
                    gr.update();
                }
                gs.info('Problem ticket created: ' + problemSysId);
            } else {
                gs.error('Failed to create problem ticket');
            }
        }
    }
})(current, previous);

 

Regards,

VS

mujahidhussain
Tera Contributor

Hi VS,

 

i have modified the script. now, im able to add all the incidents (>=3) as defined in the script. but,

 

1. problem ticket is not copying the category and sub category from the incidents.

2. if new incidents are created with the same conditioin, all the incidents are reassigned to the another problem which is in the "new" state and leaving the problem ticket created from the ui action on the incident form empty. 

 

please find the script below and do the need full. thank you!

 

(function executeRule(current, previous) {
var category = current.category;
var subcategory = current.subcategory;

var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.addQuery('category', category);
gr.addQuery('subcategory', subcategory);
gr.addQuery('state', '1');
gr.addQuery('domain', '3896007d14b4c51020d278a215e9618e');
gr.query();

var similarIncidentCount = gr.getRowCount();

if (similarIncidentCount >= 3) { // Modify the threshold as per your requirement
// Check if an existing problem ticket already exists for the category and subcategory
var existingProblemGr = new GlideRecord('problem');
//existingProblemGr.addQuery('short_description', 'Multiple incidents with the same category and subcategory');
existingProblemGr.addQuery('category', category);
existingProblemGr.addQuery('subcategory', subcategory);
existingProblemGr.query();

var problemSysId;

if (existingProblemGr.next()) {
problemSysId = existingProblemGr.sys_id.toString();

// Link incidents to the existing problem ticket
while (gr.next()) {
gr.problem_id = problemSysId;
gr.update();
}

gs.info('Incidents linked to existing problem ticket: ' + problemSysId);
} else {
// Create a new problem ticket
var problemGr = new GlideRecord('problem');
problemGr.initialize();
//problemGr.short_description = 'Multiple incidents with the same category and subcategory';
//problemGr.description = 'Multiple incidents with the same category and subcategory have been reported.';
problemGr.setValue('category', category);
problemGr.setValue('subcategory', subcategory);
problemSysId = problemGr.insert();

if (problemSysId) {
// Link incidents to the new problem ticket
while (gr.next()) {
gr.problem_id = problemSysId;
gr.update();
}

gs.info('Problem ticket created: ' + problemSysId);
} else {
gs.error('Failed to create problem ticket');
}
}
}
})(current, previous);

 

 

Regards,

Mujahid Hussain.