Friday, April 5, 2013

Standard responses with Response.cfc

Standardizing responses is useful so you know what to expect when you make a service call. That is why I wrote Response.cfc (github repo link).

This response object by default returns a json packet so it can be easily manipulated by AJAX and remote API requests.  For non-AJAX/non-remote calls, I like to deserialize the json immediately after getting the response.

Properties of Response.cfc are below with sample use cases.

  • data (used to pass back context data)
  • errorCode (used to pass a unique code)
  • errorDetail (used for debugging)
  • errorMessage (used for generic messaging back to the user)
  • errors (each error to report back to the user)
  • statusCode (http status code for use by remote calls)
  • statusText (http status text for use by remote calls)
  • success (boolean flag for overall status of request)

Credit to Ben Nadel's Building An AJAX, jQuery, And ColdFusion Powered Application post for inspiring this.

Wednesday, July 25, 2012

Validate client-side with dataset property

There are many new and exciting things in HTML5, but one of the interesting shifts it has made in my programming is for client-side form validation using the new dataset property. This was also possible pre-HTML5 via the getAttribute() method.

The code below will first try to use the new dataset property. If that fails it will fallback to trying the getAttribute() method. This ensures that all browsers will behave the same regardless of their support for HTML5. And for good measure there is another fallback to a very generic message.

To take this a step further, you could use a dynamic programming language like ColdFusion to dynamically inject the message. You could even have it display a language-specific message (i.e., English, Spanish, etc.).

Thursday, July 12, 2012

t-sql get first item from list using substring and charindex

For those of us who have inherited non-normalized databases where lists are stored in a single column, it may be helpful to be able to get the first item in a list straight from your query. ColdFusion can do this by using ListFirst() once you return the query results, but I wanted to try to do the same in T-SQL. This would be useful if, for example, the first item in the list is the parent and the other items are children. The query below will help you identify the parent.



The first query which uses @InvalidList, will return 'Item1-Item2-Item3' because it does not match the list delimiter. The second query which uses @ValidList, will return 'Item1' only since it matched the list delimiter. The REPLACE is used to obviously strip the delimiter. Note the use of LTRIM and RTRIM to trim all spaces so our value is clean.

Searching on first character with CHARINDEX

Have you ever wanted to build an alphanumeric search allowing the user to click an individual letter or number range and then display the matching results? There are numerous ways to do this in multiple languages, but one way I found that works great is to just use CHARINDEX in T-SQL.



This query will return any record whose "table_name" begins with 'a', '1', 'm', or 's'. Obviously to match just on 'm', you would set @AlphaNumChars to 'm'. You could turn this into a stored procedure or keep it as an inline query. What other methods do you use? Let us know by leaving a comment.

Wednesday, June 6, 2012

t-sql display date, time, time period

Quick post on how to display date, time, and time period in T-SQL:

Friday, May 4, 2012

IsDefined invalid variable name error

It is widely known that StructKeyExists is faster than IsDefined since IsDefined still searches all scopes for the variable until it finds it, whereas StructKeyExists explicitly defines the scope to search.

Sean Corfield also explains why IsDefined can be confusing and arguably buggy.

I ran into another buggy situation at work this week where we were checking for the existence of a variable that may or may not have a hyphen in the variable name. I know it's bad practice to have a hyphen in a variable name, but this was inherited code and the variable name was set dynamically at runtime based off a varchar ID (that's another story).

The code below will blow up when using IsDefined if the variable does not exist because you cannot have hyphens in a ColdFusion variable name. The interesting thing is that it works if the variable does exist. Of course, StructKeyExists works as expected even with the hyphen.

Monday, September 5, 2011

Easy method for setting cfqueryparam null attribute

One function I'm surprised that wasn't built-in to ColdFusion until CF9 is isNull(). Since my company is currently on CF8, I decided to create my own function.  This was useful for me when trying to set the boolean value in cfqueryparam's null attribute.

One solution would be to set a local flag by evaluating len(trim(myvar)), but I thought calling a cfc made more sense, and it is still pretty straightforward.

Feel free to use this code in your non-CF9 applications. I only developed this for use with simple strings, so use with caution. I have now updated the code to handle strings, structs, arrays, and queries. I have not tested nested structures yet. Has anyone used CF9's isNull() function for more complex structures?