Criteria Builder and Conditional Tests

I have a search form which has a dropdown list of primary keys to a related entity.

With the following code, if I select a dropdown value, all is good. If I don’t, the call fails as local.objCourseType is NULL

public any function getSearchResults(searchCriteria) {
if (arguments.searchCriteria.type == “”) {
arguments.searchCriteria.type = “not supplied”;
}

local.objCourseType = entityNew(“CourseType”).get(arguments.searchCriteria.type);

local.objCriteria = newCriteria();

local.result = local.objCriteria.iLike(“code”, arguments.searchCriteria.code & “%”)
.and(
local.objCriteria.restrictions.iLike(‘fullTitle’, arguments.searchCriteria.fullTitle & “%”)
,local.objCriteria.restrictions.eq(‘type’, local.objCourseType)
,local.objCriteria.restrictions.eq(‘offerStatus’, arguments.searchCriteria.offerStatus)
)
.list(sortOrder=“fullTitle”);

return local.result;
}

How can I conditionally search for my CourseType?

Richard

Richard, shouldn’t the get() with an uknown id give you a null object becaue it did not exist.

Yes, it does give me a null object but it seems as the…

local.objCriteria.restrictions.eq(‘type’, local.objCourseType)

…is searching for that null object which is causing nothing to be found.

Can I put some conditional test in .and() to ignore it when the object is null?