Need to find the similar incidents based on short description or configurtion item.

sai9845
Tera Contributor

Need to find the similar incidents based on short description or configurtion item. If found the similar incidents create a problem ticket with those incidents numbers and ci value. And attach the incidents and ci's in the related list of the problem ticket. And need to create a schelude on weekly basis to avoid duplication of problem ticket creation. This is the use case for creation of Agentic AI. Pls help me how to achive this.

1 ACCEPTED SOLUTION

(function execute(inputs, outputs) {

    // ----------------------------
    // 1. Read primary incident NUMBER
    // ----------------------------
    var primaryNumber = String(inputs.primary_incident_sys_id || '');
    if (!primaryNumber) {
        outputs.status = "error";
        outputs.message = "Primary incident number missing.";
        return;
    }

    // ----------------------------
    // 2. Parse similar incidents (STRING → ARRAY)
    // ----------------------------
    var numbers = [];
    var similar = inputs.similar_incident_sys_ids;

    if (typeof similar === 'string') {
        try {
            similar = JSON.parse(similar);
        } catch (e) {
            similar = [];
        }
    }

    if (Array.isArray(similar)) {
        for (var i = 0; i < similar.length; i++) {
            if (similar[i].recordNumber)
                numbers.push(String(similar[i].recordNumber));
        }
    }

    if (numbers.indexOf(primaryNumber) === -1)
        numbers.push(primaryNumber);

    if (!numbers.length) {
        outputs.status = "error";
        outputs.message = "No incidents provided.";
        return;
    }

    // ----------------------------
    // 3. Get PRIMARY incident
    // ----------------------------
    var primaryInc = new GlideRecord('incident');
    if (!primaryInc.get('number', primaryNumber)) {
        outputs.status = "error";
        outputs.message = "Primary incident not found.";
        return;
    }

    var shortDesc = String(primaryInc.short_description);

    // ----------------------------
    // 4. Find existing Problem by short_description
    // ----------------------------
    var chosenProblemId = "";
    var chosenProblemNumber = "";
    var isNew = false;

    var prbGR = new GlideRecord('problem');
    prbGR.addQuery('short_description', shortDesc);
    prbGR.orderByDesc('sys_created_on');
    prbGR.setLimit(1);
    prbGR.query();

    if (prbGR.next()) {
        chosenProblemId = prbGR.getUniqueValue();
        chosenProblemNumber = String(prbGR.number);
    }

    // ----------------------------
    // 5. Create Problem if NOT found
    // ----------------------------
    if (!chosenProblemId) {
        var newPrb = new GlideRecord('problem');
        newPrb.initialize();
        newPrb.short_description = shortDesc;
        newPrb.description =
            "Auto-created from Virtual Agent\n\n" +
            "Primary Incident: " + primaryNumber + "\n\n" +
            "Related Incidents:\n" + numbers.join("\n");

        newPrb.cmdb_ci = primaryInc.cmdb_ci;
        newPrb.priority = primaryInc.priority;

        chosenProblemId = newPrb.insert();
        chosenProblemNumber = String(newPrb.number);
        isNew = true;
    }

    // ----------------------------
    // 6. Attach ALL incidents
    // ----------------------------
    var linkedNumbers = [];

    var incGR = new GlideRecord('incident');
    incGR.addQuery('number', 'IN', numbers.join(','));
    incGR.query();

    while (incGR.next()) {
        incGR.problem_id = chosenProblemId;
        incGR.update();
        linkedNumbers.push(String(incGR.number));
    }

    // ----------------------------
    // 7. Outputs
    // ----------------------------
    outputs.problem_number = chosenProblemNumber;
    outputs.problem_sys_id = chosenProblemId;
    outputs.is_new_problem = isNew;
    outputs.incidents_linked = linkedNumbers;
    outputs.status = "success";
    if (isNew) {
    outputs.message =
        "A new Problem " + chosenProblemNumber +
        " has been created and all related incidents have been successfully linked.";
} else {
    outputs.message =
        "Similar incidents have been successfully associated with existing Problem " +
        chosenProblemNumber + ".";
}
})(inputs, outputs);

 

 

use this script as a flow action , it will all similar incident to prb

View solution in original post

7 REPLIES 7

hey @sai9845 

check your inbox.

(function execute(inputs, outputs) {

    // ----------------------------
    // 1. Read primary incident NUMBER
    // ----------------------------
    var primaryNumber = String(inputs.primary_incident_sys_id || '');
    if (!primaryNumber) {
        outputs.status = "error";
        outputs.message = "Primary incident number missing.";
        return;
    }

    // ----------------------------
    // 2. Parse similar incidents (STRING → ARRAY)
    // ----------------------------
    var numbers = [];
    var similar = inputs.similar_incident_sys_ids;

    if (typeof similar === 'string') {
        try {
            similar = JSON.parse(similar);
        } catch (e) {
            similar = [];
        }
    }

    if (Array.isArray(similar)) {
        for (var i = 0; i < similar.length; i++) {
            if (similar[i].recordNumber)
                numbers.push(String(similar[i].recordNumber));
        }
    }

    if (numbers.indexOf(primaryNumber) === -1)
        numbers.push(primaryNumber);

    if (!numbers.length) {
        outputs.status = "error";
        outputs.message = "No incidents provided.";
        return;
    }

    // ----------------------------
    // 3. Get PRIMARY incident
    // ----------------------------
    var primaryInc = new GlideRecord('incident');
    if (!primaryInc.get('number', primaryNumber)) {
        outputs.status = "error";
        outputs.message = "Primary incident not found.";
        return;
    }

    var shortDesc = String(primaryInc.short_description);

    // ----------------------------
    // 4. Find existing Problem by short_description
    // ----------------------------
    var chosenProblemId = "";
    var chosenProblemNumber = "";
    var isNew = false;

    var prbGR = new GlideRecord('problem');
    prbGR.addQuery('short_description', shortDesc);
    prbGR.orderByDesc('sys_created_on');
    prbGR.setLimit(1);
    prbGR.query();

    if (prbGR.next()) {
        chosenProblemId = prbGR.getUniqueValue();
        chosenProblemNumber = String(prbGR.number);
    }

    // ----------------------------
    // 5. Create Problem if NOT found
    // ----------------------------
    if (!chosenProblemId) {
        var newPrb = new GlideRecord('problem');
        newPrb.initialize();
        newPrb.short_description = shortDesc;
        newPrb.description =
            "Auto-created from Virtual Agent\n\n" +
            "Primary Incident: " + primaryNumber + "\n\n" +
            "Related Incidents:\n" + numbers.join("\n");

        newPrb.cmdb_ci = primaryInc.cmdb_ci;
        newPrb.priority = primaryInc.priority;

        chosenProblemId = newPrb.insert();
        chosenProblemNumber = String(newPrb.number);
        isNew = true;
    }

    // ----------------------------
    // 6. Attach ALL incidents
    // ----------------------------
    var linkedNumbers = [];

    var incGR = new GlideRecord('incident');
    incGR.addQuery('number', 'IN', numbers.join(','));
    incGR.query();

    while (incGR.next()) {
        incGR.problem_id = chosenProblemId;
        incGR.update();
        linkedNumbers.push(String(incGR.number));
    }

    // ----------------------------
    // 7. Outputs
    // ----------------------------
    outputs.problem_number = chosenProblemNumber;
    outputs.problem_sys_id = chosenProblemId;
    outputs.is_new_problem = isNew;
    outputs.incidents_linked = linkedNumbers;
    outputs.status = "success";
    if (isNew) {
    outputs.message =
        "A new Problem " + chosenProblemNumber +
        " has been created and all related incidents have been successfully linked.";
} else {
    outputs.message =
        "Similar incidents have been successfully associated with existing Problem " +
        chosenProblemNumber + ".";
}
})(inputs, outputs);

 

 

use this script as a flow action , it will all similar incident to prb

Hi @NeshanthA 

 

Thanks for sharing the architecture overview.

Have you implemented this solution in your instance?  could you please share a few screenshots of the Flow Designer setup and the Virtual Agent topic configuration for better clarity?

This would help the community understand the practical implementation details.

Looking forward to your response.

 

Thank you,
Umesh