- 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
‎10-04-2020 09:12 PM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2020 11:05 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2020 11:07 PM
//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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2020 12:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-05-2020 01:39 AM
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.