Cfqueryparam'd Query generation from function - coldfusion

There is a search query, and a function I am using to generate query elements
<cffunction name="GetSearchQuery" output="true" returntype="string" access="public">
<cfargument name="arrayName" type="array" required="yes">
<cfargument name="columnName" type="string" required="yes">
<cfargument name="searchtype" type="string" required="no" default="wildcard">
<cfset var o = "">
<cfset var i = "">
<cfset var search_item = "">
<cfset search_item = "(">
<cfloop from="1" to="#ArrayLen(Arguments.arrayName)#" index="o">
<cfif Arguments.arrayName[o][1] EQ #Arguments.columnName#>
<cfloop from="2" to="#ArrayLen(Arguments.arrayName[o])#" index="i">
<cfset search_item = search_item & #Arguments.columnName#>
<cfswitch expression="#Arguments.searchtype#">
<cfcase value="wildcard">
<cfset search_item = search_item & ' LIKE
<cfqueryparam value="%' & #Arguments.arrayName[o][i]# & '%"> AND '>
</cfcase>
<cfcase value="startswith">
<cfset search_item = search_item & ' LIKE
<cfqueryparam value="' & #Arguments.arrayName[o][i]# & '%"> AND '>
</cfcase>
<cfcase value="endswith">
<cfset search_item = search_item & ' LIKE
<cfqueryparam value="%' & #Arguments.arrayName[o][i]# & '"> AND '>
</cfcase>
<cfcase value="exactmatch">
<cfset search_item = search_item & ' =
<cfqueryparam value="' & #Arguments.arrayName[o][i]# & '"> AND '>
</cfcase>
</cfswitch>
</cfloop>
</cfif>
</cfloop>
<cfif Len(search_item) GT 4>
<cfset search_item = Left(search_item, Len(search_item)-4) & ") ">
</cfif>
<cfreturn search_item>
</cffunction>
And then call it like this in the query
SELECT * FROM #request.tablename#
WHERE #utilObj.GetSearchQuery(arrsearch, "photonumber", true)# OR
#utilObj.GetSearchQuery(arrsearch, "takenby", true)# OR
#utilObj.GetSearchQuery(arrsearch, "category", true)# OR
#utilObj.GetSearchQuery(arrsearch, "area", true)# OR
#utilObj.GetSearchQuery(arrsearch, "description", true)#
But it causes an error in the query
But without cfqueryparam in the function this woks fine.
eg. <cfset search_item = search_item & ' LIKE "%' & #Arguments.arrayName[o][i]# & '%" AND '>
Is there anyway we can add cfqueryparam dynamically to a query?

You can't create a string containing CFML and output it and expect that to somehow mean it'll be actually executed.
For one thing, that's a bit daft when you stop and think about it, innit? (sorry, I don't mean that in a mean-spirited way). And don't feel bad: I reckon we've all done this at some stage.
Secondly: CFML is compiled before it's executed. So the process is (for all intents and purposes):
File containing code is requested
Code from file is passed to the CF
compiler
CF compiler spits out java byte code
JVM executes java byte code
So your code to generate the string with the CFML code is not executed until (4), but it is needed back at (2). Unless you can time travel, that ain't gonna work.
I discuss this in my blog: "The ColdFusion request/response process"
There's a coupla things you can do:
don't write dynamic generic SQL like this. All of us do it when we first start, but quickly come to realise dynamic/generic SQL is never a good solution to whatever issue is at hand.
Use one of the already-existing DB abstraction tiers out there. CF 9+ comes with Hibernate support baked in.
If you use Query.cfc instead of <cfquery>, you can put placeholders in for the parameters, and pass the parameter data into the query separately.
Write your dynamic code to disk, then include it. This'll subvert the compile-time/run-time thing. It will be slow, as your included file will need to be compiled before it will run. It's ugly.
That list is in my order of preference for dealing with this issue.

This is in response to "Is there anyway we can add cfqueryparam dynamically to a query?". There is, but you have to do it inside the cfquery block. Something like this is ok:
<cfquery>
select SomeFields
from SomeTables
where 1 = 1
<cfif something>
and somefield = <cfqueryparam value="#SomeVariable#">
</cfif>
</cfquery>
What you can't do, at least not on version 9 and lower, is to make query parameters part of a variable. In other words, this will not compile.
WhereClause = "where 1=1";
if (something)
WhereClause &= ' and somefield = <cfqueryparam value="#SomeVariable#">';
This is more or less what you were attempting.

Related

Adding AND statement to ColdFusion query

I am trying to add AND ActiveFlag = 'Y' (value in same table as CommitteeRoleCode), but all my attempts either brake it or are ignored. Would anyone be kind enough to assist?
Thanks for your time!
<cfparam name="attributes.Address" default="">
<cfparam name="attributes.CommitteeID" default="0">
<cfparam name="attributes.AddressListName" default="">
<cfif Len(Trim(attributes.CommitteeID)) EQ 0>
<cfset attributes.CommitteeID = 0>
</cfif>
<cfif Len(Trim(attributes.Address))>
<cfset result = "">
<cfloop list="#attributes.Address#" index="i">
<cfif Refind("\[{1}.+\]{1}", i)>
<!---looking for the 'ALL' member type to grab all categories--->
<cfif i EQ "[ALL]">
<cfset l_where = "a.CommitteeRoleCode IS NOT NULL AND c.EmailAddress IS NOT NULL">
<cfelse>
<cfset l_where = "a.CommitteeRoleCode=#Chr(39)##ReReplace(i, "\[|\]", "", "ALL")##Chr(39)# AND c.EmailAddress IS NOT NULL">
</cfif>
<cfquery datasource="#application.datasource#" name="MemberTypeEmails">
Select c.EmailAddress
from (Committee_Role_Ref as a Inner Join Committee_Member as b on a.CommitteeRoleCode=b.CommitteeRoleCode)
Inner Join Contact as c on b.ContactID=c.ContactID
Where #ReReplace(l_where, "\'\'", "'", "ALL")#
AND b.CommitteeID=#attributes.CommitteeID#
</cfquery>
<cfloop query="MemberTypeEmails">
<cfif ReFind("[^#chr(13)##chr(10)##chr(9)##chr(32)#]#{1}.+\..+", EmailAddress)>
<cfif Len(Trim(result))>
<cfset result = result & "," & EmailAddress>
<cfelse>
<cfset result = EmailAddress>
</cfif>
</cfif>
</cfloop>
<cfelseif ReFind(".+#{1}.+\..+", i)>
<cfif Len(Trim(result))>
<cfset result = result & "," & i>
<cfelse>
<cfset result = i>
</cfif>
</cfif>
</cfloop>
<cfset "caller.#attributes.AddressListName#" = result>
</cfif>
You say you are trying to add AND ActiveFlag = 'Y' (value in same table as CommitteeRoleCode). However, the value CommitteeRoleCode appears in two tables and I know this because of this part of the query
a.CommitteeRoleCode = b.CommitteeRoleCode
My suggestion would be to find out which table the ActiveFlag is really in and then prefix it accordingly in the query with either the a. or b. ie
AND a.ActiveFlag = 'Y'
or
AND b.ActiveFlag = 'Y'
Then place your AND clause where indicated below. But if you want more help you should also post what error message you are receiving.
<cfquery datasource="#application.datasource#" name="MemberTypeEmails">
Select c.EmailAddress
from (Committee_Role_Ref as a Inner Join Committee_Member as b on a.CommitteeRoleCode=b.CommitteeRoleCode)
Inner Join Contact as c on b.ContactID=c.ContactID
Where #ReReplace(l_where, "\'\'", "'", "ALL")#
AND b.CommitteeID=#attributes.CommitteeID#
-- AND a. or b. ActiveFlag goes here
AND a.ActiveFlag = 'Y'
</cfquery>

Split and Sort an Array by date, getting blank array places

I am just confusing myself with this one. I have a data log that is saved on the database as something like this:
Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge).
<!--- all other tracking info --->
I am trying to pull from this log only the discharges and sort/filter them by date. This log is saved in the database as one column varchar(MAX). I need to sort this data and only pull the discharge dates / reason using regex and then apply a filter. The end product should end up with me putting that array in a table with PATIENT | DISCHARGE DATE | DISCHARGE REASON My code right now is giving me some errors as it is leaving me with some empty array values. My question is that I need to remove those blank values and sort by the date, but I am unsure where to begin.
<cfquery name="getDischarge" datasource="#this.dsn#">
select PatientsName, LogData from Patient
</cfquery>
<cfoutput query="getDischarge" group="PatientsName">
<cfif LogData neq "">
<cfsavecontent variable="str">
#LogData#
</cfsavecontent>
<cfset possibilities = reMatch("Discharge Date?:(\d\d?/\d\d?/\d{4})\s*Reason for Discharge:\d* - ((?:(?!Log-Date:).)*)", str)>
<cfset dateArray = ArrayNew(2)>
<cfloop index="w"
array="#possibilities#">
<cfif w NEQ "">
<!--- create 1 dimensional temp array to hold DATE | REASON --->
<cfset tempArray = ArrayNew(1)>
<!--- trim our regex to have only the date & then DateFormat --->
<cfset theDate = #Mid(w, 16, 11)#>
<cfset formatDate = #dateformat('#thedate#','mm-dd-yyyy')#>
<!--- use our regex to find the reason for discharge --->
<cfset theReason = reMatch("Reason for Discharge:\d* - ((?:(?!Log-Date:).)*)", str)>
<!--- append our DATE | REASON to 1d temp array --->
<cfset ArrayAppend(tempArray, '#formatDate#')>
<cfset ArrayAppend(tempArray, '#theReason#')>
<!--- append our 1d array to our 2d array to output matching DATE | REASON --->
<cfset #ArrayAppend(dateArray, '#tempArray#')#>
</cfif>
</cfloop>
<cfdump var="#dateArray#">
</cfif> <!--- logdata neq "" --->
</cfoutput>
To me, logically, this should work and omit blank values, but this is what I get when I dump this data:
Looks like you are struggling with the Jakarta ORO regular expression engine that is used in ColdFusion. reMatch/reMatchNoCase are terrible when it comes to capturing. Java on the other hand offers the POSIX regular expression engine.
<cfset data = [
"Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge).
More stuff...",
"Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/16/2012
Reason for Discharge:lorem ipsum."
]>
<cfset result = queryNew("DISCHARGE_DATE,DISCHARGE_REASON", "VARCHAR,VARCHAR")>
<cfloop array="#data#" index="line">
<cfset dischargeDate = reMatchGroupNoCase("Discharge Date:([0-9]{2}/[0-9]{2}/[0-9]{4})", line)>
<cfset dischargeReason = reMatchGroupNoCase("Reason for Discharge:([^\n]*)", line)>
<cfset hasDate = (arrayLen(dischargeDate) eq 1)>
<cfset hasReason = (arrayLen(dischargeReason) eq 1)>
<cfif hasDate or hasReason>
<cfset rowIndex = queryAddRow(result, 1)>
<cfif hasDate and isDate(dischargeDate[1])>
<cfset querySetCell(result, "DISCHARGE_DATE", dateFormat(dischargeDate[1], "yyyy-mm-dd"), rowIndex)>
</cfif>
<cfif hasReason>
<cfset querySetCell(result, "DISCHARGE_REASON", dischargeReason[1], rowIndex)>
</cfif>
</cfif>
</cfloop>
<cfquery name="orderedResult" dbType="query">
SELECT
*
FROM
[result]
ORDER BY
[DISCHARGE_DATE] ASC
</cfquery>
<cfdump var="#orderedResult#">
And here is the function you need:
<cffunction name="reMatchGroupNoCase" access="public" output="false" returnType="array">
<cfargument name="regex" type="string" required="true">
<cfargument name="value" type="string" required="true">
<cfset LOCAL.result = []>
<cfset LOCAL.Pattern = createObject("java", "java.util.regex.Pattern")>
<cfset ARGUMENTS.regex = LOCAL.Pattern.compile(ARGUMENTS.regex, bitOr(LOCAL.Pattern["CASE_INSENSITIVE"], LOCAL.Pattern["UNICODE_CASE"]))>
<cfset LOCAL.buffer = ARGUMENTS.regex.matcher(toString(ARGUMENTS.value))>
<cfset LOCAL.length = LOCAL.buffer.groupCount()>
<cfloop condition="LOCAL.buffer.find()">
<cfloop from="1" to="#LOCAL.length#" index="LOCAL.i">
<cfset LOCAL.value = LOCAL.buffer.group(
javaCast("int", LOCAL.i)
)>
<cfif isNull(LOCAL.value)>
<cfcontinue>
</cfif>
<cfset LOCAL.result.add(LOCAL.value)>
</cfloop>
</cfloop>
<cfreturn LOCAL.result>
</cffunction>
I recommend this approach:
with q1 as (select 'Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge). ' logData
)
select substring(logData, patindex('%Admission Date: %', logdata) + 16
, 10) admitDate
from q1
where logData like '%Discharge Date:%'
That returns 08/01/2012. You may have some complications for a variety of reasons, but the general idea should work.

I need to output FORM variables to a report, but only if there is a value

I have a form with many values in it (around 25 fields). After the form is posted and inserted into the database, I have to take the form information and output it to a report. The report should only show fields with those that have values in it (so the report would only have 5 fields in it, if only 5 fields were filled in).
The easiest way would be to do something like this:
<cfif form.firstname neq "">
<li><First Name: #FORM.FIRSTNAME#</li>
</cfif>
<cfif form.lastname neq "">
<li><Last Name: #FORM.LASTNAME#</li>
</cfif>
Can anyone recommend a better way of doing this? I would like to keep it on the ColdFusion side, since the entire report is stripped of HTML to produce a plain text report as well.
You can loop through them like this
<cfloop list="#form.fieldNames#" index="i">
<li><cfoutput>#i# = #form[i]#</cfoutput></li>
</cfloop>
Not sure that is exactly what you want but it might get you on the right track
Based on your comment try this :
<cfloop list="#form.fieldNames#" index="i">
<li><cfoutput>
<cfswitch expression="#i#">
<cfcase value="firstName">
First Name
</cfcase>
<cfcase value="lastName">
Last Name
</cfcase>
<cfdefaultcase>
#i#
</cfdefaultcase>
</cfswitch>
: #form[i]#</cfoutput></li>
</cfloop>
Here's my attempt at this: Thanks to Lance for pointing me towards the right direction:
<ul>
<cfoutput>
<cfloop list="#form.fieldNames#" index="i">
<li>
<cfif len(trim(form[i])) neq 0>
<cfswitch expression="#i#">
<cfcase value="FIRST_NAME">First Name</cfcase>
<cfcase value="LAST_NAME">Last Name</cfcase>
<cfdefaultcase>#i#</cfdefaultcase>
</cfswitch>
: #FORM[i]#
</cfif>
</li>
</cfloop>
</cfoutput>
</ul>
Here's an alternative to using switch/case - put labels in a struct
<cfset FieldLabels =
{ 'first_name' : "First Name"
, 'last_name' : "Last Name"
, 'other stuff' : "Whatever you like"
}/>
<cfoutput>
<ul>
<cfloop index="CurField" list=#Form.FieldNames# >
<cfif len(trim( Form[CurField] )) >
<li>
#StructKeyExists( FieldLabels , lcase(CurField) )
? FieldLabels[ lcase(CurField) ]
: HtmlEditFormat( replace(CurField,'_',' ','all') )
#
: #HtmlEditFormat( Form[CurField] )#
</li>
</cfif>
</cfloop>
</ul>
</cfoutput>
Note that it does not do neq 0 on the len - this is entirely unnecessary.
The a ? b : c construct inside the first hashes is the ternary conditional operator - a compact way of doing if/else - supported in CF10 and Railo 3.3 onwards.
If a name doesn't have an explicit label, it replaces underscores with spaces, which is probably preferable if the report is for a non-technical audience.
If it's possible that you have code that adds (or removes) items in the form scope without modifying the FieldNames list, you can change the loop for this...
<cfloop item="CurField" collection=#Form# >
<cfif CurField EQ 'FieldNames'>
<cfcontinue />
</cfif>
...
Which looks at the actual keys in the Form scope - though the ordering of them is not guaranteed for this method.
How about you loop on the struct itself directly ?
<cfscript>
for(eachKey in FORM){
if(FORM[eachKey] neq ''){
writeOutput('<li>' &
FORM[eachKey] & ' = ' &
trim( FORM[eachKey] ) &
'</li>');
}
}
</cfscript>
You can have a space in the form variable name.

Coldfusion - How to loop over all contents in a list

Making the array:
<cfset tempArr = DeserializeJSON(URL.data) />
<cfset temp1 = "" />
<cfset temp2 = "" />
<cfset selectList1 = "" />
<cfset selectList2 = "" />
<cfloop array=#tempArr# index="i">
<cfset temp1 = GetToken(i,1,":")>
<cfset temp2 = GetToken(i,2,":")>
<cfset selectList1 = listAppend(selectList1, temp1)>
<cfset selectList2 = listAppend(selectList2, temp2)>
</cfloop>
Looping through it??:
<cfquery name="sample" datasource="database">
SELECT *
FROM table
WHERE
<cfloop from="1" to="#listLen(selectList1)#" index="i"/>
#ListGetAt(selectList1, i)# = <cfqueryparam value="#ListGetAt(selectList2)#" />
</cfloop>
<cfif i neq listLen(#selectList1#)>
AND
</cfif>
</cfquery>
My intention is to search dynamically in a table based on the array that was received from the javascript page. The data comes in this form -> columnName:searchBy. ie, a sample piece would be name:Jim. I would like to build in dynamic code that would allow me to search by different columns but I can't get my loop to work. I get this error if it helps:
(Invalid CFML construct found on line 20 at column 59.)
which is this line:
<cfloop from="1" to="#listLen(selectList1)#" index="i" />
Kyle's answer is 100% correct, but here's a better solution. Using lists is a very inefficient process (see below) and using listGetAt will only anger future programmers. You can use an array to house the data and use a default WHERE statement to simplify your looping.
<cfset tempArr = DeserializeJSON(URL.data) />
<cfset temp1 = "" />
<cfset temp2 = "" />
<cfset selectList1 = [] />
<cfset selectList2 = [] />
<cfloop array=#tempArr# index="i">
<cfset temp1 = GetToken(i,1,":")>
<cfset temp2 = GetToken(i,2,":")>
<cfset arrayAppend(selectList1, temp1)>
<cfset arrayAppend(selectList2, temp2)>
</cfloop>
<cfif NOT arrayIsEmpty(tempArr)>
<cfquery name="sample" datasource="database">
SELECT column1, column2, column3
FROM table
WHERE 1 = 1
<cfloop from="1" to="#listLen(selectList1)#" index="i"/>
AND #selectList1[i]# = <cfqueryparam value="#selectList2[i]#" />
</cfloop>
</cfquery>
</cfif>
When you append to a list a new string is created in memory that combines the two previous string and the previous string is deleted. This is definitely premature optimization, but it's still a good practice to avoid using lists especially when you need to access elements in them.
I think your problem might be that the cfloop tag is self closing. Try this instead:
<cfloop from="1" to="#listLen(selectList1)#" index="i">
list attribute can be used in <cfloop>.
<cfloop list="#selectList1#" index="i">
#i# <!--- list element can be processed here with variable name #i# --->
</cfloop>

Issues with CFZip functionality

I have the following which is working half way. I am trying to CFZip the contents of a downloaded Excel file but there is one problem involved. The code below shows that there are 3 tickets. 1 main ticket and 2 tickets that are the extended answers of the main ticket. Attachments are allowed on all tickets, the main ones and the children's too.
When I try to download the Excel file, I am also trying to download two folders that should be inside the Zip file. One that should be pointing to the child and one to the parent. Both should have their respective attachments. I am missing some big piece of code here.
Here is the code:
<cfquery datasource="#request.dsn#" name="mainTickets">
SELECT s.ticketID,CAST(s.DateRaised as
varchar) AS DateRaised,s.Summary,s.RaisedBy,s.AssignedTo,
st.Status
,CAST(s.LastUpdatedDate as varchar),CAST(s.TimeSpent as float) as
timespent,stt.TicketType,s.LastUpdatedComment
from supportTickets s
inner join SupportStatusType st on st.statusID = s.status
inner join supportTicketType stt on stt.TicketTypeID = s.TicketType
where s.ticketID IN (<cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ticketID#" list="yes">
)
</cfquery>
<cfset filename = "Detailed_Ticket_Summary" & dateformat(now(), 'mm_dd_yyyy') & "." & "xls">
<cfset s = spreadsheetNew("Tickets Summary")>
<!--- Add header row --->
<cfset spreadsheetAddRow(s, "TicketID,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment, Details")>
<!--- format header --->
<cfset spreadsheetFormatRow(s, #format1#, 1)>
<cfset spreadsheetAddRows(s, mainTickets)>
<cfset SpreadsheetFormatColumn(s, {textwrap=true}, 10)>
<cfset SpreadsheetFormatColumn(s, {textwrap=true}, 3)>
<cfloop from="1" to="#mainTickets.recordcount#" index="i">
<cfset k = i + 1>
<cfset SpreadsheetSetCellFormula(s, 'HYPERLINK("[Tickets Summary]TicketID_#mainTickets.ticketID#!A1","Click Here")',#k#,11)>
<cfset SpreadsheetFormatCell(s, {bold=true, color='blue', bottomborder='dotted'}, #k#, 11)>
</cfloop>
<cfif mainTickets.recordcount>
<cfoutput query="mainTickets">
<cfquery datasource="#request.dsn#" name="fetchTickets">
SELECT s.ticketID,s.ticketNumber,CAST(s.DateRaised as
varchar),s.Summary,s.RaisedBy,s.AssignedTo,
st.Status
,CAST(s.LastUpdatedDate as varchar),s.TimeSpent,stt.TicketType,s.LastUpdatedComment
from supportTicketsHistory s
inner join SupportStatusType st on st.statusID = s.status
inner join supportTicketType stt on stt.TicketTypeID = s.TicketType
where s.ticketnumber = <cfqueryparam cfsqltype="cf_sql_numeric" value="#mainTickets.ticketID#">
</cfquery>
<!--- Add query --->
<cfif fetchTickets.recordcount>
<cfset SpreadsheetCreateSheet(s, 'TicketID_#ticketID#')>
<cfset SpreadsheetSetActiveSheet(s, 'TicketID_#ticketID#')>
<!--- Add header row --->
<cfset spreadsheetAddRow(s, "TicketID,Ticket Number,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment")>
<!--- format header --->
<cfset spreadsheetFormatRow(s, #format1#, 1)>
<cfset SpreadsheetAddRows(s, fetchTickets)>
<cfset SpreadsheetFormatColumn(s, {textwrap=true}, 11)>
<cfset SpreadsheetSetActiveSheetNumber(s, 1)>
<cfset ielements = ValueList(mainTickets.ticketID)>
<cfquery datasource="#request.dsn#" name="getAttachments">
SELECT Attach_FileName,Attach_FileSize,Attach_Time,SupportTicketID
FROM support_attachments
WHERE SupportTicketID = #val(fetchTickets.ticketnumber)#
</cfquery>
<cfquery datasource="#request.dsn#" name="getChkAttachments">
SELECT Support_Attach_FileName,Support_Attach_FileSize,Support_Attach_Time,
Support_SupportTktHistoryID,Support_SupportTicketID
FROM support_attach_History
WHERE Support_SupportTicketID = #val(fetchTickets.ticketnumber)#AND Support_SupportTktHistoryID
in (SELECT ss.ticketID FROM supportTicketsHistory ss
WHERE ss.ticketNumber = #val(fetchTickets.ticketnumber)#)
</cfquery>
<!--- handle the attachments for the Ticket in ZIp Format --->
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & getAttachments.SupportTicketID>
<cfif !DirectoryExists(currentDirectory)>
<cfdirectory action="create" directory="#currentDirectory#">
</cfif>
<cfset currentDirectory2 = GetDirectoryFromPath(GetTemplatePath()) & getChkAttachments.Support_SupportTktHistoryID>
<cfif !DirectoryExists(currentDirectory2)>
<cfdirectory action="create" directory="#currentDirectory2#">
</cfif>
<cfif getAttachments.recordcount>
<cfloop query="getAttachments">
<cffile action="copy"
source="#ExpandPath('attachments/#getAttachments.Attach_FileName#')#"
destination="#currentDirectory#\">
</cfloop>
</cfif>
<cfif getChkAttachments.recordcount>
<cfloop query="getChkAttachments">
<cffile action="copy"
source="#ExpandPath('attachments/#getChkAttachments.Support_Attach_FileName#')#"
destination="#currentDirectory2#\">
</cfloop>
</cfif>
<!--- handle the attachments for the Ticket in ZIP Format --->
<cfset spreadsheetWrite(s, filename, true)>
<cfset dest2 = getTempDirectory() & "/" & "Tickets" & ".zip">
<cfzip action="zip" file="#dest2#">
<cfdirectory action="list" directory="#currentDirectory#/" name="listRoot">
<cfdirectory action="list" directory="#currentDirectory2#/" name="listRoot2">
<cfset lstFiles = ValueList(listRoot.name)>
<cfset lstFiles2 = ValueList(listRoot2.name)>
<cfloop list="#lstFiles#" index="k">
<cfzipparam source="#currentDirectory#/#k#" recurse="yes" filter="*"/>
</cfloop>
<cfloop list="#lstFiles2#" index="kk">
<cfzipparam source="#currentDirectory2#/#kk#" recurse="yes" filter="*"/>
</cfloop>
</cfzip>
</cfif>
</cfoutput>
</cfif>
<cfheader name="Content-Disposition" value="attachment;filename=#ListFirst(filename,'.')#.zip">
<cfcontent file="#dest2#" type="application/zip" deletefile="true" reset="true">
I sorted it out here is the way I have done it.
<cfquery datasource="#request.dsn#" name="mainTickets">
SELECT s.ticketID,CAST(s.DateRaised as
varchar) AS DateRaised,s.Summary,s.RaisedBy,s.AssignedTo,
st.Status
,CAST(s.LastUpdatedDate as varchar),CAST(s.TimeSpent as float) as timespent,stt.TicketType,s.LastUpdatedComment
from supportTickets s
inner join SupportStatusType st on st.statusID = s.status
inner join supportTicketType stt on stt.TicketTypeID = s.TicketType
where s.ticketID IN (<cfqueryparam cfsqltype="cf_sql_numeric" value="#url.ticketID#" list="yes">)
</cfquery>
<cfset filename = "Detailed_Ticket_Summary" & dateformat(now(),'mm_dd_yyyy') & "." & "xls">
<cfset s = spreadsheetNew("Tickets Summary")>
<!--- Add header row --->
<cfset spreadsheetAddRow(s, "TicketID,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment, Details")>
<!--- format header --->
<cfset spreadsheetFormatRow(s,#format1#,1)>
<cfset spreadsheetAddRows(s, mainTickets)>
<cfset SpreadsheetFormatColumn(s,{textwrap=true},10)>
<cfset SpreadsheetFormatColumn(s,{textwrap=true},3)>
<cfloop from="1" to="#mainTickets.recordcount#" index="i">
<cfset k = i+1>
<cfset SpreadsheetSetCellFormula(s, 'HYPERLINK("[Tickets Summary]TicketID_#mainTickets.ticketID#!A1","Click Here")',#k#,11)>
<cfset SpreadsheetFormatCell(s,{bold=true,color='blue',bottomborder='dotted'},#k#,11)>
</cfloop>
<cfif mainTickets.recordcount>
<cfoutput query="mainTickets">
<cfquery datasource="#request.dsn#" name="fetchTickets">
SELECT s.ticketID,s.ticketNumber,CAST(s.DateRaised as
varchar),s.Summary,s.RaisedBy,s.AssignedTo,
st.Status
,CAST(s.LastUpdatedDate as varchar),s.TimeSpent,stt.TicketType,s.LastUpdatedComment
from supportTicketsHistory s
inner join SupportStatusType st on st.statusID = s.status
inner join supportTicketType stt on stt.TicketTypeID = s.TicketType
where s.ticketnumber = <cfqueryparam cfsqltype="cf_sql_numeric" value="#mainTickets.ticketID#">
</cfquery>
<!--- Add query --->
<cfset Temp = GetDirectoryFromPath(GetTemplatePath()) & 'temp'>
<cfif !DirectoryExists(Temp)>
<cfdirectory action="create" directory="#Temp#">
</cfif>
<cfif fetchTickets.recordcount>
<cfset SpreadsheetCreateSheet(s , 'TicketID_#ticketID#')>
<cfset SpreadsheetSetActiveSheet(s , 'TicketID_#ticketID#')>
<!--- Add header row --->
<cfset spreadsheetAddRow(s, "TicketID,Ticket Number,Date Raised,Summary,Raised By,AssignedTo,Status,Last Updated Date, Time Spent, Ticket Type, Last Updated Comment")>
<!--- format header --->
<cfset spreadsheetFormatRow(s,#format1#,1)>
<cfset SpreadsheetAddRows(s , fetchTickets)>
<cfset SpreadsheetFormatColumn(s,{textwrap=true},11)>
<cfset SpreadsheetSetActiveSheetNumber(s, 1)>
<cfset ielements = ValueList(mainTickets.ticketID)>
<cfquery datasource="#request.dsn#" name="getAttachments">
SELECT Attach_FileName,Attach_FileSize,Attach_Time,SupportTicketID
FROM support_attachments
WHERE SupportTicketID = #val(fetchTickets.ticketnumber)#
</cfquery>
<cfquery datasource="#request.dsn#" name="getChkAttachments">
SELECT Support_Attach_FileName,Support_Attach_FileSize,Support_Attach_Time,
Support_SupportTktHistoryID,Support_SupportTicketID
FROM support_attach_History
WHERE Support_SupportTicketID = #val(fetchTickets.ticketnumber)#
AND Support_SupportTktHistoryID in (SELECT ss.ticketID FROM supportTicketsHistory ss
WHERE ss.ticketNumber = #val(fetchTickets.ticketnumber)#)
</cfquery>
<cfif getAttachments.recordcount>
<!--- handle the attachments for the Ticket in ZIp Format --->
<cfset currentDirectory = "MainTicketID_" & "Number_" & getAttachments.SupportTicketID & "_Attachments">
<cfdump var="#temp#\#currentDirectory#">
<cfif !DirectoryExists("#temp#\#currentDirectory#")>
<cfdirectory action="create" directory="#temp#\#currentDirectory#">
</cfif>
<cfdump var="#currentDirectory#">
<cfif getAttachments.recordcount>
<cfloop query="getAttachments">
<cffile action="copy" source="#ExpandPath('attachments/#getAttachments.Attach_FileName#')#"
destination="#temp#\#currentDirectory#\">
</cfloop>
</cfif>
</cfif>
<cfif getChkAttachments.recordcount>
<cfset currentDirectory2 = "MainTicket_" & getChkAttachments.Support_SupportTicketID & "_Updated_Child_" & getChkAttachments.Support_SupportTktHistoryID & "_Attachments">
<cfif !DirectoryExists("#temp#\#currentDirectory2#")>
<cfdirectory action="create" directory="#temp#\#currentDirectory2#">
</cfif>
<cfif getChkAttachments.recordcount>
<cfloop query="getChkAttachments">
<cffile action="copy" source="#ExpandPath('attachments/#getChkAttachments.Support_Attach_FileName#')#"
destination="#temp#\#currentDirectory2#\">
</cfloop>
</cfif>
</cfif>
<!--- handle the attachments for the Ticket in ZIP Format --->
<cfset spreadsheetWrite(s, filename, true)>
<cfset dest2 = getTempDirectory() & "/" & "Tickets" & ".zip">
<cfzip action="zip" file="#dest2#" overwrite="true">
<cfzipparam source="#temp#"/>
<cfzipparam source="#filename#"/>
</cfzip>
</cfif>
</cfoutput>
</cfif>
<cfdirectory action="delete" directory="#temp#" recurse="yes">
<cfheader name="Content-Disposition" value="attachment;filename=#ListFirst(filename,'.')#.zip">
<cfcontent file="#dest2#" type="application/zip" deletefile="true" reset="true">
This can help someone instead how to nest the cfzip functionality