indexOf comparison evaluates to true

Hello Expert,

Working with an application called ServiceNow. It has a function
called Email Inbound Action.  The Email Inbound Action has a single
line text box into which a JavaScript statement can be written.
If the statement evaluates True an incident is created.
If the statement does not evaluate True an incident is not created.

ServiceNow is getting an email from another appl called SolarWinds.
The servers in SolarWinds are marked PRODUCTION Y or
PRODUCTION N and send an email to ServiceNow with the PRODUCTION
attribute if the server goes down.

If PRODUCTION attribute in SolarWinds set to Y: in email body: "PRODUCTION_Y/N:Y":  create incident.
If PRODUCTION attribute in SolarWinds set to N: in email body: "PRODUCTION_Y/N:N":  do not create incident.
If PRODUCTION attribute in SolarWinds not set (null): in email body: "PRODUCTION_Y/N:":  do not create incident.

I have got it to work for the first two cases with the condition
email.body_text.indexOf("PRODUCTION_Y/N:N") == -1

The fly in the ointment is if the PRODUCTION attribute is not set I get the
third case: "PRODUCTION_Y/N:":

If "PRODUCTION_Y/N:" ) comes in the email body the incident is created
which is incorrect.

Tried Condition:
(email.body_text.indexOf("PRODUCTION_Y/N:N") == -1 || email.body_text.indexOf("PRODUCTION_Y/N:  ") == -1)
but this does not stop the unset attribute. I think because  the second half of the OR statement is looking for a blank string when what is being returned is no text beyond the semicolon.

There are lots of ways using MATCH() or TEST() or REGEX() to check for strings but I
need to return a boolean false if a part of a string does not exist.

Any ideas?

Thanks

Allen in Dallas
Allen PittsBusiness analystAsked:
Who is Participating?
 
COBOLdinosaurConnect With a Mentor Commented:
Don't test for the null.  
NULL and N call for the same action. Therefore the logic is if not Y do not create else create.
Cd&
0
 
leakim971PluritechnicianCommented:
I need to return a boolean false if a part of a string does not exist.

var create_incident = email.body_text.indexOf( "PRODUCTION_Y/N:Y" ) >= 0;

Open in new window

or :
var dont_create_incident = !email.body_text.indexOf( "PRODUCTION_Y/N:Y" ) >= 0;

Open in new window

or :
var create_incident = email.body_text.indexOf( "PRODUCTION_Y/N:Y" ) >= 0;
var dont_create_incident = !create_incident;

Open in new window

0
 
Allen PittsBusiness analystAuthor Commented:
Hello COBOL,

That worked. Code string
email.body_text.indexOf("PRODUCTION_Y/N:N") != -1
Says if indexOf("...Y/N:Y") is not false create an incident.

Thanks

Allen in Dallas
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
if (email.body_text.indexOf("PRODUCTION_Y/N:Y") > -1) {
	// create incident
} else {
	// don't create incident
}

Open in new window

or more simply
if (email.body_text.indexOf("PRODUCTION_Y/N:Y") > -1) {
	// create incident
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.