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

Hi Willem,

This works really well. I have end up creating 3 scripts by duplicating same script for Create new, Update and reply and Forward actions.

However I just noticed 2 issues.

1. In mail logs it doesn't show which action has been processed. All showing as skipped. But record create and update work as expected. Any clue what i might of done wrong?

2. Looks like every time new update happen new incident number consumed and ticket numbers jumped up not as on sequence. How can I stop this happening?

find_real_file.png

Once again I really appreciated for your quick response. I spent few weekends to get this working. Also if you any tips to debug mail script please share.

 

Hi Denu,

1. Can you share a screenshot of the email log?

2. Numbering for new records is not consecutive. Whenever you open a record it will reserve a number, even if it it is not inserted. Have you or anyone interacted with the table?

Thanks Willem.

1. We have quite few inbound actions hard to get Email logs into one page.

Inbound action:

find_real_file.png

Email logs:

find_real_file.png

 

 

 

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

2. No one else use this instance. I think i am aware of numbering consumption in snow. Also if anyone try to open an incident and not proceed with submission that number also will never use again. 

But here I noticed that every update through inbound action consume a new instance no. Never noticed this before. I will check this in OTB instance as well.

I really appreciate your help.

  1. It appears it is skipping all of them because the type is not matched. Can you confirm this is also the case for your new inbound actions? Maybe just post the messages for those skip records?
  2. Can you find the numbers in between in the table? Or are they skipped?

 

 

Do you see the the log from the log statement?

gs.log("update:Existing ticket updated through:" + grInc.number, "EMAIL." + sys_email.sys_id);

If you can see it in the log, then we know the script has run.

 

 

Can you also try without the email update? I think that is not needed, because if the instance matches an incident, the table and record should already be the incident.

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

}

Sorry for the late response.

1.The "Skipping" issue is only happening to these new inbound mail scripts. All the other existing inbound action are working as expected.

After ticket got updated through above inbound action logs shows that ticket got updated.So we know that script has ran correctly.

gs.log("update:Existing ticket updated through:" + grInc.number, "EMAIL." + sys_email.sys_id);

find_real_file.png

Based on HI support documentation through below link says that we should use "current.update()" to avoid "Skipping" message.

 https://hi.service-now.com/kb_view.do?sysparm_article=KB0816008

find_real_file.png

I really don't know how to rewrite this "Update unique Incident" via inbound action script using current method.

Also I have tried inbound actions without below codes, then noticed that target is not populate when record updated. Same issue, HI support says that if "Target" and "email logs" to be correctly updated "current.update" method should use.

        // sys_email.target_table = "incident";
        // sys_email.instance = grInc.sys_id;
        // sys_email.update();

find_real_file.png

 

2. Also have checked if there is any number in between for incident number jump, but there is none. It got really skipped. Not sure as why instance numbers are skipping after introduced these new inbound mail scripts.

I have separate scripts for this unique number marching inbound action for create new, update through reply and forward responses. When i disabled reply and forward i do not get that instance number jump. it worked as expected.