Passing arguments in ColdFusion Component [closed] - coldfusion

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need help determining the Coldfusion StructKeyExists function. Depending on the version I get different results. For instance:
I am Passing the argument as
<cfif StructKeyExists(arguments,'userid') and arguments.userid neq ''>
It works
now if i have the argument like this:
<cfif isDefined('arguments.structform.user') and arguments.structform.user neq ''>
it Works
if i pass above as:
<cfif StructKeyExists(arguments,'structform.user') and arguments.structform.user neq ''>
Coldfusion does not recognize it and skip it altogether
Using coldfusion 10

structKeyExists isn't recursive, it can only look for a key on the current struct. In your case, you'll need to test if arguments has a structform key, and then additionally check if arguments.structform has a user key.
<cfif StructKeyExists(arguments,'structform') and StructKeyExists(arguments.structform,'user') and arguments.structform.user neq ''>
It would probably be a lot easier if you just defined the argument.
<cfargument name="structform" type="struct" default="#{user:''}#">
Now you can assume the argument exists and just test the value of arguments.structform.user.
<cfif arguments.structform.user neq ''>
Even if you only defined the structForm argument with an empty struct, it would still be better than what you were originally doing.
<cfargument name="structform" type="struct" default="#{}#">
<cfif structKeyExists(arguments.structform, "user") and arguments.structform.user neq ''>

Related

Using ColdFusion, how do I replicate a compare statement with listFind?

I'm setting up something to allow me to masquerade as another user in my system. But, I want to use listFind instead of a compare() method.
<cfif compare(session.userName, "userOne") EQ 0>
<cfset #session.userName# = "userThree">
</cfif>
In the above statement, I'm trying to conform it to a listFind, where if userOne is currently logged in, then set the session.userName to userThree. But, I'm having trouble.
What I have thus far...
<cfif #ListFind("userOne, UserTwo")#>
<cfset #session.userName# = "userThree">
</cfif>
You will need to provide two independent arguments to ListFind. At the moment you are providing one string. The list is also the first arguments and string search for is the second.
<cfif ListFind("userOne", session.userName)>
<cfset session.userName = "userThree">
</cfif>
On a side note, the hashes are only necessary for string interpolation. In other words, only when you want a variable/expression to be evaluated and inserted into a string.

Check two arguments in ColdFusion?

I have two different arguments and if they are holding specific values I shouldn't execute the block of the code inside of my statement. Here is example:
<cfarguments name="myArg1" type="string" required="yes">
<cfarguments name="myArg2" type="string" required="yes">
<cfif myArg1 NEQ 'MMT' OR myArg2 NEQ 'newMMT'>
Execute the code
</cfif>
Arguments that I have passed above are myArg1 = 'MMT' and myArg2 = 'newCCS'. My cfif was indicating to true and code inside of the cfif was executed. So if myArg1 is equal to 'MMT' or myArg2 equal to 'newMMT' code inside of the if should not be executed. If I put AND instead of OR in that case I would be looking when both arguments are equal to the required values and that's not what I want. If you hav eany idea how I can get this to work please let me know. Thank you.
You need to use an AND statement. Also, don't forget to scope your variables.
<cfif arguments.myArg1 NEQ 'MMT' AND arguments.myArg2 NEQ 'newMMT'>

Is there any limitation with number queries/statements we can write inside cftransaction?

Today while fixing bugs in some existing code I found a strange error.
Branch target offset too large for short
After searching I found that it is something to do with Java byte code conversion. Here are the links I found:
Branch target offset too large for short
Branch Target Offset Error
Why does a long cfc file work in CF8, but not CF9? Getting "Branch target offset too large for short" error
In my case cftransaction contains around 870 statements and it's working fine. But I need to add 2 more queries to this transaction. Now I am getting this error when I am adding even one line of code inside cftransaction. Currently I can not move any of the existing cfquery out of the cftransaction.
Here is the overall structure of the code:
<cftransaction action="begin">
<cfif URL.action eq 'add'>
Around 200 lines of queries/statements
<cfelseif URL.action eq 'edit'>
Around 200 lines of queries/statements
</cfif>
<cfif URL.action eq 'add' or URL.action 'edit'>
Around 450 lines of queries/statements
</cfif>
</cftransaction>
Is there any workaround to fix this problem?
Branch offset has to do with the size of the module/function. It can also be caused due to a large conditional code block of cfif/cfelse or cfswitch.
Technically, I am not sure if there is any cap on the no. of queries you can put inside the cftransaciton block. It has nothing to do with the code migration from CF8 to CF9 but the length of your code inside conditional blocks.
I would want to split the function and try to put the each of the big sized conditional blocks as a separate function inside the cfc:
<cffunction name="myFunc1">
<cftransaction action="begin">
<cfif URL.action eq 'add'>
<!--- function call with your xxx lines of queries/statements --->
<cfinvoke component="MyCfc" method="firstQueryBlock" result="result1">
<cfelseif URL.action eq 'edit'>
<!--- second function call with your yyy lines of queries/statements --->
<cfinvoke component="MyCfc" method="secondQueryBlock" result="result2">
</cfif>
<cfif URL.action eq 'add' or URL.action 'edit'>
<!--- third function call with your zzz lines of queries/statements --->
<cfinvoke component="MyCfc" method="thirdQueryBlock" result="result3">
</cfif>
</cftransaction>
</cffunction>

ColdFusion Multi Check isDefined(session) and is not blank

I am trying to say if this isDefined and is not blank show this..
<cfif (isDefined("session.checkout.info.Certificate_2")) is not "">
I also tried:
<cfif (isDefined("session.checkout.info.Certificate_2")) neq "">
Will someone please tell me why this still shows when the string is empty?
Consider using structKeyExists (it has lower overhead than isDefined) but here you go:
<cfif
isDefined("session.checkout.info.Certificate_2")
AND session.checkout.info.Certificate_2 NEQ "">
If it exists it will then check to see if it's empty. If it doesn't exist, it won't check the value.

ColdFusion isDefined

I am trying to check to see if data exist in my form If data does not exist I want to assign it to O. How can I do this.
<cfif not isDefined("FORM.Age")>
cfset FORM.Age = "0"
<cfif>
Generally the best practice is considered to be to avoid isDefined. This is because isDefined will search all scopes until it finds a matching variable. So it's more efficient to use structKeyExists, eg:
<cfif NOT structKeyExists(form, "age")>
<cfset form.age = 0>
</cfif>
Also, another way to achieve this is to use cfparam, and specify 0 as the default:
<cfparam name="form.age" default="0">
You're almost there:
<cfif not isDefined("FORM.Age")>
<cfset Form.Age = 0>
</cfif>
Technically what you have is fine once you enclose the cfset in tags < and >. Assuming that omission is just a typo, could it be you are trying to use it with a text field?
Text fields always exist on submission. The value may be an empty string, but the field itself still exists, so IsDefined will always return true. If that is the case, you need to examine the field length or value instead. Then do something if it is empty according to your criteria. For example:
<!--- value is an empty string --->
<cfif NOT len(FORM.age)>
do something
</cfif>
... OR
<!--- value is an empty string or white space only --->
<cfif NOT len(trim(FORM.age))>
do something
</cfif>
... OR
<!--- convert non-numeric values to zero (0) --->
<cfset FORM.Age = val(FORM.Age)>
There are actually two things you want to to ensure. First, make sure this page was arrived at by submitting the proper form. Next, ensure you have a numeric value for the form.age variable. Here is an example of how you might want to code this:
<cfif StructKeyExists(form, "age") and cgi.http_referrer is what it should be>
<cfif IsNumeric(form.age) and form.age gt 0>
<cfset AgeSubmitted = int(form.age)>
<cfelse>
<cfset AgeSubmitted = 0>
</cfif>
...more code to process form
<cfelse>
...code for when page was not arrived at properly
</cfif>