How to trim data from received email's body?

Rahul J
Mega Expert

G'day all,

I've a requirement such that as soon as an email will be received an Inbound Email Action Script should run, which will fetch the incident number from the body of that email and put it into a custom field  created on Incident table.

The email received is like this:

find_real_file.png

 

The incident number highlighted in Yellow should be trimmed from the email body and should be placed in a custom field - u_new_incident field on Incident Table.

Can some do me a massive favor by providing script that can be used to achieve this requirement.

Cheers-

Rahul J.

 

 

1 ACCEPTED SOLUTION

Rahul J
Mega Expert

Finally, I found a solution to this requirement.

I created an Inbound Email Action like this:

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

// Implement email action here
var rSubject=email.subject;
var bod=email.body_text;
var bod1=bod.toString();
var rBody=bod1.substring(0,12);


var inc1= new GlideRecord('incident');
inc1.addQuery('number',rSubject);
inc1.query();

gs.print("rows"+ inc1.getRowCount());

if (inc1.next()){
inc1.u_new_incident=rBody;
inc1.update();

}

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

 

And, everything worked smoothly. A big shoutout to community members who helped me in achieving this.

Cheers-

Rahul J.

View solution in original post

9 REPLIES 9

Manish Vinayak1
Tera Guru

It would be better if you could get the email which is being sent to ServiceNow, had a column header associated with it. E.g.:

If the email body has the following content:

Incident Number: RXINC0013100

 

You could access it in your inbound action using:

email.body.incident_number;

 

The following documentation explains how to use variables from email body in your inbound action:

https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/notification/reference...

 

Your inbound action code could look like this if you had an incident number column in the body:

current.u_new_incident = email.body.incident_number;

Cheers I'll give it a shot and then I'll get back to ya.

Thanks for ya response.

Cheers-

Rahul J.

Hi Manish,

Still no luck mate!

find_real_file.png

 

As you suggested to chuck Incident Number into Subject, Moreover, Inbound Email Action I've used for this purpose is like this:

find_real_file.png

 

Please let me know if I'm missing anything.

Cheers-

Rahul J.

I didn't suggest to have the incident number in the Subject, I suggested it to be in the body / message with a column header, like:

 

Email Body

-----------

Incident Number: INC1234567

You could get the email body changed and try the previous code.

 

But if you prefer having the incident number in the subject, then you can use:

current.u_new_incident = email.subject;

Hope this helps!

Cheers,

Manish