Tips and Tricks
2
Usage of cfquery result attribute
Scenario :
Usually We would first check for the Record existence using SELECT
statement and then Update or Insert will follow.
3
<cftransaction>
<!--- Check for the existence of the product --->
<cfquery name="getMyTableDetails">
SELECT ColumnName1, ColumnName2, ColumnName3
FROM MyTable
WHERE ColumnName1 = <cfqueryparam value="#arguments.ParameterName#" cfsqltype="cf_sql_integer">
</cfquery>
<!--- If it already exits in 'MyTable' then update the table else insert it --->
<cfif getMyTableDetails.recordCount>
<cfquery name="updateMyTableDetails ">
UPDATE MyTable
SET ColumnName2 = 'New Value'
WHERE ColumnName1 = <cfqueryparam value="#arguments.ParameterName#"cfsqltype="cf_sql_integer">
</cfquery>
<cfelse>
<cfquery name="insertIntoMyTableDetails" >
INSERT INTO MyTable
VALUES (
<cfqueryparam value="#arguments.ParameterName1 #" cfsqltype="cf_sql_varchar">
<cfqueryparam value="#arguments.ParameterName2#" cfsqltype="cf_sql_integer">
)
</cfquery>
</cfif>
</cftransaction>
4
<cftransaction>
<!---if record there in DB then update it --->
<cfquery name="updateMyTableDetails" result="updateMyTableResultSet">
UPDATE
MyTable
SET
ColumnName2 = 'New Value'
WHERE
ColumnName1 =
<cfqueryparam value="#arguments.ParameterName#" cfsqltype="cf_sql_integer">
</cfquery>
<!---If record doesn't exists then insert it --->
<cfif updateMyTableResultSet.recordCount eq 0>
<cfquery name="insertIntoMyTable">
INSERT INTO
MyTable
VALUES (
<cfqueryparam value="#arguments.ParameterName1#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#arguments.ParameterName2#" cfsqltype="cf_sql_integer">
)
</cfquery>
</cfif>
</cftransaction>
5
Custom Tag for getting error information – Local
Environment
Problem : In Webservies if the CFC Method is wrapped with <cftry/><cfcatch/>, the developers cant see the error
details because it is handled in the <cfcatch> section.
In <cfcatch> by using <cfmail> developers can get the error details but for that we need to configure SMTP Servers and it
will take time to receive the mail etc.
In such cases, for quick debugging developers can use this custom tag for creating the error documents in specific
directory.
Things to Do:
Create a file with a name dump.cfm in ColdFusionCustomTags directory.
6
<cftry>
<cfset content = attributes.var />
<cfparam name="type" default="0">
<cfparam name="heading" default="DefaultHeading.">
<cfif isDefined('attributes.type')>
<cfset type = attributes.type />
</cfif>
<cfif isDefined('attributes.heading')>
<cfset heading = attributes.heading />
</cfif>
<cfdocument format="pdf" filename="d:pdf#heading#_#timeformat(now(),"hh-mm-ss")#.pdf" overwrite="true">
<cfif type EQ 1>
<cfoutput>#content#</cfoutput>
<cfelse>
<cfdump var="#content#">
</cfif>
</cfdocument>
<cfcatch>
<cfdocument format="pdf" filename="d:pdferrorCreatingDoc.pdf" overwrite="true">
<cfdump var="#cfcatch#">
</cfdocument>
</cfcatch>
</cftry>
7
•In above example we create a folder with name pdf in d drive (d:pdf) for saving the
error documents. Instead of “d:pdf” we can specify any path in dump.cfm and we will get
the generated file in specified path.
•Place the custom tag “ <cf_dump var="#cfcatch#"> “ in <cfcatch> section as below
Example:
<cftry>
...
<cfcatch>
<cf_dump var="#cfcatch#">
</cfcatch>
</cftry>
• If trying to dump a String / Integer value need to add 'type=1' attribute in the custom
tag as below.
Example:
<cf_dump var="#cfcatch#" type=1>
Conclusion: For debugging the Applications using ColdFusion webservices, we can
use above custom tag.
8
Thank You.....

Coldfusion Tips and Tricks

  • 1.
  • 2.
    2 Usage of cfqueryresult attribute Scenario : Usually We would first check for the Record existence using SELECT statement and then Update or Insert will follow.
  • 3.
    3 <cftransaction> <!--- Check forthe existence of the product ---> <cfquery name="getMyTableDetails"> SELECT ColumnName1, ColumnName2, ColumnName3 FROM MyTable WHERE ColumnName1 = <cfqueryparam value="#arguments.ParameterName#" cfsqltype="cf_sql_integer"> </cfquery> <!--- If it already exits in 'MyTable' then update the table else insert it ---> <cfif getMyTableDetails.recordCount> <cfquery name="updateMyTableDetails "> UPDATE MyTable SET ColumnName2 = 'New Value' WHERE ColumnName1 = <cfqueryparam value="#arguments.ParameterName#"cfsqltype="cf_sql_integer"> </cfquery> <cfelse> <cfquery name="insertIntoMyTableDetails" > INSERT INTO MyTable VALUES ( <cfqueryparam value="#arguments.ParameterName1 #" cfsqltype="cf_sql_varchar"> <cfqueryparam value="#arguments.ParameterName2#" cfsqltype="cf_sql_integer"> ) </cfquery> </cfif> </cftransaction>
  • 4.
    4 <cftransaction> <!---if record therein DB then update it ---> <cfquery name="updateMyTableDetails" result="updateMyTableResultSet"> UPDATE MyTable SET ColumnName2 = 'New Value' WHERE ColumnName1 = <cfqueryparam value="#arguments.ParameterName#" cfsqltype="cf_sql_integer"> </cfquery> <!---If record doesn't exists then insert it ---> <cfif updateMyTableResultSet.recordCount eq 0> <cfquery name="insertIntoMyTable"> INSERT INTO MyTable VALUES ( <cfqueryparam value="#arguments.ParameterName1#" cfsqltype="cf_sql_varchar">, <cfqueryparam value="#arguments.ParameterName2#" cfsqltype="cf_sql_integer"> ) </cfquery> </cfif> </cftransaction>
  • 5.
    5 Custom Tag forgetting error information – Local Environment Problem : In Webservies if the CFC Method is wrapped with <cftry/><cfcatch/>, the developers cant see the error details because it is handled in the <cfcatch> section. In <cfcatch> by using <cfmail> developers can get the error details but for that we need to configure SMTP Servers and it will take time to receive the mail etc. In such cases, for quick debugging developers can use this custom tag for creating the error documents in specific directory. Things to Do: Create a file with a name dump.cfm in ColdFusionCustomTags directory.
  • 6.
    6 <cftry> <cfset content =attributes.var /> <cfparam name="type" default="0"> <cfparam name="heading" default="DefaultHeading."> <cfif isDefined('attributes.type')> <cfset type = attributes.type /> </cfif> <cfif isDefined('attributes.heading')> <cfset heading = attributes.heading /> </cfif> <cfdocument format="pdf" filename="d:pdf#heading#_#timeformat(now(),"hh-mm-ss")#.pdf" overwrite="true"> <cfif type EQ 1> <cfoutput>#content#</cfoutput> <cfelse> <cfdump var="#content#"> </cfif> </cfdocument> <cfcatch> <cfdocument format="pdf" filename="d:pdferrorCreatingDoc.pdf" overwrite="true"> <cfdump var="#cfcatch#"> </cfdocument> </cfcatch> </cftry>
  • 7.
    7 •In above examplewe create a folder with name pdf in d drive (d:pdf) for saving the error documents. Instead of “d:pdf” we can specify any path in dump.cfm and we will get the generated file in specified path. •Place the custom tag “ <cf_dump var="#cfcatch#"> “ in <cfcatch> section as below Example: <cftry> ... <cfcatch> <cf_dump var="#cfcatch#"> </cfcatch> </cftry> • If trying to dump a String / Integer value need to add 'type=1' attribute in the custom tag as below. Example: <cf_dump var="#cfcatch#" type=1> Conclusion: For debugging the Applications using ColdFusion webservices, we can use above custom tag.
  • 8.