Binding to non-standard Attributes Binding to non-standard Attributes Binding to non-standard Attributes

ColdFusion rocks, let me just start with that :)

Alright, now that we have that out of the way, I had an issue where I needed to bind a text field to a select menu, but not to the value attribute of that menu. I needed a bunch of other details from that select, and I wanted to prevent a bunch of unnecessary back and forth with the server. So I turned to ColdFusion's binding attributes.

Take the following code:

<cfform name="subscriptionSelection">
   <cfselect name="CostID">
      <option amount="30" description="This is product 1 yo!" value="1">My Product</option>
      <option amount="50" description="This is product 2 yo!" value="2">My Product Two</option>
   </cfselect><br />
   <cfinput type="text" name="cost" bind="{subscriptionSelection:CostID.amount}" />
   <cfinput type="text" name="description" bind="{subscriptionSelection:CostID.description}" />
</cfform>

And it works like a charm. When the select is changed, both the cost and description fields will pick up the custom attributes I added to the select. Now to be 100% standards compliant I'd need to build a custom DTD to include my custom attributes, but 99.9% of the time that's not necessary. Just take a moment and revel in the ease of using ColdFusion to do your dynamic bidding.

SQL Counting with Conditions SQL Counting with Conditions SQL Counting with Conditions

Not sure why I never thought of this before (and I know I'm not the first to come to this conclusion judging by Google), but I just figured out how to count records in a query based on their value.

Let's assume that you have a person table, and a property table. Each person has an unlimited number of properties which are either active or inactive, and you need to figure out how many they have of each. I've typically done this by using subselects or derived queries, but this is heavy on the server and requires a lot of additional table locking when you're not careful.

So instead of doing something like this:

SELECT
   Person.Name
   , Count(SELECT ID FROM Property WHERE PersonID = Person.ID AND Active = 1) AS ActiveProperty
   , Count(SELECT ID FROM Property WHERE PersonID = Person.ID AND Active = 0) AS Inactive Property
FROM Person

You can do this:

SELECT
   Person.Name
   , SUM(CASE WHEN Property.Active = 1 THEN 1 ELSE 0 END) AS ActiveProperty
   , SUM(CASE WHEN Property.Active = 0 THEN 1 ELSE 0 END) AS InactiveProperty
FROM Person
   INNER JOIN Property ON Person.ID = Property.PersonID

The second query is easier to read, and far easier on the database itself.

Incorrect Variable Values in AjaxOnLoad Incorrect Variable Values in AjaxOnLoad Incorrect Variable Values in AjaxOnLoad

I just ran into another goofy Ajax problem with my ColdFusion apps. I have a page using a bound cfdiv to load a form. The form, once submitted, is supposed to take the value of a form field entered on the page and run an AjaxOnLoad event to use that value elsewhere. Here's a simple test to show what I mean.

First, create the main page, test.cfm:

<cfsilent>
   <cfajaximport tags="cfform" />
</cfsilent>
<html>
<head></head>
<body>
   <cfdiv bind="url:form.cfm" />
</body></html>

Now in form.cfm, add the following code:

<cfset myvar = 2 />
<cfif IsDefined("FORM.fieldnames")>
   <cfset myvar = FORM.testfield />
   <cfset ajaxOnLoad("testFunction") />
</cfif>
<cfoutput>
<script type="text/javascript">
testFunction = function(){
   alert('#myvar#');
}
</script>
</cfoutput>
<cfform>
   <cfinput name="testfield" value="#myvar#" />
   <cfinput type="submit" name="submitbutton" value="Submit Test" />
</cfform>

When you submit the form, you'll get the value 2 alerted. I thought I would try to be sneaky and add an if statement around the script so that it was only output when the value wasn't 2, like so:

<cfset myvar = 2 />
<cfif IsDefined("FORM.fieldnames")>
   <cfset myvar = FORM.testfield />
   <cfset ajaxOnLoad("testFunction") />
</cfif>
<cfif myVar NEQ 2>
<cfoutput>
<script type="text/javascript">
testFunction = function(){
   alert('#myvar#');
}
</script>
</cfoutput>
</cfif>
<cfform>
   <cfinput name="testfield" value="#myvar#" />
   <cfinput type="submit" name="submitbutton" value="Submit Test" />
</cfform>

That just caused a javascript error that it couldn't find the testFunction script. So this tells me that the script is rendered to the page when the div is first loaded, and it's not updated on subsequent div reloads. So my way around this is to put the new value into a hidden form field, and then use ColdFusion.getElementValue() to pick up the value for my testFunction.

Hope this saves someone else some frustration.

Error Building Struct with CreateUUID Error Building Struct with CreateUUID Error Building Struct with CreateUUID

This is more for myself than anyone else, but I hit an odd snag in ColdFusion this morning. I got an error when trying to create a new structure member using a CreateUUID() function and ColdFusion's shorthand structure notation. The following code fails:

<cfset CurrentOrder.Cart[CreateUUID()] = {type = "foo"} />

The error I receive is that "Element 435A2A8C-0B35-C81F-4F36BE50E1B8F394 is undefined in a CFML structure referenced as part of an expression." Well duh! I'm trying to create it!

So I changed the code to this, which works perfectly:

<cfset myvar = CreateUUID() />
<cfset CurrentOrder.Cart[myvar] = {type="foo"} />

Does anyone have any idea why using the CreateUUID() function directly in the assignment statement would cause a problem?

Authorize.net and ARB Race Conditions Authorize.net and ARB Race Conditions Authorize.net and ARB Race Conditions

After trying for a week or so to figure out some race conditions with Authorize.net's Automated Recurring Billing, I finally decided to email them and see if they could tell me what was going on. Well it turns out their system is busted, and they don't plan on fixing it. Read on for the details.

Read more...

Powered by Mango Blog.