How do we use regular expression in inbound email action script to identify the incident number?

Ankit69
Tera Contributor

Requirement:

I need to modify the inbound actions 'Create new call record' and 'Create Call Record (Forward)' so that if an incident number is found in the subject line then the system should look through all the incidents an make an update to the INCIDENTrecord and do not create any CALL record.

For this logic to work, we need to use regex to identify the incident number. The incident number would have SNI text at the beginning followed by 7 digits of number. If no incident number matches then continue the normal processing of the aforementioned inbound actions.

I have tried below highlighted code in Create Call Record (Forward) Inbound Email Action but didn't worked, can you suggest something:

 

//This change would enable this feature only to the OSD team - both UK & HKG
//Ensure any inconsistent casing in the email address is taken care of
//Note that we are fetching the TO / CC to list because the original Email was sent TO/ CC to the UK / HKG groups and then got forwarded to ServiceCentral@service-now.com
var emailSender = email.origemail.toLowerCase();
var EmailFrom = email.to.toLowerCase();
var EmailFromCC = email.copied.toLowerCase();
var ServiceDesk = 'novalue';
var UKServiceDesk = '52129e8d6f0cd2409a5676dcbb3ee40b';
var HKGServiceDesk = 'da129e8d6f0cd2409a5676dcbb3ee40a';
var KLServiceDesk = '9428f7586f6bd2008be1da55eb3ee475';

//Check if any of the key recipients include the support Email IDs of UK / HKG
if ((EmailFrom.match('customerrelations@intl.telstra.com') != null) || (EmailFrom.match('dcaccess@intl.telstra.com') != null) || (EmailFrom.match('dcservices@intl.telstra.com') != null) || (EmailFrom.match('support@intl.telstra.com') != null) || (EmailFrom.match('hostmaster@intl.telstra.com') != null) || (EmailFrom.match('service@intl.telstra.com') != null) || (EmailFrom.match('credits@intl.telstra.com') != null))
{

ServiceDesk = UKServiceDesk;
ProcessOSDEmail();
}
//Check if any of the key recipients include the support Email IDs of UK / HKG in CC ONLY IF no matches are found in TO list.
//This is to avoid duplication of Call Requests when a user accidentally copies the support groups in both TO and CC
else if ((EmailFromCC.match('customerrelations@intl.telstra.com') != null) || (EmailFromCC.match('dcaccess@intl.telstra.com') != null) || (EmailFromCC.match('dcservices@intl.telstra.com') != null) || (EmailFromCC.match('support@intl.telstra.com') != null) || (EmailFromCC.match('hostmaster@intl.telstra.com') != null) || (EmailFromCC.match('service@intl.telstra.com') != null) || (EmailFromCC.match('credits@intl.telstra.com') != null))
{

ServiceDesk = UKServiceDesk;
ProcessOSDEmail();
}

//If the Email is sent to the HKG groups - assign it to the HKG Service Desk instead of UK.
else if ((EmailFrom.match('gsd@team.telstra.com') != null) || (EmailFrom.match('vsd@team.telstra.com') != null) || (EmailFromCC.match('gsd@team.telstra.com') != null) || (EmailFromCC.match('vsd@team.telstra.com') != null))
{

//Hardcoding to HKG Service Desk
ServiceDesk = HKGServiceDesk;
ProcessOSDEmail();
}

//If the Email is sent to the KL groups - assign it to the KL Service Desk instead of HK or UK.
else if ((EmailFrom.match('ascip@pacnet.com') != null) || (EmailFrom.match('asc@pacnet.com') != null) || (EmailFrom.match('cdn.support@pacnet.com') != null) || (EmailFrom.match('pen.support@pacnet.com') != null) || (EmailFrom.match('pacnetconnect@pacnet.com') != null) || (EmailFrom.match('tsd@team.telstra.com') != null) || (EmailFrom.match('cdn.support@team.telstra.com') != null) || (EmailFrom.match('pen.support@team.telstra.com') != null) || (EmailFrom.match('pacnetconnect@team.telstra.com') != null) || (EmailFromCC.match('ascip@pacnet.com') != null) || (EmailFromCC.match('asc@pacnet.com') != null) || (EmailFromCC.match('cdn.support@pacnet.com') != null) || (EmailFromCC.match('pen.support@pacnet.com') != null) || (EmailFromCC.match('pacnetconnect@pacnet.com') != null) || (EmailFromCC.match('tsd@team.telstra.com') != null) || (EmailFromCC.match('cdn.support@team.telstra.com') != null) || (EmailFromCC.match('pen.support@team.telstra.com') != null) || (EmailFromCC.match('pacnetconnect@team.telstra.com') != null))
{

//Hardcoding to KL Service Desk
ServiceDesk = KLServiceDesk;
ProcessOSDEmail();
}

else{

var user=getUser(email.origemail.toLowerCase());
if(user!=false){
if(user.company.u_offshore_supported==true){
ServiceDesk = KLServiceDesk;
ProcessOSDEmail();
}
}
else{
ServiceDesk = KLServiceDesk;
ProcessOSDEmail();
}
}

function getUser(mailID){

var ga=new GlideAggregate('sys_user');
ga.addQuery('active','true');
ga.addQuery('email',mailID);
ga.addAggregate('COUNT');
ga.query();
var users= 0;
if(ga.next())
users=ga.getAggregate('COUNT');

if(users==1){
var gr=new GlideRecord('sys_user');
gr.addQuery('active','true');
gr.addQuery('email',mailID);
gr.query();

if(gr.next()){}

return gr;
}
else {
return false;
}

}
function getUserSysID(emailList){

var email= emailList.split(',');
var idList= [];
var inactiveList = [];
for(var i=0;i<email.length;i++){

var emailId = gs.getProperty('incident.watchlist.exclude.emails');
if(!emailId.contains(email[i]))
{
var gr=new GlideRecord('sys_user');
// gr.addQuery('active','true');
gr.addQuery('email',email[i]);
gr.query();
if(gr.next())
{
if(gr.active == true)
{
idList.push(gr.sys_id.toString());
}
}
else
{
idList.push(email[i]);
}
}
}
if(JSUtil.notNil(idList))
return idList.join();
else return "";
}
function ProcessOSDEmail() {
var callRecord = new GlideRecord('new_call');
callRecord.initialize();
callRecord.description = "Received from: " + email.origemail + "\n\n" + "Email To & CC: " + email.to + "\n\n" + "Subject: " +email.subject + "\n\n" + email.body_text;
//Hardcoding to UK Service Desk
callRecord.u_assignment_group = ServiceDesk;


var res=getUser(email.origemail.toLowerCase());

if(res==false){
callRecord.caller = '9e832ca86f88da403446e4021c3ee486';
callRecord.u_alternate_contact = '9e832ca86f88da403446e4021c3ee486';
callRecord.company = 'f66b14e1c611227b0166c3a0df4046ff';

}
else{
callRecord.caller = res.sys_id.toString();
callRecord.u_alternate_contact = res.sys_id.toString();
callRecord.company = res.company;
}
//Hardcoding the user to Telstra_Assignee

callRecord.opened_by = '5136503cc611227c0183e96598c4f706'; //System generated user
callRecord.short_description = email.subject;

//Hardcoing the company to Telstra

//current.company = '215ef1f04f2bd2008c2e4c111310c7f2';

callRecord.category = "request";
callRecord.contact_type = "email";
var checkGuestEmail = getUserSysID(emailSender);
if(checkGuestEmail != '')
{
callRecord.u_watch_list=getUserSysID(EmailFrom) +','+ checkGuestEmail;
}
else
{
callRecord.u_watch_list=getUserSysID(EmailFrom);
}
callRecord.insert();
sys_email.instance = callRecord.sys_id;
sys_email.target_table = callRecord.getTableName();
}

//This is the existing code that if left alone, would impact the GES-A customers also.
//Hence it is enveloped into a function that wouldn't be used till further confirmation from OSD.
function ProcessEmail() {
if(email.subject.match(/(SNI)\d{7}/)){
var target = new GlideRecord('incident');
target.query();
target.update();
}
else{
var callRecord = new GlideRecord('new_call');
callRecord.initialize();
//Note: current.opened_by is already set to the first UserID that matches the From: email address
if(gs.getUserID()=='5136503cc611227c0183e96598c4f706')
{
callRecord.description = "Received from: " + email.origemail + "\n\n" + email.body_text;
callRecord.u_assignment_group = gs.getProperty('callrecord.default.assigngroup');
}
else
{
callRecord.description = email.body_text;
}

callRecord.caller = gs.getUserID();
callRecord.u_alternate_contact = gs.getUserID();
callRecord.company = gs.getUser().getCompanyID();
callRecord.short_description = email.subject;
callRecord.opened_by = "5492f6306f13f100ee8376dcbb3ee4eb"; //System generated user
callRecord.category = "request";
callRecord.contact_type = "email";
callRecord.insert();
sys_email.instance = callRecord.sys_id;
sys_email.target_table = callRecord.getTableName();
//current.description = removeDisclaimer(email.body_text.toString());
/*function trimStr(str) {
str.replace(/^\s+|\s+$/g, '');
return str.replace(/[\n]+/g, '\n');
}*/

/*function removeDisclaimer(email_body){
var disclaimer_begins = email_body.indexOf("**************** CAUTION");

return email_body.substring(0,disclaimer_begins);
}*/
}
}

 

1 ACCEPTED SOLUTION

Try below code

//var str = "SNI0512271 is closed " ;

var str = email.subject.toString();
var regex = new SNC.Regex('/SNI\\d{7}/im');
var match = regex.match(str);

gs.log("match is :"+match);

if(match!= null){

var target = new GlideRecord('incident');
target.addQuery('number',match);
target.query();
if(target.next()){
target.work_notes = "reply from: " + email.origemail + "\n\n" + email.body_text;
target.update();
}
}

View solution in original post

4 REPLIES 4

HarshTimes
Tera Guru

Hey Ankit

Check below regex

var str = email.subject ;
var regex = new SNC.Regex('/SNI\\d{7}/im');

var match = regex.match(str);

return match;

 

 

 

-harsh

I have modified the code to below but the update to incident record is not happening, please suggest:

if(email.subject.match(/(SNI)\d{7}/)){
var target = new GlideRecord('incident');
target.addQuery('number',match);
target.query();
target.work_notes = "reply from: " + email.origemail + "\n\n" + email.body_text;
target.update();
}
else
{

Try below code

//var str = "SNI0512271 is closed " ;

var str = email.subject.toString();
var regex = new SNC.Regex('/SNI\\d{7}/im');
var match = regex.match(str);

gs.log("match is :"+match);

if(match!= null){

var target = new GlideRecord('incident');
target.addQuery('number',match);
target.query();
if(target.next()){
target.work_notes = "reply from: " + email.origemail + "\n\n" + email.body_text;
target.update();
}
}

Bloody brilliant! Thanx for the code! For me it worked like a charm and I managed to build a future-proof solution for the Inbound Email Flow.