I am trying to write a function to copy records in the same table with new record number.
I have 2 tables, JCs header and JcsLine. No. is the key in the header and Document No is the related in the line. I have a lno key with auto increment in the line. Copying the header record into a new record is fine, I get hung up on the line record, the error I get is "JCS Line already Exists. Identification fields and values LNO='18653'
CopyJob(SourceJcs : Record "JCS Header";TargetJcsNo : Code[20];TargetJcsDescription : Text[50];TargetCustomer : Code[20];TargetSystemType : Option)
TargetJcs."No." := TargetJcsNo;
TargetJcs.TRANSFERFIELDS(SourceJcs,FALSE);
TargetJcs.INSERT(TRUE);
IF TargetJcsDescription <> '' THEN
TargetJcs.VALIDATE(Description,TargetJcsDescription);
IF TargetSystemType <> 0 THEN
TargetJcs.VALIDATE("System Type",TargetSystemType);
IF TargetCustomer <> '' THEN
TargetJcs.VALIDATE("Customer No.",TargetCustomer);
TargetJcs.VALIDATE(Status,TargetJcs.Status::Open);
 
SourceJcsLine.SETRANGE(SourceJcsLine."Document No.", SourceJcs."No.");
IF SourceJcsLine.FIND('-') THEN BEGIN
REPEAT
TargetJcsLine.INIT;
TargetJcsLine.TRANSFERFIELDS(SourceJcsLine, FALSE);
TargetJcsLine."Document No." := SourceJcs."No.";
TargetJcsLine.LNO := 0;
TargetJcsLine.INSERT(TRUE);
UNTIL SourceJcsLine.NEXT = 0;
END;