Inbound Actions - need to find INC number and 3rd party ref

Carl Fransen1
Tera Guru

Hi Team,

I have a requirement to update an Incident from an email - and not log a new one.   I need to grab the INC number from the subject to perform an update to that INC ticket - plus grab the 3rd party ref number and add it to the incident field.

The subject is always in this format, the bold text is what changes between the types of updates coming back from the 3rd party ticket software:  

                                      Ticket Received - IT Service Desk - Incident INC0010036 has been resolved. - Ticket Ref. #4392

Therefore I need to find the INC to ensure I know that this is an update (new inbound action as not a reply) - and add the email to the work notes/comments of the existing INC.

Additionally i need to grab the '4382' and add this to the '3rd_party_ref_number' field i created on the incident record.

I have tried a bunch of code (copied from the community) - but all these only create new records, none of them update the tickets:

CODE1:

 

var numberPattern = "/INC\[0-9]{7}/"; //Looking for INC plus 0-9 numbers, seven numbers in total    

var reg = new SNC.Regex(numberPattern);    

var matchedNumber = reg.match(email.subject);    

 

if (JSUtil.notNil(matchedNumber)) { //matchedNumber will be NULL if a match is not found in the email subject.   If NULL we know this is a new email    

      var incidentRec = new GlideRecord("incident");    

      incidentRec.addQuery("number", matchedNumber); //Update this with your old ticket number field column name    

      incidentRec.query();    

      if (incidentRec.next()) {    

              incidentRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;    

              incidentRec.update();    

 

  }  

      current.insert();    

}    

CODE2:

var incnr = new GlideRecord("incident");

var lowmail = email.subject.toLowerCase();

incnr.addQuery('number', lowmail.substr(lowmail.indexOf('inc')+0,10));

incnr.query();

if(incnr.next()){

//event.state="stop_processing";

} else {

current.short_description = email.subject;

current.description = email.body_text;

current.contact_type = "email";

current.insert();

}

CODE3:

var tckstr = email.subject.toString();  

  var tckpos=tckstr.indexOf("INC");  

  var tcknum= email.subject.substr(tckpos,10);  

  incTicket=new GlideRecord('incident');  

  incTicket.addQuery('number', tcknum);  

  incTicket.query();  

  while (incTicket.next()) {  

  flagticket=true;  

  updateTicket(incTicket);  

incTicket.update();  

}  

Can anyone help with this - needed for a client implementation due this week!

Thanks

Carl.

1 ACCEPTED SOLUTION

Harneet Sital
Mega Sage
Mega Sage

Hi Carl,



how to link emails to incidents using the INC reference


I tried the following scenario and works and yours looks the same so you can try changing the script accordingly.



In the condition builder you can write Subject contains INC or in the script match the three letters followed by 7 digits.



gs.include('validators');


if (current.getTableName() == "incident")


{


  //This gets you the INC number similar code can be used to get the ref number in your case.


  var index = email.subject.indexOf("INC");


  var incNumber = email.subject.substring(index,index+10);


  gs.log(incNumber,'test');



  var gr = new GlideRecord('incident');


  gr.addQuery('number',incNumber);


  gr.query();



  while(gr.next())


  {


  gr.u_description = email.body_text;


  gr.update();


  }


}


View solution in original post

5 REPLIES 5

Omar Ashqar
Tera Contributor

Hi Carl,


CODE1 looks correct don't see any issue with it but not sure where you are adding this code in Servicenow,



I would suggest to have a look at


"Inbound Email Actions" under "Email" in the Navigator on the left.



Go there and search for Inbound email actions with the name *incident and you'll find


-Update Incident and - Create Incident



You might not need to go and QUERY for the INCIDENT in your code since the inbound email action already knows if its an update or create


by using the water mark in the email so all you need to do is add the necessary code changes in "update incident" Inbound email action.



Have a good read of the following:


http://wiki.servicenow.com/index.php?title=Inbound_Email_Actions#gsc.tab=0


Hope this helps


Thanks for replying Omar - i was working in the 'inbound actions' area.



it turned out that i was adding a current.inter() at the end of my code which meant that it updated the incident AND added a new one - so it was working it just was doing an additional stop.   I also had to add a 'stop processing' line in my code to stop it continuing to other inbound actions.



Thanks again for your help.


Harneet Sital
Mega Sage
Mega Sage

Hi Carl,



how to link emails to incidents using the INC reference


I tried the following scenario and works and yours looks the same so you can try changing the script accordingly.



In the condition builder you can write Subject contains INC or in the script match the three letters followed by 7 digits.



gs.include('validators');


if (current.getTableName() == "incident")


{


  //This gets you the INC number similar code can be used to get the ref number in your case.


  var index = email.subject.indexOf("INC");


  var incNumber = email.subject.substring(index,index+10);


  gs.log(incNumber,'test');



  var gr = new GlideRecord('incident');


  gr.addQuery('number',incNumber);


  gr.query();



  while(gr.next())


  {


  gr.u_description = email.body_text;


  gr.update();


  }


}


Thanks Harneet - appreciate your help.



Although I didn't use your code exactly I modified mine to make it work.   Two things - firstly i had a 'current.insert()' in my code which meant it added a NEW record ASWELL AS updating the incident (i never checked the updates as i thought it wasn't working).   I also has to add a 'stop processing' line to ensure that the email didn't trigger other inbound rules.   My final code is below:



//   Code added to process emails from a 3rd Party Vendor with the INC number included and the 3rd party reference number


//   15/03/2017 CJF/DP - Incident Mgmnt Phase


 


var numberPattern = "/INC\[0-9]{7}/i"; //Looking for INC plus 0-9 numbers, seven numbers in total. ignores case    


var reg = new SNC.Regex(numberPattern);    


var matchedNumber = reg.match(email.subject);    


 


if (JSUtil.notNil(matchedNumber)) { //matchedNumber will be NULL if a match is not found in the email subject.   If NULL we know this is a new email    


      var incidentRec = new GlideRecord("incident");    


      incidentRec.addQuery("number", matchedNumber); //Update this with your old ticket number field column name    


      incidentRec.query();    


      if (incidentRec.next()) {    


              incidentRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;  


  var thirdPartyTickStr= "ticket ref. #";


  var emSubj= email.subject.toLowerCase();


  var stIndx= emSubj.indexOf(thirdPartyTickStr);


  incidentRec.u_3rd_party_reference = emSubj.substring(stIndx+thirdPartyTickStr.length,emSubj.length);


  incidentRec.state=2;//State set as 'In Progress'


              incidentRec.update();


  event.state="stop_processing";


  }  


}    



Thanks again.



Carl.