Skip to main content
Inspiring
Mar 31, 2019 8:38 PM UTC
Question

cfif isdefined ... AND ...

I have the following cfif statement:

<cfif status.datetime NEQ "" AND dateDiff('d',status.datetime,now()) GT 1 >

It works however in rare occasions status.datetime might not be defined. When I change it to:

<cfif isdefined("status.datetime") AND status.datetime NEQ "" AND dateDiff('d',status.datetime,now()) GT 1 >

it stops working. What am I doing wrong?

Thanks in advance for any help.

Gary

    This topic is closed to new replies. Start a new post to keep the conversation going.

    13 replies

    James Moberg
    Inspiring
    Apr 1, 2019 12:32 AM UTC

    Add an isDate() or isValid("date") to your statement prior to comparing it to ensure that value is a "date".

    While "" isn't a valid date/time value, the variable could also be NULL if returned from a database query.  (When returned from the database, the value is almost always NULL versus empty/blank.)

    <cfif isdefined("status.datetime") AND isDate(status.datetime) AND dateDiff('d',status.datetime,now()) GT 1>

    Translate
    ghanna1Author
    Inspiring
    Apr 1, 2019 1:39 AM UTC

    Thanks.  isDate() is a better way to check for the valid date then "". My problem with the isdefined not working is "status" appeared in two different queries.

    Translate
    WolfShade
    Legend
    Apr 1, 2019 1:12 PM UTC

    For some reason, there has been a long established issue with isDefined() and I would recommend not using it for anything.  Much more precise and less prone to error is StructKeyExists().

    <cfswitch expression="#StructKeyExists(status,'datetime')#">

         <cfcase value="yes">

              <!--- successful code here --->

         </cfcase>

    </cfswitch>

    HTH,

    ^ _ ^

    Translate