A script for extracting the body of an email (that contains no MSG-tag) into the Work notes of an incident

Marlowe Klingva
Kilo Contributor

Hi

I want to create an email inbound action that will extract the text in the body of the email into the Work notes on the Notes tab on the Incident form in ServiceNow. The e-mail that's incoming does not contain a MSG-tag, so the relevant Incident is recognized if the Incident number is present in the format "INCXXXXXXX" (X= any number between 0-9) anywhere in the Subject of the email.

Furthermore, I would like this extraction of the text to be executed only if the State of the Incident is either New, In Progress or On Hold.

I'm struggling with this a lot, and am relatively new to all this. So any help I would receive would be totally awesome. The code that I have so far - which I probably have messed up and which isn't working is this:

var str = email.subject;
var number = str.indexOf("INC");
var res = str.substring(number, number+7);

var getInc = new GlideRecord('incident');
                getInc.comments = email.body;

if(getInc.query()){
                getInc.comments = email.body;
getInc.update();
}

Kind regards

Marlowe Klingvall

8 REPLIES 8

Allen Andreas
Administrator
Administrator

Hi,

So you can use current as the inbound action is already on the table and ServiceNow already associates this inbound action with a specific record if the number is in the subject. Then in the script actions you could simply do:

if (current.state == '1' || current.state == '2' || current.state == '3') {
current.work_notes = email.body_text;
current.update();
}

 

replace 1 and 2 and 3 with the number values of that states you're talking about.

See if that works.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Updated


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen A and Lokesh

Thank you so much for your replies. I have tried both your solutions and in all but one aspect they work out great. I can get the text in a body of an e-mail to be extracted and inserted into Activities in the Notes tab. The only thing is that it will not update the existing incident, the one that's referred to in the subject of the email, instead it creates a brand new incident. Now, I have tried to deactivate every other Inbound action, but that doesn't help any. So, I wonder, do you have any ideas about why this isn't working? Is there a Business rule or Client Script or the alike that I should deactivate/modify? I have looked through them but found nothing that I thought should interfer. What do you think?

Kind regards

/Marlowe Klingvall

Hi, firstly, if you don't mind, please mark my reply above as Helpful...if it was.

Secondly, if it's creating a new incident you'd want to look at your inbound actions for the Incident table and see if one is running before the other. 

Also in your original post above you said: "so the relevant Incident is recognized if the Incident number is present in the format "INCXXXXXXX" (X= any number between 0-9) anywhere in the Subject of the email."

But now you're saying that it's creating new incidents. So...is it associating it to the incident in the system already or not?

Unfortunately, we're not getting a lot of information from you and some of it is mixed signals, so let's see how my above information assists you or not before I go any further.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!