Incident task is getting created twice from my BR.

sreeshsurendran
Tera Guru

Incident task is getting created twice from my BR.

 

Please find the script below:

 

(function executeRule(current, previous /*null when async*/ ) {
    if (!current.isActionAborted()) {
        var incGr = new GlideRecord('incident');
        var CalledFromBR = true;
        if (current.incident.nil()) {
            var CrInc = new CSMIncidentIntegrations_BT().copyFieldsFromCaseToIncident(incGr, current, CalledFromBR);
			incGr.assigned_to = '';
            if (current.u_issue_type.toLowerCase() == 'network') {
                incGr.assignment_group = gs.getProperty('sn_customerservice.Group for Network Team');
            } else
                incGr.assignment_group = current.assignment_group;
            var incSysId = incGr.insert();
            current.incident = incSysId;
        }
        if (current.u_issue_type.toLowerCase() == 'openreach' || current.u_issue_type.toLowerCase() == 'open reach' || current.u_issue_type.toLowerCase() == 'no issue' || current.u_issue_type.toLowerCase() == 'inconclusive(testing failed)' || current.u_issue_type.toLowerCase() == 'inconclusive') {
            var incidentTaskGR = new GlideRecord('incident_task');
            incidentTaskGR.initialize();
            incidentTaskGR.short_description = current.short_description;
            incidentTaskGR.assignment_group = gs.getProperty('sn_customerservice.bt.openreach.assignment.group');
            incidentTaskGR.incident = current.incident;
            incidentTaskGR.company = current.account;
            incidentTaskGR.description = current.description;
            incidentTaskGR.cmdb_ci = current.incident.cmdb_ci;
            //incidentTaskGR.assigned_to = current.incident.assigned_to;
            incidentTaskGR.insert();
        }
    }
})(current, previous);

 

Please do let me know - where I'm doing mistake(s).

 

 

Thanks,

Sreesh Surendran

2 REPLIES 2

Jitendra Diwak1
Kilo Sage

Hi @sreeshsurendran,

 

Please try below updated code. 

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

    if (!current.isActionAborted()) {

        var incGr = new GlideRecord('incident');

        var CalledFromBR = true;

        if (current.incident.nil()) {

            var CrInc = new CSMIncidentIntegrations_BT().copyFieldsFromCaseToIncident(incGr, current, CalledFromBR);

            incGr.assigned_to = '';

            if (current.u_issue_type.toLowerCase() == 'network') {

                incGr.assignment_group = gs.getProperty('sn_customerservice.Group for Network Team');

            } else {

                incGr.assignment_group = current.assignment_group;

            }

            var incSysId = incGr.insert();

            current.incident = incSysId;

        }

        if (current.u_issue_type.toLowerCase() == 'openreach' || current.u_issue_type.toLowerCase() == 'open reach' || current.u_issue_type.toLowerCase() == 'no issue' || current.u_issue_type.toLowerCase() == 'inconclusive(testing failed)' || current.u_issue_type.toLowerCase() == 'inconclusive') {

            // Check if incident task already exists

            var incidentTaskGR = new GlideRecord('incident_task');

            incidentTaskGR.addQuery('incident', current.incident);

            incidentTaskGR.query();

            if (!incidentTaskGR.next()) { // Create incident task only if it doesn't exist

                incidentTaskGR.initialize();

                incidentTaskGR.short_description = current.short_description;

                incidentTaskGR.assignment_group = gs.getProperty('sn_customerservice.bt.openreach.assignment.group');

                incidentTaskGR.incident = current.incident;

                incidentTaskGR.company = current.account;

                incidentTaskGR.description = current.description;

                incidentTaskGR.cmdb_ci = current.incident.cmdb_ci;

                incidentTaskGR.insert();

            }

        } 

    }

})(current, previous);

 

Please accept my solution if it resolves your issue and thumps 👍 up 

 

Thanks 

Jitendra 

 

Please accept my solution if it works for and thumps up.

tebohomolebats1
Tera Contributor

The issue of the incident being created twice by your business rule likely stems from the conditions under which the rule is being triggered. To prevent the incident creation logic from running multiple times, you need to ensure that the business rule is set up correctly. This involves verifying the conditions, execution order, and perhaps adding a safeguard within the script itself.

1. Check Business Rule Conditions and Trigger:
   Ensure that the business rule conditions and trigger events are correctly set to avoid multiple executions. For instance, the rule should only run under specific conditions or states of the record.

2. Safeguard Within the Script:
   Add a check within the script to see if the incident has already been created. This can be done using a custom flag or an additional field on the record.

Here’s how you can modify your script to include a safeguard:
(function executeRule(current, previous /*null when async*/ ) {
    if (!current.isActionAborted()) {


        // Check if the incident field is already set to avoid duplicate creation
        if (current.incident.nil()) {


            var incGr = new GlideRecord('incident');
            var CalledFromBR = true;
            var CrInc = new CSMIncidentIntegrations_BT().copyFieldsFromCaseToIncident(incGr, current, CalledFromBR);
            incGr.assigned_to = '';

            if (current.u_issue_type.toLowerCase() == 'network') {
                incGr.assignment_group = gs.getProperty('sn_customerservice.Group for Network Team');
            } else {
                incGr.assignment_group = current.assignment_group;
            }

            var incSysId = incGr.insert();
            current.incident = incSysId;


            current.update(); // Update the current record to save the incident sys_id
        }

        // Create Incident Task based on issue type
        var issueType = current.u_issue_type.toLowerCase();
        if (issueType == 'openreach' || issueType == 'open reach' || issueType == 'no issue' || issueType == 'inconclusive(testing failed)' || issueType == 'inconclusive') {


            var incidentTaskGR = new GlideRecord('incident_task');
            incidentTaskGR.initialize();
            incidentTaskGR.short_description = current.short_description;
            incidentTaskGR.assignment_group = gs.getProperty('sn_customerservice.bt.openreach.assignment.group');
            incidentTaskGR.incident = current.incident;
            incidentTaskGR.company = current.account;
            incidentTaskGR.description = current.description;
            incidentTaskGR.cmdb_ci = current.incident.cmdb_ci;
            // incidentTaskGR.assigned_to = current.incident.assigned_to;
            incidentTaskGR.insert();
        }
    }
})(current, previous);

1. Check if Incident is Already Set:
   Before creating a new incident, the script checks if the `incident` field is already populated (`current.incident.nil()`). If it is `nil`, it proceeds to create a new incident and sets the `incident` field on the current record with the `sys_id` of the newly created incident.

2. Update the Current Record:
   After setting the `incident` field, the script updates the current record (`current.update()`). This ensures that the field is saved and prevents the rule from creating another incident if triggered again.