Question

I have an error while looping over a query using cfloop.

When I use a cfdump on the query (inside the loop, mind you), I can see all the data just fine. But when I try to get the value of each variable as you normally do in cfloop, I get a message that says they are undefined. I then changed each variable to reference the query specifically, and now the problem is that the variable is undefined in the query. Here's the code:

<cffunction name="writeCourses">
<cfargument name="recordset" required="yes" type="query">
     <cfif recordset.RecordCount NEQ 0>
         <cfset temp = "">  
         <cfoutput>
         <cfloop query="recordset">     
         <!--- <cfdump var="#recordset#"> <cfabort/> --->  

            <cfset temp = temp & "<strong>#recordset.courseType# #recordset.courseNum# ">
            <cfif isDefined("recordset.courseTHM") AND recordset.courseTHM EQ 1>
                <cfset temp = temp & "(#left(recordset.courseNum,3)#4) ">
            </cfif>
            <cfif isDefined("recordset.courseName")>
                <cfset temp = temp & "#recordset.courseName# </strong><br>">
            </cfif>
            <cfset temp = temp & "#recordset.courseDESC#<br>">
            <cfset temp = temp & "#recordset.courseHours#<br><br>">
        </cfloop>
        </cfoutput>
     <cfelse>
        <cfset temp = "">
     </cfif>
 <cfreturn temp>
</cffunction>

So as you can see, each variable is enclosed in ## tags. Originally none of them were proceeded by recordset. but they were still undefined. And when I uncomment the cfdump and cfabort tags, those work fine and I can see the recordset query with all the data as it should be.

Every other time I have used cfloop with a query it works as expected. Also, I did not write this code, I am having to modify it (the original author no longer works here).

Here's an example of the recordset dump:

enter image description here

The error message:
Detail: [empty string]
ErrNumber: 0
Message: Element COURSETYPE is undefined in RECORDSET.
Resolvedname: RECORDSET

The error line is:

   <cfset temp = temp & "<strong>#recordset.courseType# #recordset.courseNum# ">   
   <cfif isDefined("recordset.courseTHM") AND recordset.courseTHM EQ 1>
   <cfset temp = temp & "(#left(recordset.courseNum,3)#4) ">
   </cfif>
   <cfif isDefined("recordset.courseName")>
   <cfset temp = temp & "#recordset.courseName# </strong><br>">
   </cfif>

That's all one line :/

The stored procedure/function calling the above:

<cffunction name="getCoursesByDept">
<cfargument name="deptCode" required="yes" type="string">
<CFSTOREDPROC procedure="dbo.GetCourses"     datasource="WebCatalog">
    <CFPROCPARAM type="IN" dbvarname="@deptCode" value="#deptCode#" cfsqltype="CF_SQL_CHAR">
    <CFPROCRESULT name="result">
</CFSTOREDPROC>
<cfinvoke method="writeCourses" recordset="#result#" returnvariable="output">
<cfreturn output>
</cffunction>
Was it helpful?

Solution

Your problem appears to be failure to scope. Here are your first 4 lines:

<cffunction name="writeCourses">
<cfargument name="recordset" required="yes" type="query">
 <cfif recordset.RecordCount NEQ 0>
     <cfset temp = "">  

Try it like this:

<cffunction name="writeCourses">
<cfargument name="recordset" required="yes" type="query">
<cfset var temp = "">  
<cfif arguments.recordset.RecordCount NEQ 0>

The differences are the use of the var keyword for your local variable temp, and adding the arguments scope to the recordset variable.

OTHER TIPS

(In addition to Dan's comments ...)

If [the procedure] can't find anything it returns a query containing an error message (i.e. no course found)

Then that means the COURSETYPE column does not always exist in the resultset, which is exactly what the error message is reporting. If the procedure returns any result, regardless of the contents, the code inside the cfif block will execute. Since the first line of code uses that column, without verifying it exists, it would cause the exact error you are seeing.

Also, as I mentioned in the comments, you really need to localize the function variables result, output, temp, etectera. Lack of var scoping can create problems, even within same page, if you reuse variable names. As @Dan suggested you should fully scope all variables - in particular, the function arguments.

(As an aside, I understand you are modifying existing code, but the error message should really be handled in CF, not inside the procedure. The procedure's job is just to return data. The CF code should check the recordCount and take the appropriate action if no records are found.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top