Link to home
Start Free TrialLog in
Avatar of CraigDerington
CraigDerington

asked on

ListGetAt Error in Cold Fusion - Need Help

Hello EE.

I inherited a CF site recently and have been able to work through most of the issues I have run into.  However, there is this one problem which I can not solve.  I have a page with a form that contains 4 elements.  Date, PaymentAmount, DisbursementID, and I have now added PayNote.

The query looks like this:

<CFELSEIF IsDefined("FORM.updateAllDisbursements")>
      <CFQUERY DATASOURCE="#APPLICATION.datasource#">
            <CFLOOP FROM="1" TO="#ListLen(FORM.disbursementID)#" STEP="1" INDEX="i">
            <CFIF i IS NOT "">
                  UPDATE disbursements
                     SET paymentDueDate = #CreateODBCDate(CreateDate(ListGetAt(FORM.year,i), ListGetAt(FORM.month,i), ListGetAt(FORM.day,i)))#, paymentAmount = #ListGetAt(FORM.paymentAmount,i)#, payNote = '#ListGetAt(FORM.payNote,i)#'
                   WHERE disbursementID = #ListGetAt(FORM.disbursementID,i)#;
            </CFIF>
            </CFLOOP>
      </CFQUERY>

The form can contain multiple payments and a ListGetAt function is being used to put all of the elements into a list to loop through and update the database for each Disbursement ID.  After reading a lot of questions and answers on EE yesterday and this morning about this particular CF function, I am still getting the index error.  I know that is a element is empty, CF ignores it.

Let's say there are 15 payments that need to be updated.  If the end user loads the form displaying the 15 payments that need to be rescheduled but only needs to add a note for 3 of the 15 payments, when they click Update All, I get this error.

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

Any ideas?  As always, thank you in advance for your help.

Craig
Avatar of gdemaria
gdemaria
Flag of United States of America image


Right, just finishing up another question on this, you may find the answers you need here...

https://www.experts-exchange.com/questions/22463420/CFPARAM-for-form-submittal.html

In short, you need to convert the empty cells of your list to real values so the list functions won't skip over them...

 <cfset variables.myList = replace(variables.myList, ",,",  ",NULL,", "all")>
 
then whenever you see the word NULL, you take it to mean EMPTY.

It's a pain for sure.   The better way is not to use lists but to use an "array" style input tags.   Follow the link above for details.   Please see posts with my username.

Avatar of CraigDerington
CraigDerington

ASKER

Thank you for the quick reply.  I will review the other EE Expert Answer you quoted.  

Thank You!


Craig
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since we are no longer using the CF ListGetAt function, is it still necessary to peforms the replace on the empty elements?

 nope, you can remove that.
The guy who originally designed this uses a CF include - dateSelect.cfm to generate the drop downs for the data menu (if the end user needs to reschedule the disbursement for another date, along with the other fields I have already mentioned.  I saved the dateSelect.cfm as dateSelect2.cfm and appended the field names with the #variables.ct#.  In my form, I have also appended the other form elements with the #variables.ct#.

It seems that when I view the source after loading the page, the date fields month, day, year all have the counter appended to the name like year.15, but my other form elements names are paymentAmount#variables.ct# and not paymentAmount15.  Is this because the included dateSelect2.cfm is getting the variables.ct count and it can not also append the other form elements with the correct count?

I'm stuck.

Thanks for your help.

Craig
Here is the form.

<form action="receiptsDisbursements.cfm" method="post">
<CFLOOP query="disbursementList">
<CFSET variables.ct = disbursementList.currentRow>
<tr class="color-row"><td width="16%" height="20">Disbursement Date:</td>
<td height="20"><CFSET date = DateFormat(paymentDueDate, "MM/DD/YYYY")>      <CFINCLUDE TEMPLATE="includes/dateSelect2.cfm">
</td>
</tr>
<tr class="color-row"><td width="16%" height="20">Disbursement Amount:</td>      
<td height="20"><input type="text" name="paymentAmount#variables.ct#" value="#NumberFormat(paymentAmount, '.99')#" size="5" />
&nbsp;&nbsp;Payment Note:
<input name="payNote#variables.ct#" type="text" class="color-row" id="payNote" value="#payNote#" size="30" maxlength="200" /><input type="hidden" name="totalRecords" value="#variables.ct#">
</td>
</tr>
<tr align="left" class="color-row">
<td colspan="3" height="20"><input type="hidden" name="disbursementID#variables.ct#" value="#disbursementID#" /></td>
</tr>
</CFLOOP>
<tr align="left" class="color-row">
<td colspan="3" height="20">
<input type="submit" name="updateAllDisbursements" value="Update All Disbursements" /></td></tr>
</form>


The form processor code:

<CFELSEIF IsDefined("FORM.updateAllDisbursements")>
<CFQUERY DATASOURCE="#APPLICATION.datasource#">
<CFLOOP INDEX="i" FROM="1" TO="#FORM.totalRecords#">
UPDATE disbursements
SET paymentDueDate = #CreateODBCDate(CreateDate((FORM.year & i), (FORM.month & i), (FORM.day & i)))#,
paymentAmount = #(FORM.paymentAmount & i)#,
payNote = #('FORM.payNote' & i)#
WHERE disbursementID = #(FORM.disbursementID & i)#;
</CFLOOP>
</CFQUERY>

From looking at the other answer, this looks accurate...




>  It seems that when I view the source after loading the page, the date fields month, day, year all have the counter appended to the name like year.15, but my other form elements names are paymentAmount#variables.ct# and not paymentAmount15.

When this happens its because the coldfusion variable is not being processed, you need to surround this in CFOUTPUT.

this format you are using is not quite right...

FORM.paymentAmount & i

Should be..

FORM['paymentAmount' & i]
OK.  I have worked through all of that and have the form and the values displaying properly.  When I click Update All I get this CF error:

The value "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19" cannot be converted to a number

Something is amiss with my processing code I believe.
Ahh, I will try that right now.  Thanks.

Your totalRecords input tag is INSIDE your CFLOOP, that means it will be a comma delimited list of 1,2,3,4,5..   I think you want it to be outside your CFLOOP so you get just one value, the total count.

  <tr align="left" class="color-row">
     <td colspan="3" height="20"><input type="hidden" name="disbursementID#variables.ct#" value="#disbursementID#" /></td>
 </tr>
</CFLOOP>

<input type="hidden" name="totalRecords" value="#variables.ct#">

Do you see a problem with this.  

UPDATE disbursements
SET paymentDueDate = #CreateODBCDate(CreateDate(FORM['year' & i], FORM['month' & i], FORM['day' & i]))#,
paymentAmount = #FORM['paymentAmount' & i]#,
payNote = #FORM['payNote' & i]#
WHERE disbursementID = #FORM['disbursementID' & i]#;  


Now I am getting this error

Error Executing Database Query.  
[Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near the keyword 'WHERE'.  
 
I'm going to try and remove the commas and the semi-colon from the query.

is your payNote a string?

 if so, add single quotes...

  payNote = '#FORM["payNote" & i]#'
Yes, it is a string.

Thank you very much.  That did it.  I was trying to do the single quote around the #FORM['payNote' & i] instead of the entire string '#FORM["payNote" & i]#' and I wasn't using double quotes for the payNote inside the brackets like you have above.

I will be awarding the points to you immediately.

Thank you again for your exceptional EXPERT support.

Craig

 excellant, glad I could lend a hand