Issue with IndexOf

Andrew Bettcher
Kilo Sage

Hi,

I have an inbound mail action that looks for specific text in the subject line of an inbound email and then uses specific parts of the email body depending on what the it finds in the subject.

It works for the most part except I have a line in there that says:

else if (email.subject.indexOf("message from unity") >0)
current.short_description = email.body.alert_text;

If the subject of an emails says "message from unity" it works fine. However, if the subject says what is more likely in a real scenario, "Error: Message from Unity" or "Alert: Message from Unity" it doesn't work.

I tried changing the string search position at the end of the first line. It originally said:

else if (email.subject.indexOf("message from unity") >-1)
current.short_description = email.body.alert_text;

But that didn't work either. I thought that IndexOf would find that text in wherever it sits in the subject line or do I need to use "contains" or something like that?

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

That's because indexOf looks for literally what you put in quotes. What you say is coming through in a real scenario...is not what you're looking for in your use of indexOf. Case matters here.

Consider using: toLowerCase(); to achieve your goal.

Example: https://www.w3schools.com/jsref/jsref_tolowercase.asp

Example with indexOf:

if (email_body.toLowerCase().indexOf("ral") === -1) { 

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


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

View solution in original post

2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hi,

That's because indexOf looks for literally what you put in quotes. What you say is coming through in a real scenario...is not what you're looking for in your use of indexOf. Case matters here.

Consider using: toLowerCase(); to achieve your goal.

Example: https://www.w3schools.com/jsref/jsref_tolowercase.asp

Example with indexOf:

if (email_body.toLowerCase().indexOf("ral") === -1) { 

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


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

Andrew Bettcher
Kilo Sage

Excellent.  Thank you.

I defined a variable for the subject in lower case and changed the script to:

else if (emailSubjectLowerCase.indexOf("message from unity") >-1)
	
	current.short_description = email.body.alert_text;

And it worked a treat.

Kudos.