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>
I am getting an error after upgrade from coldfusionOX to coldfusion 10.
Error Occurred While Processing Request Complex object types cannot be
converted to simple values.
The expression has requested a variable or an intermediate expression
result as a simple value. However, the result cannot be converted to a
simple value. Simple values are strings, numbers, boolean values, and
date/time values. Queries, arrays, and COM objects are examples of
complex values. The most likely cause of the error is that you tried
to use a complex value as a simple one. For example, you tried to use
a query variable in a cfif tag.
It occurs at line " cfloop index="local.thisRight" list="#rights#" ". It seems like ColdFusion does not like the "rights" here.
Anyone can give me some help? Thanks so much.
<cfif local.profile.rights.profile.self is not "">
<cfquery name="local.getAffiliations" datasource="#Request.readerDSN#">
SELECT tblPersonsToAffiliations.affiliationID, tblPersonsToAffiliations.rights, tblAffiliations.affiliationType, tblPersonsToAffiliations.relationshipType
FROM tblPersonsToAffiliations INNER JOIN tblAffiliations
ON tblPersonsToAffiliations.affiliationID = tblAffiliations.affiliationID
WHERE tblPersonsToAffiliations.personID IN (#local.profile.rights.profile.self#)
AND (tblPersonsToAffiliations.archived IS NULL
OR tblPersonsToAffiliations.archived = '')
</cfquery>
<cfif local.getAffiliations.recordCount is not 0>
<cfloop query="local.getAffiliations">
<cfif local.getAffiliations.relationshipType is "interested">
<cfset local.thisRelationshipType = "provisionalMember">
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRelationshipType#")>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = ListAppend(local.profile.rights[affiliationType][local.thisRelationshipType], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][thisRelationshipType] = affiliationID>
</cfif>
<cfelse>
<cfset local.thisRelationshipType = "fullMember">
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRelationshipType#")>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = ListAppend(local.profile.rights[affiliationType][local.thisRelationshipType], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = affiliationID>
</cfif>
<cfif isNull(rights)>
<cfelse>
<cfloop index="local.thisRight" list="#rights#" >
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRight#")>
<cfset local.profile.rights[affiliationType][local.thisRight] = ListAppend(local.profile.rights[affiliationType][local.thisRight], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][local.thisRight] = affiliationID>
</cfif>
</cfloop>
</cfif>
</cfif>
</cfloop>
</cfif>
</cfif>
A bit earlier in your code you do this:
<cfif local.getAffiliations.relationshipType is "interested">
I think you need the same query name prefix in front of "rights" that is used when evaluating "relationshipType".
Try this:
#local.getAffiliations.rights#
HTH!
I am betting it is failing on this line:
<cfloop index="local.thisRight" list="rights" >
You are attempting to use the string "rights" as a list. My first reaction would be that you need to make that:
<cfloop index="local.thisRight" list="#rights#" >
I have a variable named #cfData# which contains an array of structures. As it's clear from the image, for 1st structure array there are 6 keys and for 2nd, only two keys,viz date and open.
If I run a common loop, to go through each and every key, I will get an error at second array element. So the following only works when all the keys are present in the structure:
<cfset blockedtotal = 0 />
<cfset bouncetotal = 0 />
<cfset blocked = 0/>
<cfset datetotal = 0 />
<cfloop array = #cfData# index = "i">
<cfset blockedtotal += i.blocked />
<cfset bouncetotal += i.bounce />
</cfloop>
After reading online, I got an idea of using StructKeyExists where I think I can proceed in the following way:
<cfif structKeyExists(cfData,"bounce")>
<cfoutput>Bounce:#cfData.bounce#"/></cfoutput>
<cfelse>
<cfoutput> Bounce : [none]<br/></cfoutput>
</cfif>
But I am wondering, where exactly should I insert the above code inside the cfloop? Please advise if my approach is wrong.
Update:
Thanks guys. I got it running by using the following code based on the answers and it's running fine:
<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "date")>
<cfset counter++>
<cfoutput>#counter#</cfoutput> Date is: <cfoutput> #i.date#</cfoutput> <br/>
</cfif>
</cfloop>
you don't need a "common loop". You can loop through each struct with
<cfloop array="#cfData#" index="i">
<cfloop collection="#i#" item="key">
struct with key '#key#' has data: #i[key]#
</cfloop>
</cfloop>
Of, if you need to decide if the struct has certain key, do something:
<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "someKey")>
<cfset counter++>
</cfif>
</cfloop>
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>
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