Solved

indexOf comparison evaluates to true

Posted on 2014-02-07
4
219 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:9apit
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 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 82

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:9apit
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 61
Wrapper for APPs 9 76
Google Sheets script efficiency 2 24
ASP.NET gridview select textbox on focus 2 34
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

770 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