I'm trying to find out if a url variable exists, and if it doesn't, make sure that it's not empty.
This does not work:
<cfif IsDefined('URL.affiliateId') and is not "">
//
</cfif>
<cfif structKeyExists(url, 'affiliateID') and trim(url.affiliateID) neq "">...</cfif>
You can simplify the logic a bit as well by using <cfparam> to ensure that the URL variable always exists. Then rather than having 2 conditions, you just need 1.
<cfparam name="URL.affiliateId" type="string" default="" />
<cfif trim( URL.affiliateId ) is not "">
do stuff here
</cfif>
To ignore most white space
<cfif IsDefined('URL.affiliateId') and len(trim(URL.affiliateId))>
value is defined and not empty
</cfif>
... or alternately
<cfif IsDefined('URL.affiliateId') and len(trim(URL.affiliateId)) gt 0>
value is defined and not empty
</cfif>
<cfif IsDefined('URL.affiliateId') and URL.affiliateId neq "">
//
</cfif>
I will just sum up the answers and offer my version of it:
<cfparam name="URL.affiliateId" type="string" default="" />
<cfif len(trim(URL.affiliateId))>
...do something with the affiliate...
</cfif>
You do not need structKeyExists or isDefined and it would be better to avoid them. Also you do not need the 'greater than zero' part after the 'len()'.
Related
I am trying to say if this makeCodeNumber is defined and is not null use this
<cfpdfformparam name="make" value="#session.checkout.vehicle.makeCodeNumber#">
but if it is null (or empty) then use this
<cfpdfformparam name="make" value="#session.checkout.vehicle.vehiclemake#">
All the code
<cfif isDefined("session.checkout.vehicle.makeCodeNumber")>
<cfif len(trim("session.checkout.vehicle.makeCodeNumber"))>
<cfpdfformparam name="make" value="#session.checkout.vehicle.makeCodeNumber#">
<cfelse>
<cfpdfformparam name="make" value="#session.checkout.vehicle.vehiclemake#">
</cfif>
</cfif>
I am showing that it is null (or empty string) when I dump the variables but for some reason I must be overlooking something because when it is null it still is only going by <cfpdfformparam name="make" value="#session.checkout.vehicle.makeCodeNumber#"> Instead of <cfpdfformparam name="make" value="#session.checkout.vehicle.vehiclemake#">.
Any help would be greatly appreciated!
You're evaluating the len(trim()) of the string "session.checkout.vehicle.makeCodeNumber" not the value of the variable session.checkout.vehicle.makeCodeNumber. You need to remove your " in your second if statement
<cfif isDefined("session.checkout.vehicle.makeCodeNumber")>
<cfif len(trim(session.checkout.vehicle.makeCodeNumber))>
<cfpdfformparam name="make" value="#session.checkout.vehicle.makeCodeNumber#">
<cfelse>
<cfpdfformparam name="make" value="#session.checkout.vehicle.vehiclemake#">
</cfif>
</cfif>
I'm trying to use autocomplete with a field with commas in it. When I type the comma it will ignore it, and won't return anything. So far I have this:
index.cfm
<!--- A simple form for auto suggest --->
<cfform action="autosuggest.cfm" method="post">
Artist:
<cfinput type="text" name="artist" size="50" autosuggest="cfc:autosuggest.findartist({cfautosuggestvalue})" autosuggestminlength="4" maxresultsdisplayed="5" /><br /><br />
</cfform>
autosuggest.cfc
<cfcomponent output="false">
<!--- Lookup used for auto suggest --->
<cffunction name="findartist" access="remote" returntype="string">
<cfargument name="search" type="any" required="false" default="">
<!--- Define variables --->
<cfset var local = {} />
<!--- Query Location Table --->
<cfquery name="local.query" datasource="#application.datasource#" >
select DISTINCT artist
from items
where artist like <cfqueryparam cfsqltype="cf_sql_varchar" value="#ucase(arguments.search)#%" />
order by artist
</cfquery>
<!--- And return it as a List --->
<cfreturn valueList(local.query.artist)>
</cffunction>
</cfcomponent>
When I try to search for example Brown,James it doesn't return anything. What do I need to put in it to return results with commas.
Thanks
One option is to have your function return an array, instead of a string. Then delimiters are not an issue.
<cffunction name="findartist" access="remote" returntype="array">
...
<cfreturn listToArray(valueList(local.query.artist, chr(30)), chr(30))>
</cffunction>
Update:
As Raymond pointed out, the only sure-fire way to avoid delimiter issues on the CF side is not to use them. Instead loop through the query to build the array, ie:
<cffunction name="findartist" access="remote" returntype="array">
...
<cfset local.arr = []>
<cfloop query="local.query">
<cfset arrayAppend(local.arr, local.query.artist)>
</cfloop>
<cfreturn local.arr>
</cffunction>
I have defined a datasource in the admin console of ColdFusion Admin. I have declared the configuration with the same datasource name in set_datasource_util file. I have a webpage that connects to a DB(Oracle) using the datasource given and it works fine most of the time. But lately I am starting to see exceptions like "Variable datasource-name(my app name) is undefined". I am not able to identify the root cause as I am not able to recreate it as it works most of the time. I can say on average on 1000 hits of page, it fails 1 time with that exception. Can anyone help in identifying what could be the possible issue so that I can investigate in that direction.
I have configured WOCD080P_ABC in CFAdmin.
Below is the code from Application.cfm
<cfif client.securityLevel NEQ ''>
<cfinvoke component="#siteroot#.CFC.Set_Data_Source_Util" method="fnSetABCDataSource" returnvariable="ABCDataSourceDefinition">
<cfinvokeargument name="Environment" value="#variables.thisServerType#" />
</cfinvoke>
<cfif ABCDataSourceDefinition.DataSource NEQ 'Error'>
<cfset DATASOURCE_ABC = ABCDataSourceDefinition.DataSource />
<cfset DBUSER_ABC = ABCDataSourceDefinition.DBUser />
<cfset DBPASSWORD_ABC = ABCDataSourceDefinition.DBPassword />
</cfif>
</cfif>
Below is the code from Set_datasource_UTil :
<cffunction name="fnSetABCDataSource" access="public" returntype="struct">
<cfargument name="Environment" type="string" required="yes">
<cfset ReturnParameters = StructNew() />
<cfif Environment IS 'prod'>
<cfset ReturnParameters.DataSource = 'WOCD080P_ABC' />
<cfset ReturnParameters.DBUser = ''/>
<cfset ReturnParameters.DBPassword = '' />
<cfelse>
<cfset ReturnParameters.DataSource = 'WOCD080T_ABC'/>
<cfset ReturnParameters.DBUser = ''/>
<cfset ReturnParameters.DBPassword = '' />
</cfif>
<cfreturn ReturnParameters>
</cffunction>
Below is the code which i am executing from HTML file.
<cfinvoke component="#siteroot#.cfc.ABC_UTIL" method="GetRegion" returnVariable = "USregion" >
<cfinvokeargument name="dbSource" value=#DATASOURCE_ABC# />
<cfinvokeargument name="dbUser" value=#DBUSER_ABC# />
<cfinvokeargument name="dbPass" value=#DBPASSWORD_ABC# />
</cfinvoke>
The code in the HTML file works most of the time but breaks sometime with error Variable DATASOURCE_ABC not defined.
Any help appreciated.
Please ensure you are using the var keyword for local variables inside of CFC Functions:
<cfset var ReturnParameters = StructNew() />
CF9+ local scope can be used in lieu of var:
<cfset local.ReturnParameters = StructNew() />
For extra protection, improve this statement in your application.cfm:
<cfif isDefined("ABCDataSourceDefinition.DataSource")
AND ABCDataSourceDefinition.DataSource NEQ 'Error'>
<cfset DATASOURCE_ABC = ABCDataSourceDefinition.DataSource />
<cfset DBUSER_ABC = ABCDataSourceDefinition.DBUser />
<cfset DBPASSWORD_ABC = ABCDataSourceDefinition.DBPassword />
</cfif>
Also, the could be another source of why a datasource is not being defined. I assume you have an interceptor that catches that before this datasource error occurs and sends them to a login page or something.
I want to display the value of a struct key like:
#stReviewDetail['tags']['travelParty']['value']#
It is possible that tags, travelParty or value is missing. What is the best way to check if the structure hierarchy is available? Something like:
<cfif StructKeyExists(stReviewDetail, 'tags') AND
StructKeyExists(stReviewDetail['tags'], 'travelParty') AND
StructKeyExists(stReviewDetail['tags']['travelParty'], 'value') >
....
</cfif>
or is there a better way to do this?
Multiple StructKeyExists are ugly, and it's easy to write a function to simplify this:
Usage:
<cfif CheckNestedKeys(stReviewDetail,['tags','travelParty','value']) >
#stReviewDetail['tags']['travelParty']['value']#
</cfif>
Code:
<cffunction name="CheckNestedKeys" returntype="Boolean" output=false>
<cfargument name="Struct" type="Struct" required />
<cfargument name="Keys" type="Array" required />
<cfset var CurStruct = Arguments.Struct />
<cfloop index="local.CurKey" array=#Arguments.Keys# >
<cfif StructKeyExists(CurStruct,CurKey)>
<cfset CurStruct = CurStruct[CurKey] />
<cfelse>
<cfreturn false />
</cfif>
</cfloop>
<cfreturn true />
</cffunction>
If you know the specific keys, you could just use isDefined:
<cfif isDefined("stReviewDetail.tags.travelParty.value")>
<cfdump var="#stReviewDetail.tags#">
</cfif>
I have a text field that I want to autosuggest values based on a query. I have a main file along with a separate file (getdata.cfc) that holds my query.
Here is the text field portion of my main file:
<cfinput name="search_query" autosuggest="url:getdata.cfc?suggestvalue={cfautosuggestvalue}" maxResultsDisplay="10" showAutoSuggestLoadingIcon="true" size="10" />
Here is the code in getdata.cfc:
<cfcomponent>
<cffunction name="get_data" access="remote" output="false">
<cfargument name="suggestvalue" required="true">
<cfquery name="get_data" datasource="#application.DSN#">
SELECT DISTINCT myItem
FROM myTable
WHERE myItem LIKE <cfqueryparam value="#suggestvalue#%"
cfsqltype="cf_sql_varchar">
ORDER BY myItem
</cfquery>
<cfif get_data.recordCount eq 1>
<cfreturn ",#get_data.myItem#">
<cfelse>
<cfreturn ValueList(get_data.myItem)>
</cfif>
</cffunction>
</cfcomponent>
The text field shows up fine, but when I type a word no autosuggest values show up. Nothing happens. The text is just displayed as I type it.
Any suggestions? Thank you!
I switched away to using jquery plugins from a lot of CF stuff, but here is an example I have that works in some old production code
<cfinput type="text" name="email" id="email" autosuggest="cfc:cfc.users.lookupEmail({cfautosuggestvalue})" maxresultsdisplayed = "25">
<cffunction name="lookupEmail" access="remote" returntype="array">
<cfargument name="search" type="any" required="false" default="">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery name="data" datasource="datasource" maxrows="25" cachedwithin="#CreateTimeSpan(0,0,30,0)#">
SELECT distinct email
FROM users
WHERE email LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.search#%">
ORDER BY email
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, email)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
maybe this helps
also make sure you have your cfform tags around your form, and make sure that your /cfide folder is mapped to your website.
looking at your code and comparing it... it may be the way your calling the cfc (filename)
try: autosuggest="cfc:getdata.get_data.({cfautosuggestvalue})"