Solved

indexOf comparison evaluates to true

Posted on 2014-02-07
4
211 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 21

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now