Skip to main content
Participant
February 3, 2008
Question

correct syntax to check if return value is NULL inside Cffunction

  • February 3, 2008
  • 2 replies
  • 638 views
Hi,

Can someone tell me how to check for a Null return value inside a cffunction?. Here's my code:
<cffunction name="getNewAgreementId" access="public" returntype="numeric" hint="Returns new ID." output="false">
<cfset newAgreementId=0>
<cfquery name="getLastAgreementId" datasource="#application.DSN#">
SELECT MAX(Agreement_Id) AS LastAgreementId
FROM Agreements
</cfquery>
<cfif getLastAgreementId.recordcount EQ 0 OR getLastAgreementId.LastAgreementId IS NULL>
<cfset newAgreementId=1>
<cfelse>
<cfset newAgreementId = getLastAgreementId.LastAgreementId + 1>
</cfif>
<cfreturn newAgreementId>
</cffunction>

The result of the cfquery is null.

Getting an error:
Variable NULL is undefined.
    This topic has been closed for replies.

    2 replies

    Inspiring
    February 3, 2008
    cf does not really have a NULL value...
    your error comes from this line:
    <cfif getLastAgreementId.recordcount EQ 0 OR
    getLastAgreementId.LastAgreementId IS NULL> and is due to a simple
    syntax error: IS NULL - cf assumes NULL is a var name since it is not
    enclosed in quotes.
    you do not really need the second half of the above cfif statement since
    if your cfquery returns NULL as result it means that its recordcount = 0
    you can also use you db-specific functions to make sure your query never
    returns NULL (or rather returns something else instead of NULL, i.e. 0)
    - this can eliminate the need for your entire cfif block in question and
    make you code neater...

    ---
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com
    Inspiring
    February 3, 2008
    Cold Fusion converts null to an empty string.