Skip to main content
Inspiring
November 2, 2009
Question

listGetAt error

  • November 2, 2009
  • 1 reply
  • 2592 views

Good morning all,

I am using this code:

<cfif listlen(list1) gt 0>
Units:
<cfloop from="1" to="#listlen(list1)#" index="j">
<cfoutput>#listGetAt(list1, j)# </cfoutput>
</cfloop>
<br />
Defect Codes:
<cfloop from="1" to="#listlen(list2)#" index="k">
<cfoutput>#listGetAt(list2, k)#</cfoutput>
</cfloop>
</cfif>

I keep getting this error:

In function ListGetAt(list, index [, delimiters]), the value of index, 6, is not a valid as the first argument (this list has 5 elements). Valid indexes are in the range 1 through the number of elements in the list.

Has anyone resolved this issue?

Thanks in advance,

DJ Khalif

This topic has been closed for replies.

1 reply

Inspiring
November 2, 2009

This would be more reliable, also faster.

<cfif listlen(list1) gt 0>
Units:
<cfloop list="#List1#" index = "abc">
<cfoutput>#abc# </cfoutput>
</cfloop>

To troubleshoot your current code, start displaying data.

<cfif listlen(list1) gt 0>

<cfdump var = "List 1 is #list1#>

<cfflush>
Units:
<cfloop from="1" to="#listlen(list1)#" index="j">
<cfoutput>j is #j# list item is  #listGetAt(list1, j)# </cfoutput>

<cfflush>
</cfloop>
<br />
and something similar for your other list.

djkhalifAuthor
Inspiring
November 2, 2009

Dan,

Thanks, I used this:

<cfif listlen(list1) gt 0>
Units:
<cfloop list ="#list1#" index="j">
<cfoutput>#j# </cfoutput>
</cfloop>
</cfif>
<br />
<cfif listlen(list2) gt 0>
Defect Codes:
<cfloop list ="#list2#" index="k" >
<cfoutput>#k#</cfoutput>
</cfloop>
</cfif>

Now, can you make sense out of this:

<cfquery name="finalDefects" datasource="#REQUEST.datasource#">
INSERT INTO tbl_Final_Defects (ID, Unit, DefectCode)
SELECT ID, '#Trim(FORM.Unit)#', '#Trim(FORM.DefectCode)#'
FROM tbl_FinalInspAttr
WHERE tbl_FinalInspAttr.WorkOrder = '#FORM.WorkOrder#' and 1 = 2
<cfloop list="#FORM.UNIT#" index="j" >
UNION
SELECT DISTINCT #Trim(FORM.UNIT)#, '#j#'
FROM tbl_DefectTypes
</cfloop>
</cfquery>

How can I get both list to populate the database?

Inspiring
November 2, 2009

If the two lists are the same length, a from, to loop in your query is the way to go.  Couple of notes though:

In union queries you don't need the keyword distinct.

Your current query is not getting the ID value you want.   What's the logic you intended to use to get that?