Inbound email actions without re: prefix

chinna
Mega Guru

Actually my requirement if a provider send a new email with existing incident number, its updating the existing incident.

am tried in different ways am not able to do,it creating a new incident 

IT is possible? with a new email subject contains existing number we can update the same incident ?

Can Someone Help me on this?

1 ACCEPTED SOLUTION

Dude. i don't even know where to start with this!

The code you've posted that is apparently working doesn't look likely to work at all. Can you check the email logs of some inbound messages from your provider that you think were processed by this inbound action and just confirm which inbound action actually picked them up?

1. You're getting the indexOf('INC') in you mcn1 variable but then making a substring of that index+1 so you'll end up with 'NC12345678'

2. You're then making an incNum variable with substrings of an inc variable that doesn't exist

3. You're also using a regex to find a string of 7 numbers

4. You're then running a glide record where you are trying to find a record where the number field matches the content of 3 different variables that you've populated with different values. THIS WILL NEVER RETURN ANY RESULTS!

Say the number field is INC12345678, your mcn1 variable will be NC12345678, your incNum variable will be undefined and your matchedNumber variable will be 1234567. None of them would work on their own and they certainly won't work when your query requires all of them to match the number field!

You can try the code below but without knowing your instance or being able to troubleshoot myself i'm kinda shooting in the dark.

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 
var sub = email.subject; 
var index = sub.indexOf('INC'); 
var mcn1 = sub.substring(index,index+10);

gs.log('this is the inc ref: ' + mcn1);

var index1 = sub.indexOf('[OS');
var index2 = sub.indexOf(']');
var mcn = sub.substring(index1,index2+1);

gs.log('this is the provider ref: ' + mcn);

var gr = new GlideRecord('incident');
gr.addQuery('number',mcn1);
gr.query();
if(gr.next()){ 

gs.log("Test8 inside if"+gr.number+"mission-->"+gr.u_external_ticket_reference);

gr.comments="UPDATE FROM OPEN SYSTEM ";
gr.u_external_ticket_reference=mcn;
gr.work_notes = "received from: " + email.origemail + "\n\n" + email.body_text;
gr.update();
}
}
})(current, event, email, logger, classifier);

Write exactly that and let me know what the gs.log's come back as maybe i can help further, we'll see!

View solution in original post

28 REPLIES 28

And what is the order of Create incident inbound action?

May be you disable that and check if this inbound action is working fine or not.

use this code, i don't know why you changed it. The regex was completely unnecessary, you were matching the glide record on 2 different values for the same field and you had moved the event state to the end where it wouldn't work as required.

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 gs.include('validators');
 var sub=email.subject;
 var inc=sub.indexOf('INC');
 var incNum= sub.substring(inc,inc+7);

 var gr = new GlideRecord("incident");
 gr.addQuery('number',incNum);
 gr.query();
 if(gr.next()) {
 
 if(email.subject.toLowerCase().indexOf("worknotes") >= 0){
 gr.work_notes = "reply from: " + email.origemail + "\n\n" + email.body_text;
 gr.update();
 event.state="stop_processing";
 }
 else{
 gr.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
 gr.update();
 event.state="stop_processing";
 gr.update();
 }
 }

 })(current, event, email, logger, classifier);

chinna
Mega Guru

Hi David,

i done inactive of existing inbound action of create new incident,i tried you posted code,but it not creating a incident,

the target field should be empty.

 

pls find my screenshot.

Check the email logs on that message to see what inbound action created the incident. There's nothing in the code i provided that will create an incident so you must have another inbound action doing it.

chinna
Mega Guru

Actually david i dont want to create a new incident,

If provider sends a new email from their side without Re: prefix and without Watermark ,the subject line contains only provider number and Incident number like INC0012345 needs to update the existing one.

 

In other cases,servicenow sends a email to the provider,provider gives a reply without re: prefix and without withoutmark,subject contains incident number it updates the same existing incident in snow.for this case i written code like below and it works

 

var numberPattern = "/#{2}[0-9]{5}#{2}/"; //Looking for # (2 occurrences) followed by 0-9 numbers (5 occurrences) followed by # (2 occurrences)
var reg = new SNC.Regex(numberPattern);
var matchedNumber = reg.match(email.subject);
var incNum= sub.substring(inc,inc+10);

if (JSUtil.notNil(matchedNumber)) { //matchedNumber will be NULL if a match is not found in the email subject. If NULL we know this is a new email
var incidentRec = new GlideRecord("incident");
incidentRec.addQuery("number", matchedNumber); //Update this with your old ticket number field column name
incidentRec.addQuery('number',incNum);
incidentRec.query();

 

if (incidentRec.next()) {

 

incidentRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;

 

incidentRec.update();

 

}

 

 

 

But my requirement is provider sends a new email with subject line INC123456 it needs to update the existing one.