Jason
Jason

Reputation: 1977

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;
}

Upvotes: 1

Views: 996

Answers (2)

Henry
Henry

Reputation: 32905

isNull() is the function you're looking for.

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSe9cbe5cf462523a0-3c4c0bb81223f1daffb-8000.html

UPDATE: Although isDefined() or structKeyExists() might work, isNull() is the De facto function for checking if entityLoad() found anything. In fact, it was introduced in CF9 solely for doing this.

Upvotes: 6

BKK
BKK

Reputation: 2073

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

Upvotes: 2

Related Questions