Inbound action to find unique number in email subject and update ticket

denu
Kilo Expert

Hi,

I have a requirement to match unique number in email subject (Not ServiceNow record or water mark) and create a new ticket if match is not found in active tickets list or update ticket if match is found.

I have started matching complete subject line with the help of other community post.It but I still could not get the unique no in subject at all. Unfortunately we are unable to match with complete subject line.

Any help would be really appreciated

Example:

  • Unique reference start with (00DU0JKNe_)

find_real_file.png

This scrip is match with complete subject. but i only want to match unique no in subject line.

//Check for Current Open Incident and Update
var eSubject = email.subject;
var grInc = new GlideRecord('incident');
var grEmail = new GlideRecord('sys_email');
var incUpdated = false;
//Weeks ago
//grEmail.addEncodedQuery('sys_created_onONThisweek@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()^subject=' + eSubject);
//Find the matching subject
grEmail.addEncodedQuery('^subject=' + eSubject);
grEmail.query();
if(grEmail.getRowCount != 0){
while(grEmail.next() && incUpdated != true){
grInc.get(grEmail.instance);
if(grInc.active == true){
incUpdated = true;
grInc.work_notes = '\nFrom: ' + email.from + '\nTo: ' + email.to + '\nSubject: ' + email.subject + '\n\n' + email.body_text;
sys_email.target_table = "incident";
sys_email.instance = grInc.sys_id;
sys_email.update();
grInc.update();
gs.log("update:Existing ticket updated through:" + grInc.number, "EMAIL." + sys_email.sys_id);
}

  }
}

if(incUpdated == false){
// If existing incident not found create new incident
current.caller_id = gs.getUserID();
current.description = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;
current.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;

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

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

if (email.importance != undefined) {
   if (email.importance.toLowerCase() == "high") {
		current.impact = 1;
		current.urgency = 1;
   }
}

current.insert();		

}

 

 

 

1 ACCEPTED SOLUTION

So I think you want to check the number is in the subject of any email? And then update the incident record for those emails. If no match found then insert?

 

Can you try like this:

//Check for Current Open Incident and Update
var eSubject = email.subject;
var regex = /00DU0JKNe_(\d*)/g;
eSubject = eSubject.match(regex);
var grInc = new GlideRecord('incident');
var grEmail = new GlideRecord('sys_email');
var incUpdated = false;
//Find the matching subject
grEmail.addQuery('subject', 'CONTAINS', eSubject);
grEmail.query();

while (grEmail.next() && incUpdated != true && eSubject) {
    grInc.get(grEmail.instance);
    if (grInc.active == true) {
        incUpdated = true;
        grInc.work_notes = '\nFrom: ' + email.from + '\nTo: ' + email.to + '\nSubject: ' + email.subject + '\n\n' + email.body_text;
        sys_email.target_table = "incident";
        sys_email.instance = grInc.sys_id;
        sys_email.update();
        grInc.update();
        gs.log("update:Existing ticket updated through:" + grInc.number, "EMAIL." + sys_email.sys_id);
    }

}


if (incUpdated == false) {
    // If existing incident not found create new incident
    current.caller_id = gs.getUserID();
    current.description = "received from: " + email.origemail + "\n\n" + email.body_text;
    current.short_description = email.subject;
    current.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;

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

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

    if (email.importance != undefined) {
        if (email.importance.toLowerCase() == "high") {
            current.impact = 1;
            current.urgency = 1;
        }
    }

    current.insert();

}

View solution in original post

16 REPLIES 16

Willem
Giga Sage
Giga Sage

Use this:

//Check for Current Open Incident and Update
var eSubject = email.subject;
var regex = /00DU0JKNe_(\d*)/g;
eSubject = eSubject.match(regex);
var grInc = new GlideRecord('incident');
var grEmail = new GlideRecord('sys_email');
var incUpdated = false;
//Weeks ago
//grEmail.addEncodedQuery('sys_created_onONThisweek@javascript:gs.beginningOfThisWeek()@javascript:gs.endOfThisWeek()^subject=' + eSubject);
//Find the matching subject
grEmail.addEncodedQuery('^subject=' + eSubject);
grEmail.query();
if(grEmail.getRowCount != 0){
while(grEmail.next() && incUpdated != true){
grInc.get(grEmail.instance);
if(grInc.active == true){
incUpdated = true;
grInc.work_notes = '\nFrom: ' + email.from + '\nTo: ' + email.to + '\nSubject: ' + email.subject + '\n\n' + email.body_text;
sys_email.target_table = "incident";
sys_email.instance = grInc.sys_id;
sys_email.update();
grInc.update();
gs.log("update:Existing ticket updated through:" + grInc.number, "EMAIL." + sys_email.sys_id);
}

  }
}

if(incUpdated == false){
// If existing incident not found create new incident
current.caller_id = gs.getUserID();
current.description = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;
current.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;

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

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

if (email.importance != undefined) {
   if (email.importance.toLowerCase() == "high") {
		current.impact = 1;
		current.urgency = 1;
   }
}

current.insert();		

}

 

Replaced your first line with usage of regular expression. To learn more about Regular Expressions, please read:

https://community.servicenow.com/community?id=community_article&sys_id=ebcf2b6edbd7d0103daa1ea668961...

 

Bookmark and mark helpful if you think it is.

Hi Willem,

Thanks a lot. I will try this and let you know.

 

After adding the code it started creating new records every time when new or reply email comes with this unique no.

find_real_file.png

Just wondering is that syntax is correct in the code, i do not get any syntax error through. Could you please verify this for me?

find_real_file.png

thank you

 

 

So I think you want to check the number is in the subject of any email? And then update the incident record for those emails. If no match found then insert?

 

Can you try like this:

//Check for Current Open Incident and Update
var eSubject = email.subject;
var regex = /00DU0JKNe_(\d*)/g;
eSubject = eSubject.match(regex);
var grInc = new GlideRecord('incident');
var grEmail = new GlideRecord('sys_email');
var incUpdated = false;
//Find the matching subject
grEmail.addQuery('subject', 'CONTAINS', eSubject);
grEmail.query();

while (grEmail.next() && incUpdated != true && eSubject) {
    grInc.get(grEmail.instance);
    if (grInc.active == true) {
        incUpdated = true;
        grInc.work_notes = '\nFrom: ' + email.from + '\nTo: ' + email.to + '\nSubject: ' + email.subject + '\n\n' + email.body_text;
        sys_email.target_table = "incident";
        sys_email.instance = grInc.sys_id;
        sys_email.update();
        grInc.update();
        gs.log("update:Existing ticket updated through:" + grInc.number, "EMAIL." + sys_email.sys_id);
    }

}


if (incUpdated == false) {
    // If existing incident not found create new incident
    current.caller_id = gs.getUserID();
    current.description = "received from: " + email.origemail + "\n\n" + email.body_text;
    current.short_description = email.subject;
    current.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;

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

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

    if (email.importance != undefined) {
        if (email.importance.toLowerCase() == "high") {
            current.impact = 1;
            current.urgency = 1;
        }
    }

    current.insert();

}