Ask a Jedi: Find available UDFs

This post is more than 2 years old.

Brandon asks:

Is there a way to see what UDF's are available to the requesting template?

While there is probably some internal method that can do this, there is a little known function you can use to find out if a variable is a UDF. Let's start by creating a bunch of variables and UDFs.

<cfscript> a = 1; b = 2; c = 3; d = 'you betcha!';

function e() { return 'beer!'; }

f = [1,2,3]; g = {name='Ray',consuming=e};

function h() { return "more beer!"; } </cfscript>

<cffunction name="i"> <cfreturn "Stuff"> </cffunction>

Yes, those are horrible variable names, but you get the idea. In the above list, e, h, and i are udfs. Everything else is not. So how can we find them? First lets treat the variables scope like a structure:

<cfloop item="k" collection="#variables#">

Next use the isCustomFunction function:

<cfoutput> The variable #k# is <cfif not isCustomFunction(variables[k])>NOT</cfif> a UDF.<br /> </cfoutput>

That's it! If you run this you will see that it correctly recognizes which variables are UDFs.

In case you're curious, there isn't a direct way to tell if "X" is a built in function. You can use getFunctionList, which returns a struct, but you would then obviously need to do a structKeyList followed by a listFindNoCase.

I'll admit to have never using either of these functions in production. Has anyone used them?

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA