Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:40:12+00:00 2026-06-05T21:40:12+00:00

I am trying to write a session helper and facing to problem to test

  • 0

I am trying to write a session helper and facing to problem to test if a Struct key in session exists?

I am trying like

<cffunction name="Exists" access="public" output="false" returntype="boolean" >
    <cfargument name="Key" required="true" type="Any" />
    <cfreturn Evaluate( "StructKeyExists( Session, #Arguments.Key# )" ) />
</cffunction>

Where I am calling the function like

<cfif Exists("data.fromdate") >
 ...
</cfif>

How should I write it?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:40:14+00:00Added an answer on June 5, 2026 at 9:40 pm

    The following code will check any depth struct, and also correctly locks the Session scope.

    <cffunction name="Exists" access="public" output="false" returntype="boolean">
        <cfargument name="Key" required="true" type="string">
    
        <cfset local.mainKeyList = ListChangeDelims(ListDeleteAt(Arguments.Key, ListLen(Arguments.Key, "."), "."), ",", ".")>
    
        <cfset local.StructChain = "Session">
        <cfloop list="#local.mainKeyList#" index="local.CurrentKey">
            <cfset local.StructChain &= '["#local.CurrentKey#"]'>
        </cfloop>
    
        <cflock scope="session" type="readonly" timeout="3">
            <cftry>
                <cfset local.Exists = StructKeyExists(Evaluate(local.StructChain), ListLast(Arguments.Key, "."))>
    
                <cfcatch>
                    <cfset local.Exists = false>
                </cfcatch>
            </cftry>
        </cflock>
    
        <cfreturn local.Exists>
    </cffunction>
    
    <cflock scope="session" type="exclusive" timeout="3">
        <cfset Session.data.log.deep = "I'm here!">
    </cflock>
    <cfoutput>#Exists("data.log.deep")#</cfoutput>
    

    Hopefully the amount of code in this function will justify why a helper function would be useful, to those pondering your reasons. This doesn’t currently, but could be enhanced to, deal with Structs inside of Arrays as well. This also doesn’t deal with an empty Arguments.Key, or fail gracefully on a cflock timeout, but should get you started.

    Additionally, those that want to comment that cflock isn’t required, please read the ColdFusion cflock docs first.

    Simplified, but may provide inaccurate results in extremely rare conditions

    Doing an IsDefined inline in your code will provide the opportunity for false positives, however having the IsDefined inside a udf or cfc method reduces this risk greatly to the point it may not need be a consideration. If you’re happy to take that chance then you can simplify the function using IsDefined as Peter Boughton suggests.

    <cffunction name="Exists" access="public" output="false" returntype="boolean">
        <cfargument name="Key" required="true" type="string">
    
        <cflock scope="session" type="readonly" timeout="3">
            <cfset local.Exists = IsDefined("Session." & Arguments.Key)>
        </cflock>
    
        <cfreturn local.Exists>
    </cffunction>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a unit test for the following class: @Transactional public
I'm trying to write basic assert test: def assert_session_has ( sessionvar ) return assert_not_nil
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write a function to see how often an object exists and give
I am trying to write a simple integration test on a Rails 3.1 application
I'm trying to start a new user session by creating a helper that POSTs
i'm trying to write a php page that call for a server program like
I am trying to write some plugins to work with the Terminal Services Session
I am trying ot write an integration test for one of my controller classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.