If structKeyExists and value in this case

What I am trying to do is see session.checkout.info.gender_1

if it exists if it also checks if it is Male or Female. Whichever is the case for cfpdfformparam, either M or F to better fit the shape. I've tried "IsDefined", not "". Anyone tell me where I am going wrong?

    <cfif structKeyExists(session, 'checkout.info.gender_1') and trim("session.checkout.info.gender_1") neq "Female">
        <cfpdfformparam name="sex" value="M">
</cfif> <!---Section1 Owner Sex--->
    <cfif structKeyExists(session, 'checkout.info.gender_1') and trim("session.checkout.info.gender_1") neq "Male">
        <cfpdfformparam name="sex" value="F">
</cfif>

      

+3


source to share


4 answers


You should add your tests. Your test should be the same ...



<cfif isDefined("session")
    and structKeyExists(session, 'checkout') 
    and structKeyExists(session.checkout, 'info') 
    and structKeyExists(session.checkout.info, 'gender_1') >

    <cfif session.checkout.info.gender_1 eq "Female">
         do this
    <cfelse>
         do that
    </cfif>

</cfif>

      

+4


source


In your example code, the function trim

actually truncates the string "session.checkout.info.gender_1", not the variable. Please remove the quotes and try again.



<cfif structKeyExists(session, 'checkout.info.gender_1') and trim(session.checkout.info.gender_1) neq "Female">
    <cfpdfformparam name="sex" value="M">
</cfif> <!---Section1 Owner Sex--->
<cfif structKeyExists(session, 'checkout.info.gender_1') and trim(session.checkout.info.gender_1) neq "Male">
    <cfpdfformparam name="sex" value="F">
</cfif>

      

+4


source


This is a formatted comment. Eliminate the if / else logic like this:

<cfif myVariable is theExpectedValue>
yes
<cfelse>
no
<cfdump var = "expected #theExpectedValue# and got #myVariable#>
</cfif>

      

After choosing the logic, insert the actual code for each condition.

+1


source


try it

<cfif structKeyExists(session, 'checkout.info.gender_1') and len(trim(session.checkout.info.gender_1))>
    <cfpdfformparam name="sex" value="#UCase(Left(Trim(session.checkout.info.gender_1),1))#">
<cfelse>
    <cfpdfformparam name="sex" value="Undefined">
</cfif>

      

You may never need your pdf report to say undefined for gender, but it will at least allow you to do some debugging.

However, since you are checking for gender, it seems like this is optional, in which case you can use the same code to set the gender value to X

or Not Set

when the variable is undefined or empty.

And since you are mapping Male to M and Female to F, we can use Left()

to just get the first letter.

0


source







All Articles