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

You can try include an update for when it is updated:

//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 == true){
    current.update();
}


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();

}

 

If that will not trigger the update you can try:

current.setForceUpdate(true);
    current.update();

I have tested above code on update but did not work.

Can you tell me how i should use below within this code?

current.setForceUpdate(true);
    current.update();

Many thanks

//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 == true){
    current.setForceUpdate(true);
    current.update();
}


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();

}

I have tested this on update script but this is now creating new incident instead of updating existing incident. but the code does not have current.insert method at all.

Looks like logic is not correct.

 

find_real_file.png

When I add current.update existing update incident script then it create new incident. I have done many tests before and now but still no luck.