- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 12:56 AM
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_)
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();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 03:37 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 01:02 AM
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:
Bookmark and mark helpful if you think it is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 01:12 AM
Hi Willem,
Thanks a lot. I will try this and let you know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 03:25 AM
After adding the code it started creating new records every time when new or reply email comes with this unique no.
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?
thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 03:37 AM
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();
}