Finding text in emails - inbound email actions

brycefraser
Giga Contributor

I am trying to create an inbound email action that takes our Separation Notification emails, finds the user name within the email, and then uses that to automatically disable the AD account associated.

The emails look something like this:

Good morning all,

Please be advised that the following employee has finished at St Andrews War Memorial Hospital.

Name:

John Smith

Employee No:

000000

Position:

Employee

The problem I am having is trying to pull the name from the email, currently I am trying to do so using email.body_text.indexof, e.g.;

var pos1 = email.body.indexof("Name:");

var pos2 = email.body.indexof("Employee No:");

var name = email.body.substring((pos1 + 1), (pos2 - 14));

Ideally this should set the variable "name" to whatever is between the Name: line and the "Employee No:" line.

When the code runs, however, all that returns for pos1, pos2 and name is "undefined". I can't seem to work out what is wrong.

Any help with this would be great.

Thanks!

1 ACCEPTED SOLUTION

ganeshb_
Tera Expert

Hi Bryce,


I don't think you need to do all this substring stuff. You can directly access/pull the elements of the email body as:


var xyz = email.body.name; OR var xyz = email.body.employee_no;


For email like :


Name:John Smith


                            Employee No:000000


                            Position:Employee




Please try , and let me know.



Thanks,


Ganesh


View solution in original post

5 REPLIES 5

Michael Kaufman
Giga Guru

What happens when the email has the name on the same line, like:



Name: John Smith


I think the carriage return is messing it up.   The carriage return symbol is \n



Mike


Hi Mike



Thanks for your response, i'll give that a try.



Even if it does work, unfortunately I'll be unable to change the email that is sent as it's generated automatically from our payroll system.



I'll report back with how it went.



Thanks again!


Gurpreet07
Mega Sage

Try to use toString() before substring()


ganeshb_
Tera Expert

Hi Bryce,


I don't think you need to do all this substring stuff. You can directly access/pull the elements of the email body as:


var xyz = email.body.name; OR var xyz = email.body.employee_no;


For email like :


Name:John Smith


                            Employee No:000000


                            Position:Employee




Please try , and let me know.



Thanks,


Ganesh