Incrementing Primary Key

I have a very simple code unit that is trying to insert records into the Sales Header and Line tables, based on input from a bespoke table. The basic code is as follows; IF InputData.FIND(’-’) THEN REPEAT SalesHeader.INIT; SalesHeader.“Document Type” := SalesHeader.“Document Type”::Invoice; SalesHeader.VALIDATE(“No.”); SalesHeader.INSERT(TRUE); SalesHeader.VALIDATE(SalesHeader.“Sell-to Customer No.”,InputData.Customer); SalesHeader.VALIDATE(SalesHeader.“Order Date”,WORKDATE); SalesHeader.MODIFY; NextLineNo:= 10000; SalesLine.INIT; SalesLine.“Document Type” := SalesHeader.“Document Type”; SalesLine.“Document No.” := SalesHeader.“No.”; SalesLine.“Line No.” := NextLineNo; SalesLineRec.VALIDATE(Type,1); SalesLineRec.VALIDATE(“No.”,‘6310’); SalesLine.VALIDATE(“Shipment Date”,InputData.“Trip Date”); SalesLine.VALIDATE(Quantity,1); SalesLine.VALIDATE(SalesLine.“Unit Price”,InputData.“Meter Fare”); SalesLine.INSERT(TRUE); SalesLine.MODIFY; NextLineNo := NextLineNo + 10000; InputData.“Line Polled” := TRUE; InputData.MODIFY; UNTIL InputDataRec.NEXT = 0; MESSAGE(‘Orders Generated’); If I run this for a single input record, it works fine and creates a Sales Invoice that I can post. However, if I have 2 records in my input table, then the program fails on the second loop through as it tries to create a second Sales Header record with the same Document No. Why is the second document No not being allocated correctly? I thought the validate function would handle this. TIA

Hi TIA, I think is because the second time the SalesHeader.“No.” contains the no. of previous document. The INIT does not clear the primary key fields. so try to clear the “no.” field. SalesHeader.VALIDATE(“No.”,’’); Elena

Hi Elena, First, TIA is just short for Thanks In Anticipation [:)] I agree that the second time the SalesHeader.“No.” contains the no. of previous document. I’ve put in some messages to confirm this is happening. However, I can’t use your suggestion of .VALIDATE(“No.”,’’) as there is a no. series behind the No. field on the Sales Header. Therefore, I need the next number to be allocated dynamically. I originally thought that the SalesHeader.VALIDATE(“No.”) statement would achieve this. Thanks for the help so far.

Think I’ve solved my own problem. [:D] I added a CLEAR(SalesHeader.“No.”) prior to my validate for that field, and that seems to have solved the problem Thanks anyway for the help

Hi Skippy, What Elena means is that you have to set your “No.”-field to blank. When you use SalesHeader.INIT, you do NOT clear the values in primary-key fields. Your INSERT(TRUE)-statement only gives you a new number, if the “No.”-field is blank (which it is not in your 2. run). Either you have to use CLEAR(SalesHeader); instead of SalesHeader.INIT; Or you have to set the “No.”-field to blank SalesHeader.“No.” := ‘’; regards Alexander