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.

How to restrict creation of duplicate incident based on the short decscription?

jugantanayak
Tera Guru

Hi,

I have a requirement to restrict(Stop) the creation of duplicate incident based on the short description.

And the incidents are getting created from 3rd party integration.

 

Can anyone please help ?

 

Best Regards,

Juganta

3 REPLIES 3

Maddysunil
Kilo Sage

@jugantanayak 

I think you can before insert business rule on incident table , below is the code :

 

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

    // Check if the current operation is an insert (i.e., a new incident is being created)
    if (current.operation() == 'insert') {
        // Get the short description of the current incident
        var shortDescription = current.short_description;

        // Create a GlideRecord query to check for existing incidents with the same short description
        var incidentGr = new GlideRecord('incident');
        incidentGr.addQuery('short_description', shortDescription);
        incidentGr.query();

        // If an incident with the same short description already exists, cancel the creation process
        if (incidentGr.next()) {
            gs.addErrorMessage("An incident with the same short description already exists. Please check and try again.");
            current.setAbortAction(true); // Stop the creation process
        }
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Simran Gadodiya
Mega Sage

Hi @jugantanayak 

 

Hope this helps you. If this helps you accept the solution and mark it as helpful.

 

Create a before Business Rule in the incident table. and make it run only on insert

 

var incGr=new GlideRecord('incident');

incGr.addActiveQuery();

incGr.addQuery('sys_created_by',current.sys_created_by);

incGr.addQuery('opened_by',current.opened_by);

incGr.addQuery(' description', 'LIKE',current.description);

incGr.addQuery('short_description','LIKE',current.short_description);

incGr.query();

if(incGr.next()){
gs.addErrorMessage('Possible duplicate incident detected : ' +incGr.number);

current.setAbortAction(true);
}

 

Thanks

Amit Gujarathi
Giga Sage
Giga Sage

HI @jugantanayak ,
I trust you are doing great.
First, create a Script Include that will contain the logic to check for existing incidents with the same short description

// DuplicateIncidentChecker.js
var DuplicateIncidentChecker = Class.create();
DuplicateIncidentChecker.prototype = {
    initialize: function() {},

    // Check if a duplicate incident exists based on short description
    isDuplicateIncident: function(shortDescription) {
        var incidentGr = new GlideRecord('incident');
        incidentGr.addQuery('short_description', shortDescription);
        incidentGr.query();
        return incidentGr.hasNext(); // Returns true if a duplicate incident is found
    },

    type: 'DuplicateIncidentChecker'
};

Create a onBefore Business Rule with below code :

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

    var shortDescription = current.short_description;
    var duplicateChecker = new DuplicateIncidentChecker();

    if (duplicateChecker.isDuplicateIncident(shortDescription)) {
        gs.addErrorMessage('Duplicate incident with the same short description already exists. Please check.');
        // Cancel the creation of the duplicate incident
        current.setAbortAction(true);
    }

})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi