coldfusion if - how to look for several items in an IF - coldfusion

I have an if structure as follows:
<cfif #user.personnel_no# is 'xxxxx' or #user.personnel_no# is 'xxxxx' or #user.personnel_no# is 'xxxxx'>
---data
</cfif>
how can I do something like:
<cfif #user.personnel_no# in ('xxxxx','yyyyy','zzzzz')>
---data
</cfif>
to look among all values in if?
or declaring a list and do something like
list = 'xxxxx','yyyyy','zzzzz'
<cfif #user.personnel_no# in list>
---data
</cfif>
Thank you.

<cfscript>
user.personnel_no = 'yyyyy'
asArray = ['xxxxx','yyyyy','zzzzz']
writeOutput(asArray.find(user.personnel_no)) // 2
asList = 'xxxxx,yyyyy,zzzzz'
writeOutput(asList.listfind(user.personnel_no)) // 2
</cfscript>
https://trycf.com/gist/f737ef6d010d4ce37936f1d53d021a62/lucee5?theme=monokai
https://cfdocs.org/listfind
https://cfdocs.org/arrayfind

Related

making the listcolumns search dynamic

I am following upn on a tute on datatables and found a code which basically iterates over the columns for a order clause but in the case, i do not have a list of order columns hardcoded, its a dynamic list
how can I change this
<cfif form["order[0][column]"] gt 0>
ORDER BY
<cfif form["order[0][column]"] eq '1'>
empname <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>
<cfif form["order[0][column]"] eq '2'>
empno <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>
<cfif form["order[0][column]"] eq '3'>
ic <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>
</cfif>
to be dynamic as my list variable is a comma separated as id,name,email and lots more
The direction I always would go will be to create a Coldfusion array or structure with this fieldNames, like the following.
<cfset orderColumnReference = ['empname', 'empno', 'ic', 'id', 'name', 'email', ...]>
<cfif form["order[0][column]"] GTE ArrayLen(orderColumnReference)>
<cfset orderByColumn = orderColumnReference[form["order[0][column]"]]>
ORDER BY
#orderByColumn# <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>

Parse the contents of a Coldfusion variable into separate variables

OK, so I've been banging my head against this one for a while and getting nowhere. I've been attempting to take the contents of a variable and parse the contained string into parts that would then be ingested into 5 separate variables. Seems simple enough right? Well, it has not proven to be simple at all, at least for me.
So I have a variable (PageContent) that contains the trimmed content from a CFHTTP request.
The PageContent variable now contains:
<tdnowrapalign=right>07/18/2020 13:00</td>
<tdalign=right>1002.12</td>
<tdalign=right>2,874,887</td>
<tdalign=right>12,766</td>
<tdalign=right>13,038</td>
It seems like there should be an easy way to write a loop that would loop over the tags in the "PageContent" variable assigning the content of each tag to a different variable. But every way I try to parse the data in the variable I either get an error (Complex object types cannot be converted to simple values.) or I end up with the content that I originally had in the "PageContent" variable repeated within the loop.
For instance, if I had a loop that would run through 5 iterations and could grab the contents of the tags assigning each to a variable then the desired result would be:
DateTime = "07/18/2020 13:00"
Elevation = "1002.12"
Storage = "2,874,887"
Outflow = "12,766"
Inflow = "13,038"
After trying every example I could find here and elsewhere online I'm now on something like my 100th attempt. Now I'm trying to use regular expressions to grab the contents of the tags and assign them to variables but no luck there. What I ended up with was the entire contents of the PageContent variable being stuffed into each one of the variables. The result was not really unexpected since I don't know of a way to differentiate between the 3 identical "tdalign" tags, but still it seems like at least the first variable would have worked since the tag was different "tdnowrapalign".
<cfset i=5/>
<cfloop index = "LoopCount" from = "1" to = #i#>
<cfif i EQ 1>
<cfset dataDateTime = Replace(PageContent, "<[tdnowrapalign][^>]*>(.+?)</[td]>","","ALL")>
<cfelseif i EQ 2>
<cfset elevation = Replace(PageContent, "<[tdalign][^>]*>(.+?)</[td]>","","ALL")>
<cfelseif i EQ 3>
<cfset storage = Replace(PageContent, "<[tdalign][^>]*>(.+?)</[td]>","","ALL")>
<cfelseif i EQ 4>
<cfset outflow = Replace(PageContent, "<[tdalign][^>]*>(.+?)</[td]>","","ALL")>
<cfelseif i EQ 5>
<cfset inflow = Replace(PageContent, "<[tdalign][^>]*>(.+?)</[td]>","","ALL")>
</cfif>
<cfoutput>
<cfif isdefined("dataDateTime")>
dataDateTime = #dataDateTime#<br>
</cfif>
<cfif isdefined("elevation")>
elevation = #elevation#<br>
</cfif>
<cfif isdefined("storage")>
storage = #storage#<br>
</cfif>
<cfif isdefined("outflow")>
outflow = #outflow#<br>
</cfif>
<cfif isdefined("inflow")>
inflow = #inflow#<br>
</cfif>
</cfoutput>
<cfset i = i - 1>
</cfloop>
Does anyone know if there is a way to get to the desired outcome I described where I end up with 5 variables containing the contents of the tags contained within the "PageContent" variable?
One way of doing it would be like this
<cfset PageContent = '<tdnowrapalign=right>07/18/2020 13:00</td>
<tdalign=right>1002.12</td>
<tdalign=right>2,874,887</td>
<tdalign=right>12,766</td>
<tdalign=right>13,038</td>' />
<cfset data = ListToArray(PageContent, '</td>', false, true) />
<cfset DateTime = ListLast(data[1], '>') />
<cfset Elevation = ListLast(data[2], '>') />
<cfset Storage = ListLast(data[3], '>') />
<cfset Outflow = ListLast(data[4], '>') />
<cfset Inflow = ListLast(data[5], '>') />
Demo: https://trycf.com/gist/b4f3b630bd1cbdc505d07a7d79b68ef5/acf?theme=monokai

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>

Coldfusion 10 cfloop errors

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#" >

Checking for key existence in structure

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>