web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Copy lines to new record in same table

(0) ShareShare
ReportReport
Posted on by

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;

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rajasekhar@MS Profile Picture
    5,569 on at

    Hi

    Change your Primary key to "Document No., Line no." in the Jcs Line Table.

  • Suggested answer
    Amol Salvi Profile Picture
    18,698 on at

    Check your primary key as whenever record getting inserted it is adding same document no  to avoid that add one field such as line no /entry no and increment during copying the record.

  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    If LNO is set to AutoIncrement then don't assign any value comment your code TargJcsLine.LNO := 0. You do need to set your primary key for the Line table as Document No. and Line NO. just using Line No. is good design.

  • RainerK Profile Picture
    on at

    You are assigning your the source document no. to the new lines:

    TargetJcsLine."Document No." := SourceJcs."No.";

    ==> Shouldn't it be the TargetJcsNo

    And in Addition:

    If the error message is "Identification fields and values LNO='18653'" without stating the Document No. it seems that the document no. is not part of the Primary key in your line table.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans