Recent Posts

Returning an xml document from a coldfusion component – use cfcontent

So I was pretty annoyed how hard it was to find a description of how to return an xml document from a component back to an AJAX page that had sent a request to the component. I was making a simple form, and all I wanted was to make a simple call to a remoteService, and get a simple xml response. I didn’t really want an AJAX Framework/object for this simple task.

So after returning a simple string, the coldfusion service was adding the standard wddx xml tags around the string before it was received by the client page. So I learned you have to use a return type xml, instead of a return type string. But when I did that, even though the xml document looked well-formed, and how I had intended it to look with no wddx stuff, the receiving page was seeing null in the responseXML property of the XMLHttpRequest object.

So on this AJAX page, my XMLHttpRequest variable is called xmlHttp. And all I was getting was the xml doc as text in the xmlHttp.responseText property of the xmlHttp object. I wanted the xml doc to be in xmlHttp.responseXML and not the xmlHttp.responseText.

Using firebug (great FF add-on) I examined the reponse I was getting. Turns out the header of the response showed a content type of text/html and not an xml type. I finally figured out the key. In php yu could just add a header to set the content type to xml, using the header function. That is what I had been doing. Buth with coldfusion I had no idea how to add a content type header. THE KEY WAS IN THE CFCONTENT TAG. In my component being called, before I returned the xml object I had created, I learned you could just add a cfcontent tag to change the return type’s header.

<cffunction name="getSomethingInfo" access="public" output="false" returntype="xml">
<cfargument name="somethingID" type="string" required="true" />
<cfset var xml = "">
<cfquery datasource="adatasource" name="qThingInfo">
SELECT s2.*, u.* FROM Something s
INNER JOIN Something2 s2 on s2.userID = s.consultantID
INNER JOIN [User] u ON u.id = s2.userID
WHERE s.id = <cfqueryparam cfsqltype="cf_sql_integer" value="#somethingID#">
</cfquery>
<cfif qThingInfo.RecordCount EQ 0>
<cfxml variable="xml">
<noResults>
<requestSubject>SomethingInfo</requestSubject>
</noResults>
</cfxml>
<cfelse>
<cfoutput query="qThingInfo" maxRows="1">
<cfxml variable="xml">
<SomethingInfo>
<firstName>#UrlEncodedFormat(firstName)#</firstName>
<lastName>#UrlEncodedFormat(lastName)#</lastName>
</SomethingInfo>
</cfxml>
</cfoutput>
</cfif>
<!--- THIS IS KEY!!!--->
<cfcontent type="application/xml; charset=UTF-8">

<cfreturn xml />
</cffunction>

My code to send the request using a coldfusion component:

xmlHttp.open("GET", "RemoteServices/ARemoteService.cfc?method=getSomethingInfo"
+ cacheEntry , true);
// define the method to handle server responses
xmlHttp.onreadystatechange = handleRequestStateChange;
// make the server request
xmlHttp.send(null);

to call a coldfusion component and get a value from a method, the url for the request is:

PathtoYourComponent/YourComponent.cfc?method=yourMethodName&
param1=param1value&param2=param2value

Helpful Links:
http://www.coldfusionjedi.com/index.cfm/2007/2/8/Returning-XML-in-ColdFusion-for-AJAX

http://www.brucephillips.name/blog/index.cfm/2006/11/11/Use-ColdFusion-Function-Return-Type-of-XML-to-Provide-XML-to-Both-Spry-and-Flex

Cart/Session Data vs Form data

So I was making a form that was originally one page, to submit and order.

It got more complex when I learned I should probably get the tax/subtotal/verification from our order processor before submitting the final order.

So now I had a 2 part form. Part one: Enter line items Process part one: Calculate subtotal/tax and list info for verification Part2:Verify info and submit for final order processing.

Still, just 2 steps, not a very big deal, right? I didn’t want to worry about the session, a cart, etc. I ended up passing the info from step 1 (lineitems) and cloning it on step two into hidden form values, so these lineItems would be available when step 2 was processed.

The kicker for me to switch to use a cart was that in order for me to calculate the price from the lineItems, I needed the price and the totals to not be form data that could be manipulated by the user. Instead of adding hidden price fields on the form, and worry about that specific security issue of modifying form data pricing, I just decided to keep track of the pricing in the session, on the cart, which also reduced my amount of form value overhead, bu mades things a little more complex.

Element 1 is undefined in a Java object of type class coldfusion.runtime.Array

This error is thrown when you try and access an element in an Array that is undefined. How would this happen? Here’s what I did.

I wanted to make an array of 2 elements. I did this in a loop that ran 5 times. Instead of making an array of 2 elements, I made an array of 5 elements, only 2 of which were defined:

Here is the messed up array:

messed up array

So check your array, and make sure when it is being created that no undefined elements are created by skipping element assignments. Mae sure you don’t create an array, and then do something like:

<cfset items[3]="bbgun" />
<cfset items[5]="bb" />

Here, elements 1,2, and 4 will be undefined. Should be:

<cfset items[1]="bbgun" />
<cfset items[2]="bb" />

Great site for Photoshop Brushes

So I’ve discovered the power of brushes and symbols in Photoshop and Illustrator (CS3). Seriously, if you’re looking for some great graphics that are totally scalable (look photo realistic, but are vectorized) you had to familiarize yourself with brushes and symbols. Just google for some tutorials, or just use the Adobe Help, which is amazing. I love Adobe’s documentation for the most part. Even with work and developing in Coldfusion and Flex, the adobe livedocs, etc. are great.

Here’s a site with some cool brushes. I was amazed at the water drops, trees, and even the guns. It looks like a lot of people are starting to use http://www.deviantart.com too. Here’s the link I was talking about:

http://www.psbrushes.net/

Coldfusion Unity Reactor – Default behavior for zero values and nullable columns

I noticed when I was unit testing that when I was setting a reactor record field to zero (userrecord.setUsedFilespace(0)), and then saving the record, the database would not update the table’s value from null to zero.

Turns out, as Chris pointed out, that if a table has a column that allows nulls, any zero value will automatically be saved as NULL. So this column,of type bigint, had no way of getting a zero value into it through reactor, which would always set the zero value to null.

What we did was customize the userDAO. We changed the create and update methods. In the DAO folder of the project, the userDAO was edited. In the create and update methods we changed the code:

null=”#Iif(arguments.to.usedFilespace EQ 0 OR NOT Len(arguments.to.usedFilespace)

TO

null=”#Iif(NOT Len(arguments.to.usedFilespace)

And this allowed the updates and creates to use zero values.

Javascript variable names and ‘missing ; before statement’

So after being confused as to why I kept seeing the javascript error ‘missing ; before statement’, I realized that javascript variables can’t begin with a number.

My code was:

for (int i = 0; i < top; ++i) {
eval("form." + <span style="color:red">fieldsArray[i]</span> + ".value");
}

fieldsArray contains a string ‘0to99scans’ referring to a form input ‘0to99scans’. This should have been something like ‘zeroTo499scans’.

Accessing data in a coldfusion query object

The syntax is:

queryvariable.columnname[rownumber]

It would seem that the query datatype is an array of structs. Indeed as I read some posts online, I discovered a query is like an array of structs. But the query object is a special data type in coldfusion, like an array or a struct. See the Adobe article linked below.

I don’t know why this was so hard to find in the livedocs, or why the ColdFusion documentation for cfquery didn’t have any useful links, but Joel was able to point out to me right off that you can access the data in the rows/fields of a cfquery easily.

I’ll quote Adobe’s livedocs here, in a buried artilce. I ended up finding this useful page with a google search.

“A query object, sometimes referred to as a query, query result, or record set, is a complex ColdFusion data type that represents data in a set of named columns, similar to the columns of a database table.”

“You reference query columns by specifying the query name, a period, and the column name”

“You can access query columns as if they are one-dimensional arrays.”

Ex:<cfset myVar = myQuery.Employee[2]>”

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Variables_17.html