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.

Inbound action to avoid duplicate incident.

Meenal Gharat
Tera Guru

Hi,

Inbound email action to avoid duplicate incident.

If the incident is created using inbound emai action with the same CI name it's state is Resolved/closed it should create new incident.

if the state is open/active it should not create new incident rather it should update the same incident

I have tried writting the below code,kindly assist.

 

current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
//current.short_description = email.subject;
current.short_description = email.body.short_description;

//current.category = "inquiry";
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

if(email.body.category!= undefined)
current.category = email.body.category;

//gs.log('email'+ email.direct.toString());

//if(email.body.subcategory!=undefined)
current.subcategory=email.body.subcategory;

if (email.body.assign != undefined)
current.assigned_to.setDisplayValue(email.body.assign_to);

// if (email.subject.toLowerCase().indexOf("configuration item") >= 0
// && email.subject.toLowerCase().indexOf("device ip address") > 0
// && email.subject.toLowerCase().indexOf("severity: critical") > 0 )
if (email.subject.indexOf("configuration item") >= 0
&& email.subject.indexOf("device ip address") > 0
&& email.subject.indexOf("severity: critical") > 0 ) {

var subjectSplit = email.subject.split('item:');
var theName = subjectSplit[1].split(',');
var res = theName[0];
gs.log('meen gharat 1'+ res);
var op = res.replace(/ /g,'');
gs.log('meen gharat'+op);

var cmdbci = new GlideRecord("cmdb_ci");
cmdbci.addQuery('name',op);
cmdbci.query();
gs.log('row count '+ cmdbci.getRowCount());
if(cmdbci.next())
{
gs.log('ci getting' +cmdbci.sys_id);
current.cmdb_ci = cmdbci.sys_id;
}
if(!cmdbci.next())
{
current.cmdb_ci.setDisplayValue('Dummy');
}
if(cmdbci.next())
{
var inc = new Gliderecord("incident");
inc.addQuery('active',true);
inc.addQuery('state','!=',4);
inc.addQuery('state','!=', 5);
inc.addQuery('cmdb_ci','=',cmdbci.sys_id);
inc.query();

if(!inc.next())
{
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.body.short_description;
current.category = email.body.category;
current.subcategory=email.body.subcategory;
//current.cmdb_ci.setDisplayValue(cmdbci.sys_id);
current.insert();

}
}
else
{
gs.addErrorMessage('Incident Already Exist');
}

}

current.insert();

1 ACCEPTED SOLUTION

Meenal Gharat
Tera Guru

Hi all

 I have used below code and it works fine for me.

 


var eSubject = email.subject;
var grInc = new GlideRecord('incident');
var grEmail = new GlideRecord('sys_email');
var incUpdated = false;
grEmail.addEncodedQuery('sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)^subject=' + eSubject);
grEmail.query();
if(grEmail.getRowCount != 0){
while(grEmail.next() && incUpdated != true){
grInc.get(grEmail.instance);


if(grInc.active == true && grInc.state < 6){ //is the existing incident active and not in closed or resolved state

incUpdated = true;

gs.log('Incident already exists');
grInc.work_notes = '\nFrom: ' + email.from + '\nTo: ' + email.to + '\nSubject: ' + email.subject + '\n\n' + email.body_text;
grInc.update();
}
}
}

//if(!gr.next());
if(incUpdated == false)

{
current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
//current.short_description = email.subject;
current.short_description = email.body.short_description;

//current.category = "inquiry";
current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

if(email.body.category!= undefined)
current.category = email.body.category;

//gs.log('email'+ email.direct.toString());

//if(email.body.subcategory!=undefined)
current.subcategory=email.body.subcategory;

if (email.body.assign != undefined)
current.assigned_to.setDisplayValue(email.body.assign_to);

var subjectSplit = email.subject.split('item:');
var theName = subjectSplit[1].split(',');
var res = theName[0];
gs.log('meen gharat 1'+ res);
var op = res.replace(/ /g,'');
gs.log('meen gharat'+op);

var cmdbci = new GlideRecord("cmdb_ci");
cmdbci.addQuery('name',op);
cmdbci.query();
gs.log('row count '+ cmdbci.getRowCount());
if(cmdbci.next())
{
gs.log('ci getting' +cmdbci.sys_id);
current.cmdb_ci = cmdbci.sys_id;
}
//if(!cmdbci.next())
else
{
current.cmdb_ci.setDisplayValue('Dummy');
}


current.insert();
//your code to create an incident
}


})(current, event, email, logger, classifier);

 

Thank you for relpying my query.

Regards,

Meenal

View solution in original post

37 REPLIES 37

But this dosen't complete my requirement.

I'm facing the same issue that duplicate incident is being created.

Try this code.

 current.caller_id = gs.getUserID();
 current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
 current.short_description = email.body.short_description;

//current.category = "inquiry";
 current.incident_state = IncidentState.NEW;
 current.notify = 2;
 current.contact_type = "email";


 if(email.body.category!= undefined)
   current.category = email.body.category;
 
 if(email.body.subcategory!=undefined)
   current.subcategory=email.body.subcategory;
 
 if(email.body.assign != undefined)
   current.assigned_to.setDisplayValue(email.body.assign_to);

if (email.subject.indexOf("configuration item") >= 0
 && email.subject.indexOf("device ip address") > 0
 && email.subject.indexOf("severity: critical") > 0 ) 
 {
   var subjectSplit = email.subject.split('item:');
   var theName = subjectSplit[1].split(',');
   var res = theName[0];
   gs.log('meen gharat 1'+ res);
   var op = res.replace(/ /g,'');
   gs.log('meen gharat'+op);
   var cmdbci = new GlideRecord("cmdb_ci"); 
   cmdbci.addQuery('name',op); 
   cmdbci.query();
   gs.log('row count '+ cmdbci.getRowCount());
   if(cmdbci.next())
   {
     //check if incident exists for this CI
     var inc = new Gliderecord("incident"); 
     inc.addEncodedQuery("active=true^state!=4^ORstate!=5^cmdb_ci="+cmdbci.sys_id);
     inc.query();
     if(!inc.next())
     {
       gs.log('ci getting' +cmdbci.sys_id);
       current.cmdb_ci = cmdbci.sys_id;
       current.insert();
     }
     else 
     {
       //update inciden here.
       gs.addErrorMessage('Incident Already Exist');
     }
   }
 }

 

Mark the comment as a correct answer and other comment(s) as helpful once worked.

 

Dubz
Mega Sage

I think it might be easier and would make more administrative sense if you create a new inbound action to update open incidents when the email type is new. You can then run your query against the incident table in this inbound action which will run before the create incident action and, if you find an open incident you can update it and stop processing.

It could get confusing if you are seeing the create incident action as being processed in the email logs but no new incident being created, try to keep the create incident action just for the purpose of creating incidents.

mholmes
Tera Expert

I haven't looked extensively at your code, but I did notice one major problem. You have the following line:

current.insert();

At the bottom of your script, outside of any conditional blocks. This means that it will always insert no matter what.

 

Also: We can't see this in your example, but something I learned the hard way is that if you have any "Field Actions" on the "Inbound Action," then it will always create an Incident regardless of what you do in your script. So if there's a scenario where you do not want to create an Incident, the "Inbound Action" must be wholly scripted and not use any "Field Actions."

Meenal Gharat
Tera Guru

Hi All,

 

I have modified the script to this can someone help me to correct the script if something is wrong?

 

if (email.subject.indexOf("configuration item") >= 0
&& email.subject.indexOf("device ip address") > 0
&& email.subject.indexOf("severity: critical") > 0 ) {
var subjectSplit = email.subject.split('item:');
var theName = subjectSplit[1].split(',');
var res = theName[0];
gs.log('meen gharat 1'+ res);
var op = res.replace(/ /g,'');
gs.log('meen gharat'+op);

var cmdbci = new GlideRecord("cmdb_ci");
cmdbci.addQuery('name',op);
cmdbci.query();
gs.log('row count '+ cmdbci.getRowCount());

if(cmdbci.next())
{
gs.log('ci getting' +cmdbci.sys_id);
current.cmdb_ci = cmdbci.sys_id;
}
else
{
current.cmdb_ci.setDisplayValue('Dummy');
}

var gr = new GlideRecord('sys_email');
gr.addQuery('subjectCONTAINS',cmdbci.sys_id);
gr.addQuery('sys_id','=',current.sys_id);
// gr.addQuery('')
gr.query();

if(gr.next())
{
var grInc = new GlideRecord('incident');
grInc.addEncodedQuery("active=true^state!=4^ORstate!=5^cmdb_ci="+cmdbci.sys_id);
grInc.addQuery('sys_created_on', '>', gs.daysAgo(1));
grInc.addQuery();

if(!grInc.next())
{
current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
//current.short_description = email.subject;
current.short_description = email.body.short_description;


current.incident_state = IncidentState.NEW;
current.notify = 2;
current.contact_type = "email";

if(email.body.category!= undefined)
current.category = email.body.category;


current.subcategory=email.body.subcategory;

if (email.body.assign != undefined)
current.assigned_to.setDisplayValue(email.body.assign_to);

}

current.insert();
}

}

else
{
gs.addErrorMessage('Incident is already created');
}

})(current, event, email, logger, classifier);