[ColdBox 3.7.0] CriteriaBuilder Error

Hi Guys,

I’m taking my first steps into using CriteriaBuilder in a new project I’m working on and I’m getting the below error from what I thought was a simple bit of code.

`

var criteria = personService.newCriteria();
rc.people = criteria.ilike(“firstName”,"#rc.filter#%")
.order(“firstName”,“asc”)
.list(offset=rc.paging.startrow, max=getSetting(“PagingMaxRows”),timeout=3000, ignoreCase=true, asQuery=true);
rc.peopleCount = criteria.ilike(“firstName”,"#rc.filter#%").count();

`

here’s the error which happens on the last line

`
rc.peopleCount = criteria.ilike(“firstName”,"#rc.filter#%").count();

`

Application Execution ExceptionError Type: Expression : [N/A]

Error Messages: Variable RESULTS is undefined.

Stack Trace
`

coldfusion.runtime.UndefinedVariableException: Variable RESULTS is undefined.
	at coldfusion.runtime.CfJspPage._get(CfJspPage.java:377)
	at coldfusion.runtime.CfJspPage._get(CfJspPage.java:339)
	at coldfusion.runtime.CfJspPage._autoscalarize(CfJspPage.java:1447)
	at cfCriteriaBuilder2ecfc1609076048$funcCOUNT.runFunction(E:\coldbox\system\orm\hibernate\CriteriaBuilder.cfc:214)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:491)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:337)
	at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2360)
	at cfPeople2ecfc472383962$funcINDEX.runFunction(E:\tap\handlers\People.cfc:54)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:517)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:496)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:355)
	at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2301)
	at coldfusion.tagext.lang.InvokeTag.doEndTag(InvokeTag.java:389)
	at coldfusion.runtime.CfJspPage._emptyTcfTag(CfJspPage.java:2723)
	at cfController2ecfc1154376470$funcINVOKER.runFunction(E:\coldbox\system\web\Controller.cfc:756)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2582)
	at cfController2ecfc1154376470$funcRUNEVENT.runFunction(E:\coldbox\system\web\Controller.cfc:640)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:517)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:496)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:355)
	at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2301)
	at cfColdbox2ecfc923304934$funcPROCESSCOLDBOXREQUEST.runFunction(E:\coldbox\system\Coldbox.cfc:236)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2582)
	at cfColdbox2ecfc923304934$funcONREQUESTSTART.runFunction(E:\coldbox\system\Coldbox.cfc:382)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:491)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:337)
	at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2360)
	at cfApplication2ecfc395267276$funcONREQUESTSTART.runFunction(E:\tap\Application.cfc:96)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
	at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
	at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
	at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
	at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
	at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:491)
	at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:337)
	at coldfusion.runtime.AppEventInvoker.invoke(AppEventInvoker.java:88)
	at coldfusion.runtime.AppEventInvoker.onRequestStart(AppEventInvoker.java:258)
	at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:349)
	at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
	at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
	at coldfusion.filter.PathFilter.invoke(PathFilter.java:94)
	at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70)
	at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
	at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
	at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46)
	at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
	at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
	at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
	at coldfusion.CfmServlet.service(CfmServlet.java:200)
	at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
	at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
	at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
	at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
	at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
	at jrun.servlet.FilterChain.service(FilterChain.java:101)
	at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
	at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
	at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
	at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
	at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
	at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:320)
	at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
	at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:266)
	at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

`

I’m running CF9. I don’t know if I’m missing something really obvious but any help would be much appreciated.

Thanks,

Richard

Richard,

You need to either do a new criteria or just do the .count() first, then the .order() and .list()

Var c = personService.newCriteria();

C.ilike(“firstName”,"#rc.filter#%);

Rc.peopleCount = c.count();
Rc.people = c.order(“firstName”,“asc”).list(offset=rc.paging.startrow, max=getSetting(“PagingMaxRows”),timeout=3000, ignoreCase=true, asQuery=true);

Should do it.

Curt

Thanks Curt,

That fixed it. If I had only read the note on the docs that says you must call count() before list().

Anyway we live and learn.

Thanks,

Richard

Just for future reference, you can also do a sort by sending the sortOrder argument to list(), eg.

c.list( sortOrder=“LastName ASC” );

It’s not as flexible if you need to build sort order dynamically, but works nicely for simple cases.

Aliso when you do counts as best practice I pass the primary Id to avoid duplicates.

Is it possible for us to enhance the criteria builder code to check for bad-apple scenarios like that and throw a helpful error that explains what the developer did wrong and how to fix it.

For instance, “You are calling list() before count() and that won’t work. Please call count() before calling list().

That definitely seems better than “Variable RESULTS is undefined.”

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

That would certainly help. The error did confuse me a bit. Though it doesn’t take much :slight_smile:

Richard