Highlighted

Why is my IF statement not working?

Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Here's my current IF statement that is not working.

<cfif (not structKeyExists(cfData, 'data.remove_pdf')) OR (structKeyExists(cfData, 'data.remove_pdf') AND #cfData.data.remove_pdf# neq "true")>

<a href="https://mysite.com/help/pdfexport/id/#cfData.data.id#">

<img src="/images/pdficon_small.png" alt="PDF of this article"/>

</a>

</cfif>

I want to say if remove_pdf field is undefined or if it’s defined but the value is not equal to true then show the pdf icon. When I dump the #cfData#, it shows remove_pdf value as undefined or true.

Views

753

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Why is my IF statement not working?

Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Here's my current IF statement that is not working.

<cfif (not structKeyExists(cfData, 'data.remove_pdf')) OR (structKeyExists(cfData, 'data.remove_pdf') AND #cfData.data.remove_pdf# neq "true")>

<a href="https://mysite.com/help/pdfexport/id/#cfData.data.id#">

<img src="/images/pdficon_small.png" alt="PDF of this article"/>

</a>

</cfif>

I want to say if remove_pdf field is undefined or if it’s defined but the value is not equal to true then show the pdf icon. When I dump the #cfData#, it shows remove_pdf value as undefined or true.

Views

754

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 31, 2018 0
LEGEND ,
Jan 31, 2018

Copy link to clipboard

Copied

Remove the quotes and the hashtags.

  1. <cfif (not structKeyExists(cfData, data.remove_pdf)) OR (structKeyExists(cfData, data.remove_pdf) AND cfData.data.remove_pdf neq "true")> 
  2. <a href="https://mysite.com/help/pdfexport/id/#cfData.data.id#"> 
  3. <img src="/images/pdficon_small.png" alt="PDF of this article"/> 
  4. </a> 
  5. </cfif>

V/r,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Are you referring to removing the quotes and hash tags on these lines?

  1. <a href="https://mysite.com/help/pdfexport/id/#cfData.data.id#">   
  2. <img src="/images/pdficon_small.png" alt="PDF of this article"/>   
  3. </a>  

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
LEGEND ,
Jan 31, 2018

Copy link to clipboard

Copied

No.. the quotes and hashtags on line 1.

V/r,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

This is what I have and it still failed at this IF statement.

<cfif (not structKeyExists(cfData, data.remove_pdf)) OR (structKeyExists(cfData, data.remove_pdf) AND cfData.data.remove_pdf neq true)>

I don't think there is any hash tags or quote in this line anymore.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
LEGEND ,
Jan 31, 2018

Copy link to clipboard

Copied

Hmm.. then you probably need to change it so that you are checking cfData.data as the struct and use remove_pdf as the key.  And I was wrong about the quotes. 

<cfif (not structKeyExists(cfData.data, 'remove_pdf')) OR

     (structKeyExists(cfData.data, 'remove_pdf') AND cfData.data.remove_pdf neq true)>

HTH,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Okay, this line has no error; however, it isn't working because because even when remove_pdf field has a "true" value, this IF statement still allows it to go through.

<cfif (not structKeyExists(cfData, 'data.remove_pdf')) OR (structKeyExists(cfData, 'data.remove_pdf') AND cfData.data.remove_pdf neq true)>

do something

</cfif>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
LEGEND ,
Jan 31, 2018

Copy link to clipboard

Copied

Are you setting the value as a boolean, or a string?  If as a string, then change the first line to = "true" instead of = true.

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

This could be rewritten as:

<cfif (NOT structKeyExists(cfData.data, 'remove_pdf')) OR (structKeyExists(cfData.data, 'remove_pdf') AND NOT cfData.data.remove_pdf)>

     do something 

</cfif>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

I used neq 'true' and neq "true" and both does not work. The value is string "true".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

This:

<cfif (NOT structKeyExists(cfData.data, 'remove_pdf')) OR (structKeyExists(cfData.data, 'remove_pdf') AND NOT cfData.data.remove_pdf)>

     do something

</cfif>

Only works when remove_pdf is true but when it's null, it errors out.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

I have tried:

<cfif (not structKeyExists(cfData, 'data.remove_pdf')) OR (structKeyExists(cfData, 'data.remove_pdf') AND cfData.data.remove_pdf neq 'true')>  

<h3>#cfData.data.remove_pdf#</h3>

</cfif>

And line 2 did show the value as "true", without the quotes. So, it proves that AND cfData.data.remove_pdf neq 'true' didn't work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

You'll have to be more specific about "it errors out".  If the value does not exist, then it will error for sure if you are trying to display

<h3>#cfData.data.remove_pdf#</h3>

I'd imagine at this point your conditional logic is pretty set, and the problem now exists further into your code.  A suggestion for when you troubleshoot conditionals in the future would be to cfdump each individual conditional so that you can get a peek into what is evaluating as true/false/etc, as well as what the value is.

-Nic

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

nic_tunney  wrote

You'll have to be more specific about "it errors out".  If the value does not exist, then it will error for sure if you are trying to display

<h3>#cfData.data.remove_pdf#</h3>

I'd imagine at this point your conditional logic is pretty set, and the problem now exists further into your code.  A suggestion for when you troubleshoot conditionals in the future would be to cfdump each individual conditional so that you can get a peek into what is evaluating as true/false/etc, as well as what the value is.

-Nic

The error is: Exception: Element REMOVE_PDF is undefined in a CFML structure referenced as part of an expression.

Again, this when remove_pdf value is null or in ColdFusion when I dump, it shows undefined. When the value of remove_pdf is true, there is no issue.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Sounds like it is doing exactly what you are telling it to.  Your condition passes if remove_pdf is not defined as a structure key.  If you try to reference that key once you enter the cfif block, you'll get an error if you try to access the key, since it doesn't exist.  Not quite sure what you are trying to do, but remove the not structKeyExists(cfData, 'data.remove_pdf') if you want to be sure it DOES exist inside your block.

-Nic

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Okay, so this line works.

<cfif (not structKeyExists(cfData, 'data.remove_pdf'))>

This will catch when the value of remove_pdf is undefined.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

nic_tunney  wrote

Sounds like it is doing exactly what you are telling it to.  Your condition passes if remove_pdf is not defined as a structure key.  If you try to reference that key once you enter the cfif block, you'll get an error if you try to access the key, since it doesn't exist.  Not quite sure what you are trying to do, but remove the not structKeyExists(cfData, 'data.remove_pdf') if you want to be sure it DOES exist inside your block.

-Nic

I'm not referencing that key after it passes.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

However, when I added this OR (structKeyExists(cfData.data, 'data.remove_pdf') AND NOT cfData.data.remove_pdf), it does not catch it when the value of remove_pdf is true.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Or this: OR (structKeyExists(cfData.data, 'data.remove_pdf') AND #cfData.data.remove_pdf# neq 'true')

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Sorry I didn't look at the new code.  You are still referencing it incorrectly.  The structure you are searching is cfData.data.  The key you are looking for is "removePDF".  You need to be consistent in your evaluation.  It should always look like this:

(structKeyExists(cfData.data, "remove_pdf")

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

nic_tunney  wrote

Sorry I didn't look at the new code.  You are still referencing it incorrectly.  The structure you are searching is cfData.data.  The key you are looking for is "removePDF".  You need to be consistent in your evaluation.  It should always look like this:

(structKeyExists(cfData.data, "remove_pdf")

Tried this and it's still pass through when the remove_pdf has a value of true.

<cfif (not structKeyExists(cfData, 'remove_pdf')) OR (structKeyExists(cfData.data, 'remove_pdf') AND #cfData.data.remove_pdf# neq 'true')>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Just for testing, I remove this (not structKeyExists(cfData, 'remove_pdf')) OR  from the line above and when the remove_pdf is true, it's now preventing it from entering into the IF statement like it should. So, the issue seems to be when I combine these two conditions together with an OR. Separately, they both work but when combined with OR the condition that checks for if when remove_pdf neq to true is no longer work.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
2Charlie LATEST
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

Okay, I think I got it working now.

<cfif (not structKeyExists(cfData.data, 'remove_pdf')) OR (structKeyExists(cfData.data, 'remove_pdf') AND #cfData.data.remove_pdf# neq 'true')>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

I doubt that you want to use data.remove_pdf as a string literal, but that is what is happening above.  What structKeyExists(cfData, 'data.remove_pdf') means is that the string data.remove_pdf is the literal name of the key it is looking for (cfData['data.remove_pdf']).  Not exactly sure what you are trying to do, but are you maybe looking to see if the remove_pdf key exists in data like structKeyExists(data, 'remove_pdf')?

-Nic

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0
Engaged ,
Jan 31, 2018

Copy link to clipboard

Copied

I want to test if the field remove_pdf is defined. If it is, is the value not equal true?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 31, 2018 0