ColdFusion CFTRY and CFCATCH confusion - coldfusion

I am trying to use a <cftry> and <cfcatch> block of code. However I am confused about something, I put a <cftry> block around my query and the result of that query is not empty, but the condition always goes into the <cfcatch> block.
Example :
<cftry>
<cfquery name="qcar" datasource="xxxx">
select * from allcar
</cfquery>
<cfcatch>
<script>
alert("ERROR");
</script>
</cfcacth>
</cftry>
What is wrong with this code that it always goes to the <cfcatch>?

You have a typo in your code, the code in your answer should not even pass the syntax check. Please correct both - the code in your question and the one on your server, and check whether this solves your problem. If this is not the case, please apply a <cfdump var="#cfcatch#" />, like #beloitdavisja told you and show as further error messages. The typo is in line 9, </cfcacth> should be </cfcatch>.
<cftry>
<cfquery name="qcar" datasource="xxxx">
select * from allcar
</cfquery>
<cfcatch type="any">
<!--- Your debug output <script>alert("ERROR");</script>--->
<!--- Debugging - The ColdFusion Way --->
<cfdump var="#cfcatch#" />
</cfcatch>
</cftry>

<cftry>
<cfquery name="qEmployee" datasource="cfdocexamples">
SELECT * FROM Employeess
</cfquery>
<cfcatch type="any">
<cfoutput>
Error occured....<br /><br />
Message: <b>#cfcatch.Message#</b><br />
Detail: <b>#cfcatch.Detail#</b><br />
Type: <b>#cfcatch.Type#</b><br />
</cfoutput>
</cfcatch>
</cftry>
Note: #cfcatch# give full details in json format
#cfcatch.Message# give you only error message in string format

Related

How to return cfdocument pdf file and server to the user in the browser?

I have function that will generate cfdocument pdf file. Then I would like to return the document and server to the browser. Here is example of the function that generates the pdf file.
<cftry>
<cfdocument format="PDF" filename="file.pdf" overwrite="Yes">
<table>
<tr>
<td>Test</td>
</tr>
</table>
</cfdocument>
<cfset local.fnResults = {file: //Here I'm not sure what I should return}>
<cfcatch type="any">
<cfset local.fnResults = {status : 400, message : "Error! Something went wrong."}>
</cfcatch>
</cftry>
<cfreturn fnResults>
Function above should genrate the file and return PDF in fnResults structure. Then on serve.cfm I have this logic:
<cfset local.Results = genertePDF()>
<cfif structKeyExists(local.Results, "FILEPATH")>
<cfheader name="content-disposition" value="attachment;filename=#local.Results["FILENAME"]#"/>
<!---Add the file content to the output stream:--->
<cfcontent file="#local.Results["FILEPATH"]#" type="application/octet-stream" reset="true"/>
<!---Exit immediately after adding the file content to avoid corrupting it:--->
<cfabort/>
<cfif>
You can ignore the structure of RESULTS since I haven't modified everything. My only problem here is to figure out how to return cfdocument content? If anyone knows how to get this to work please let me know. Thank you.
Change:
<cfheader name="content-disposition" value="attachment;filename=#local.Results["FILENAME"]#"/>
To:
<cfheader name="content-disposition" value="inline;filename=#local.Results["FILENAME"]#"/>
Reference

Using cftransaction does not rolling back the entry

I am using the cftransaction tag and when error is coming, it is not rolling back the entry, i am not sure what is wrong, I am not using the nested part of cftransaction yet, simple one is not working, i am not sure how nested will behave here, correct my code if it is wrong guys...
<cffunction access="public" name="newRegistration" returntype="any">
<cfargument name="structform" default="" required="no" type="struct">
<cfset var newregis = "">
<cfset var str = "">
<cfset var msg = "">
<cftry>
<cftransaction action="begin"/>
<cfquery name="newregis" result="lastGenerated">
--- Insert Goes Here ---
</cfquery>
<cfset rsLastID = lastGenerated.generated_key>
<cfif isDefined('arguments.structform.mailoption') AND arguments.structform.mailoption EQ 'Yes'>
<cfset msg = createAccountEmail(rsLastID,'nar')>
</cfif>
<cftransaction action="commit"/>
<cfset str = "Account Created Successfully. #msg#">
<cfcatch type="any">
<cftransaction action="rollback"/>
<cfset str = "Error! #cfcatch.Detail# #cfcatch.Message#. Entry rolled back, try again">
</cfcatch>
</cftry>
<cfreturn str>
</cffunction>
You appear to be missing the closing CFTRANSACTION tag which should be wrapped around the try/catch block.
<cftransaction action="begin">
<cftry>
...
<cfcatch type="any">
<cftransaction action="rollback" />
...
</cfcatch>
</cftry>
</cftransaction>
Use that structure and you should be good.

ColdFusion autosuggest in text field is not responsive

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})"

Attributes missing in <cfdocumentItem >

<cfparam name="attributes.mytestvalue" default="0">
<cfdump var="#attributes#">
<!--- Attributes are there. --->
<cfdocumentSection>
<cfdocumentitem type="footer">
<cfdump var="#attributes#">
</cfdocumentitem type="footer">
</cfdocumentSection>
<!--- Attributes are not there. --->
Why does this happen? Is this a known bug?
According to \WEB-INF\cftags\META-INF\taglib.cftld, cfdocumentsection and cfdocumentitem are implemented as custom tags themselves and therefore possibly have their own attributes scope, thereby masking the values you've put into attributes scope.
You could try this:
<cfdocumentSection>
<cfdocumentitem type="footer" mytestvalue="#attributes.myTestValue#>
<cfdump var="#attributes#">
</cfdocumentitem type="footer">
</cfdocumentSection>
Or perhaps this:
<cfset attribs={type="footer", myTestValue=0}>
<cfdocumentSection>
<cfdocumentitem attributeCollection="#attribs#">
<cfdump var="#attributes#">
</cfdocumentitem>
</cfdocumentSection>
Also, although I don't think it's the problem here, you've got the type="footer" attribute on the closing tag of cfdocumentItem, which isn't necessary
<cfset request.myTestValue=attributes.myTestValue>
<cfdocumentSection>
<cfdocumentitem type="footer" mytestvalue="#request.myTestValue#>
<cfdump var="#attributes#">
</cfdocumentitem type="footer">
</cfdocumentSection>
This fixed it However now I know why it was not working. Thanks Barnyr.

Unwanted Facebook button at first time use of my FB application in coldfusion

I am playing around with facebook application, making them in iframe using coldfusion.
Following is the url of my app http://apps.facebook.com/firstones/
Instead of going directly going to the permissions page, it goes to the page shown below in picture.
And after clicking on that big 'Facebook' button it goes to ask for applications permission. And once permissions are granted it takes to my website, where I have hosted the application, instead of opening it in Facebook only.
Following is the code of my canvas url [http://www.dkyadav.com/firstOnes/auth/]
index.cfm
<cfparam name="client_id" default="1234"> <!---same as app_id --->
<cfparam name="redirect_uri" default="http://www.dkyadav.com/firstOnes/auth/">
<cfparam name="redirect_uri_final" default="http://www.dkyadav.com/firstOnes/">
<cfparam name="scope" default="user_education_history,user_hometown,friends_education_history ">
<cfparam name="client_secret" default="56789"> <!---- App_secret key ---->
<cfif not isdefined('url.code')>
<Cflocation url="https://www.facebook.com/dialog/oauth?client_id=#client_id#&redirect_uri=#redirect_uri#&scope=#scope#" >
<Cfelse>
<Cfif isdefined('url.error')>
<Cfoutput>
#url.error#<br />
Access denied.
</Cfoutput>
<cfelse>
<cfoutput>#url.code#<br />
<cfhttp url="https://graph.facebook.com/oauth/access_token" result="token">
<cfhttpparam name="client_id" value="#client_id#" encoded="no" type="url">
<cfhttpparam name="redirect_uri" value="#redirect_uri#" encoded="no" type="url">
<cfhttpparam name="client_secret" value="#client_secret#" encoded="no" type="url">
<cfhttpparam name="code" value="#url.code#" encoded="no" type="url">
</cfhttp>
<cflocation url="#redirect_uri_final#?#token.filecontent#" addtoken="no">
</cfoutput>
</Cfif>
</cfif>
And in http://www.dkyadav.com/firstOnes/index.cfm I look for access_token and have rest of my application.
I does the above said things only for the first time it run. Once its permission get approved, it works normally as expected.
You can try out this app running yourself http://apps.facebook.com/firstones/
Please help and let me know what I am actually missing. Thanks!!
I think the problem you have is that your initial display page on Facebook is actually bursting out of the iframe.
You can see the Javascript doing that on http://www.dkyadav.com/firstOnes/index.cfm in the fbLogin() function:
top.location.href = "https://graph.facebook.com/oauth/authorize?...
top refers to the window at the top of your window hierarchy which in your case will mean the actual Facebook home page with the iframe in it.
Hope that helps!
I think I dont need access_token, I just need oauth_token that I get by Decoding the result I got from submitting my client_id to 'https://www.facebook.com/dialog/oauth....'
Below is the code that I am using:
<cfif isdefined('signed_request')>
<cfoutput>
<cfset b64 = signed_request>
<!--- Split the param by the . --->
<cfset raw_str = ListGetAt(b64, 2, ".")>
<cfset res = Len(raw_str) % 4>
<cfif res eq 2>
<cfset raw_str &= "==">
<cfelseif res eq 3>
<cfset raw_str &= "=">
</cfif>
<!--- Base 64 Decode --->
<cfset result = ToString(BinaryDecode(raw_str, "Base64"))>
<!--- String to JSON --->
<cfset json = DeserializeJSON(result)>
<cfif StructKeyExists(json,'oauth_token')>
<Cfset at = json.oauth_token>
<cfhttp url="https://graph.facebook.com/me/friends?access_token=#at#" result="newsFeed" />
<cfif IsJSON(newsFeed.filecontent)>
<cfset cfData=DeserializeJSON(newsFeed.filecontent)>
<cfdump var = #cfData#>
<cfelse>
filecontent not in json format
</cfif>
<cfelse>
<script>
top.location.href='https://www.facebook.com/dialog/oauth?client_id=12345XXX&redirect_uri=http://apps.facebook.com/test_app/&scope=read_stream,email'
</script>
</cfif>
</cfoutput>
</cfif>