Search code examples
coldfusioncoldfusion-9

checking whether or not entityLoad with filtercriteria returned a result or not


I am calling entityLoad like this:

currentSubmission =  entityload("EventSubmission", { eventID = variables.eventID, profileID = variables.profileID, true }

How do I check if the entityLoad has returned a record or not? If there is a match on the filtercriteria, an Object is returned. If there is no match, nothing is returned and the variable currentSubmission doesn't exist.

The problem I am finding is that if I use:

a. isObject(currentSubmission), gives an error when nothing is returned, as currentSubmission doesn't exist.

b. isDefined(currentSubmission), gives an error when something is returned as you can't perform isDefined on an object.

So the quesiton is, what method should I use to determine whether or not entityLoad returned a result or not?

Here is the full method I am trying to put together. Basically, I want to load an entity based on some filter criteria and return it, if there is no matching entity, then return a new Empty entity.

public function getByEventProfile(){
    currentSubmission =  entityload("EventSubmission", { eventID = variables.eventID, profileID = variables.profileID }, true);
    if (!isObject(currentSubmission))
        currentSubmission = entityNew("EventSubmission");
    return currentSubmission;
}

Solution

  • Try isDefined("currentSubmission") (note the quotes) or slightly faster structKeyExists(variables,"currentSubmission")