Update Existing case record if email subject is same as short Description

Arun151
Tera Contributor

Our requirement is to create a case from email inbound action type is new ,Here we need to update case if short description is already present on existing records if not need to create a new case followed by Incident . Below is my script include it is not comparing email subject and creating a new record which is leading to duplicates.

 

var CreateIntactEmail = Class.create();
CreateIntactEmail.prototype = {
initialize: function() {},

 

createCaseRecord: function(currentRecord) {
var emailsubject = email.subject;
gs.log('case:' + email.subject);

var gr = new GlideRecord('sn_customerservice_case');
gr.addQuery('account', '4ab62c661b88f510f5544370f54bcb3e');
gr.addQuery('short_description', emailsubject);
gr.query();
if (gr.next()) {
gs.log('case found' + gr.sys_id);
gs.log('case match found');
gr.comments = " Updated comments from email: " + email.body_text;
gr.update();
} else {
gs.log('case create:' + gr.short_description);

var cas = new GlideRecord('sn_customerservice_case');
cas.initialize();
cas.work_notes = "received from: " + email.origemail + "\n\n Email body \n\n" + email.body_text;
cas.short_description = email.subject;
cas.description = email.body_text;
cas.insert();

 

if (cas.short_description.indexOf('Issue') > -1 || cas.short_description.indexOf('ISSUE') > -1) {
gs.log('incident creation');
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
//gs.log('incident created');
incidentGR.short_description = cas.short_description.toString();
incidentGR.parent = cas.sys_id;
incidentGR.contact_type = "email alert";


incidentGR.work_notes = "received from: " + email.origemail + "\n\n Email body \n\n" + email.body_text;
incidentGR.assignment_group = '574802eddb6b2c10fef3df0bd3961921'; // Cloud Ops Group


incidentGR.insert();
}

}

},


type: 'CreateIntactEmail'
};

 

Arun151_1-1690429382229.png

 

Arun151_2-1690429434993.png

 

I have even created a before business rule to abort action when short description already exist but no luck

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Arun151 

there is a way how ServiceNow determines if incoming email is of type New or Reply

If the incoming email contains watermark then it considers it as reply and will update the record.

Check this link for more details

Inbound email actions 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Arun151
Tera Contributor

My script is working fine in background but is not working through script include the problem is it is not even entering into if condition  to check shortdescription direclty going to else condition and creating new record