Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

indexOf comparison evaluates to true

Posted on 2014-02-07
4
Medium Priority
?
290 Views
Last Modified: 2014-02-07
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
0
Comment
Question by:Allen Pitts
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 total points
ID: 39843052
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
 
LVL 83

Expert Comment

by:leakim971
ID: 39843257
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
 

Author Closing Comment

by:Allen Pitts
ID: 39843385
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
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39843389
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question