Skip to main content
Known Participant
January 19, 2009
Question

How to insert line item numbers

  • January 19, 2009
  • 3 replies
  • 754 views
I have a form where the user enters one or more part numbers, and a quantity for each part number. When the form is submitted, I do a cflocation to another page that displays the part number and then input fields (serial numbers) for each part number, based on the quantity. For example, of the quantity for part number pn1 is 3, then I dynamically generate three fields to input serial numbers for that part number.

Here is the code I use :

<td class="TitleText">
<cfoutput query="qrySerialNumbers" group="partNumber"><font face="Verdana" size="2"><b>Part Number:</b> #partNumber#</font><p>
          
<cfif quantity is 1>
<b>Serial Number:</b><br>
<cfelseif quantity gt 1>
<b>Serial Numbers:</b><br>
</cfif>
<cfloop from="1" to="#quantity#" index="i">
<table border="0">
<tr>
<td width="33" align="right" class="TitleText"><b>#i#:</b> </td>
<td width="148" align="right" clsas="TitleText"><cfinput type="text" name="serialNumber" size="40"><input type="hidden" name="PartNumberID" value="#PartNumberID#"><br></td>
</tr>

This displays the part number once, the title Serial Number or Nubmers, a line item count for each serial number, and the input fields for the serial numbers. So for part number PN1, there are three serial numbers, so it woul have line item #1, serial number input, line item #2, serial number input, line item #3, serial number input. etc...If there is another part number, it skips a line and displays the same info for the next part number, etc.

Here is the problem I am having. The part numbers are stored in one table along with a part number id, and the serial numbers are stored in another table, along with the part number id. I join the two tables together via the part number id to get the part number and its assoicatied serial numbers.When I submit the form, I need to also insert the line items for each serial number. So part number pn1 is inserted into the part number table and the part number id is 1. Serial numbers sn1, sn2, and sn3 are inserted into the serial number table along with part number id 1. The line item numbers inserted should be 1, 2, 3. If there is another part number/serial number, those are also inserted but the line items continue to increment, 4, 5,6..etc. instead of resetting at 1 because it is a new part number.

Here is modfied version of my insert code :

<cfloop from="1" to="#listlen(form.serialNumber)#" index="i">
<cfquery name="qrySerialNumberID" datasource="recDisc">
select max(SerialNumberID) as maxSerialNumberID
from test_SerialNumbers
</cfquery>
<!--- Get the next transaction id by adding 1 to the last number --->
<cfset nextSerialNumberID = #qrySerialNumberID.maxSerialNumberID# + 1>


<cfset col2 = listgetat(form.PartNumberID, i)>
<cfset col3 = listgetat(form.serialNumber, i)>

<cfquery name="qryInsertSerial" datasource="recDisc">
insert into test_SerialNumbers
(serialNumberID,
PartNumberID,
serialNumber,
status,
lineItem,
closedFlag)

values
('#nextSerialNumberID#',
'#col2#',
<cfif col3 is "">
'None',
<cfelse>
'#ucase(col3)#',
</cfif>
'1',
'#i#',
'')

</cfquery>
</cfloop>

Sorry for the long post, had to explain what I am trying to do.

How do I get the line item numbers to reset for each part number ?
    This topic has been closed for replies.

    3 replies

    Inspiring
    January 19, 2009

    <cfscript>
    ControlPartNumber = "ThisIsNotAPartNumber;
    ThisLineItem = 0;
    </cfscript>
    <cfloop list="#form.fieldnames#" index = "element">
    <cfscript>
    if (left(element, 12) is "SerialNumber") {
    ThisPartNumber = removechars(element, 12);
    ThisSerialNumber = form[element];

    if (ThisPartNumber is ControlPartNumber) {
    ThisLineItem = ThisLineItem + 1;
    }
    else {
    ControlPartNumber = ThisPartNumber;
    ThisLineItem = 1;
    }

    } // serial number element
    <cfscript>

    or something like that
    Inspiring
    January 19, 2009
    You don't need the hidden field. The code is something like this:

    <cfloop list="#form.fieldnames#" index = "element">
    <cfscript>
    if (left(element, 12) is "SerialNumber") {
    ThisPartNumber = removechars(element, 12);
    ThisSerialNumber = form[element];
    <cfscript>
    Then you can use those two variables in your query.
    Known Participant
    January 19, 2009
    OK, I understand what you are doing here, as far as extracting the serial number and the part number. I just need to substitute the part number with the part number id, since that is what is in the table with the serial number.

    But what I still do not understand is that during the insert, how/where, does it know that a diffenernt part number/part number ID is being inserted, so that the line item numbers reset to 1 and do not continously increment ?
    Inspiring
    January 19, 2009
    You have form 1 where the user submits some part numbers, right?

    Then you have form 2 where the user submits some serial numbers for each part number, right?

    On form 2, make the part number part of the input name where the user is entering serial numbers. Then look for those part numbers when you process form 2.

    You'll need to use array notation. If you don't know how, ask.
    Known Participant
    January 19, 2009
    So my serial number input field would be :

    <cfinput type="text" name="serialNumber#partNumber#" size="40">
    <input type="hidden" name="PartNumberID" value="#PartNumberID#">

    I would keep the hidden partNumberID because that is how I relate the serial number back to the part number ?

    For the insert part, how do I search/extract the part number so that the insert resets each time the part number changes ?

    If I am totally off base, can you give me an example (code) of the array notation you are talking about ?

    thanks