– Common Function Library Project

isDefinedValueMX(varname [, value])

Last updated October 20, 2003

Version: 1 | Requires: ColdFusion MX | Library: DataManipulationLib

Rated 1 time(s). Average Rating: 5.0

Use like isDefined for variables plus confirm the variable has an assigned value. For a named variable, if it is defined and has value returns 1, if it is not defined returns 0; if it is defined and has empty value returns 0. Optionally, allows specifying a check value to test the named variable for a specific value. This extends the function by, if it is defined and has this value, then return 1. A checked value must be a simple value or an empty string. isDefinedValueMX improves on the isDefinedValue CF5 version and built-in isDefined by returning a 0 on error conditions. Usage Notes. 1) Array indexes, like myArray[1]. are checked by first removing the index to confirm the array is defined, then checks the indexed location for a simple value. If the index is missing or bogus, the function returns 0. 2) Query record set results, checks the first record?s value, not the entire record set. 3) Queries results with a zero record count return 0. Example: <cfparam name="url.test" default="1"> isDefinedValue("url.test") returns 1 <cfparam name="url.test" default=""> isDefinedValue("url.test") returns 0 and isDefinedValue("url.test",??) returns 1

Return Values:
1 (yes, it is defined) or 0 (no, it is not defined)


view plain print about
<cfset = structnew()>
<cfset MyNewArray[1] = "test"> 
<cfset MyNewArray[2] = ""> 
<CFPARAM name = "" DEFAULT = "">
<CFPARAM name = "simplevalue" DEFAULT = "#Now()#">
<cfparam name="" default="">
<cfparam name="url.test" type="boolean" default="1">
<cfoutput>#isDefinedValue("url.test")# </cfoutput>

<br><br> simplevalue
<cfif isDefinedValueMX("simplevalue")>
<br> happy <cfdump var="#simplevalue#"> 
<br> no joy simplevalue

<cfif isDefinedValueMX("")>
<br> happy <cfdump var=""> 
<br> no joy Structure Key

<cfif isDefinedMX("" )>
<br>happy <cfdump var=""> 
<br> no joy Structure

<br><br> MyNewArray [1]
<cfif isDefinedValueMX("MyNewArray[1]")>
<br> happy <cfdump var="#MyNewArray#">

<br> no joy MyNewArray

<br><br> MyNewArray [2]
<cfif isDefinedValueMX("MyNewArray[2]")>
<br> happy <cfdump var="#MyNewArray#">
<br> no joy MyNewArray

<cfif isDefinedValueMX("")>
<br> happy <cfdump var="">
<br> no joy
<!--- make a query on the snippets datasource --->
<br><br> With No Query
<cfif IsDefinedValueMX("qGetEmployees")> 
<br> <cfdump var="#qGetEmployees#"> 
<br> no joy qGetEmployees

<br><br> With Results Query
<cfdirectory action="list" directory="#expandPath("/WEB-INF/debug/")#" name="getFiles">

<cfdump var="#getFiles#">

<br><br> With Results Query<br>
<cfif IsDefinedValueMX("getFiles")> 
Yes, the query exists and is not empty

<br><br> With Results Query on field
<cfif IsDefinedValueMX("getFiles.size")> 
<br> Yes, the field "size" is defined in the query "getFiles"

<br><br> With No Results Query
<cfquery name = "getFilesFiltered" dbType="query">
FROM getFiles
WHERE name = 'IDoNotExist'

<cfif NOT IsDefinedValueMX("getFilesFiltered")> 
<br> No, this query is empty


Name Description Required
varname The name of the variable to test for Yes
value The value a simple variable should be to pass the test (optional) No

Full UDF Source:

view plain print about
 * Checks that a variable exists and has value. CFMX version.
 * @param varname      The name of the variable to test for (Required)
 * @param value      The value a simple variable should be to pass the test (optional) (Optional)
 * @return 1 (yes, it is defined) or 0 (no, it is not defined) 
 * @author Joseph Flanigan ( 
 * @version 1, October 20, 2003 

function isDefinedValueMX(varname)
  var varvalue = "";
    if (IsDefined(listfirst(Arguments[1],"[")))
     varvalue = evaluate(Arguments[1]);

     if (IsSimpleValue(varvalue))
            if (ArrayLen(Arguments) EQ 2 )
                { if ( varvalue EQ Arguments[2]){return 1;}
                else return 0; 
            else if ( find(varvalue,"" )) {return 0;}  
            else return 1;  // something is there, just not testing for it.
     else if (IsStruct(varvalue))
            if (StructIsEmpty(varvalue)) { return 0;} 
            else {return 1;}
     else if (IsArray(varvalue))
            if (ArrayIsEmpty(varvalue)) {return 0;} 
            else {return 1;}
     else if (IsQuery(varvalue))
            if (YesNoFormat(varvalue.recordcount)) {return 1;} 
            else {return 0;}
    return 0; // not defined
     } //try
     catch(Any excpt)
      { return 0;} // return excpt.Message;
return 0; 
blog comments powered by Disqus


Latest Additions

CF Ninja CF Ninja added
13 day(s) ago

Stephen Withington Stephen Withington added
13 day(s) ago

Adam Cameron Adam Cameron added
a while ago

Ray Ford Ray Ford added
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson