Copy record using transferfields with records of linked table

DamjanDamjan Posts: 20Member
Hi All,

I need a some advice on how to link a table to a source table from the one I have to copy record. Source table has some records in a linked table and I would like to copy also these records of the linked table.

code:
JobTaskRec.CHANGECOMPANY(CompanyTo); //copy record in another company
JobTaskRec.TRANSFERFIELDS(SourceJobTask); //user's record to be copied
JobTaskRec.INSERT(TRUE);

CopyJobTasks.CopyProjectTasks -> this is the function I would call after recors in the main table=JobTaskRec are inserted.

I was looking at CU80, but it does not work on that way, or let me say, that I would really like to have as little code inserted as possible, since we are going to migrate to BC and I do now want to have a lot of work with migration:)

Thanks Damjan

Best Answer

  • DamjanDamjan Posts: 20
    Accepted Answer
    I have now resolved all the issues, except this one.

    In the codeunit this code is inserted on OnRun trigger:

    //Transfers a record of Job (always only 1 record). This is header table
    JobToRec.CHANGECOMPANY(CompanyTo);
    JobToRec.TRANSFERFIELDS(SourceJob);
    JobToRec.INSERT(TRUE);

    //Finds all records of linked table to header table. SlourcJobTaskF is linked table
    SourceJobTaskF.SETRANGE("Job No.",SourceJob."No."); //SourceJob is header table
    IF SourceJobTaskF.FINDSET THEN
    REPEAT
    TargetJobTaskF.INIT;
    TargetJobTaskF.CHANGECOMPANY(CompanyTo);
    TargetJobTaskF.TRANSFERFIELDS(SourceJobTaskF,FALSE);
    TargetJobTaskF.INSERT;
    UNTIL SourceJobTaskF.NEXT = 0;

    This means that SourceTable is the header table with only 1 records of the job (project) and in lines of the project (job) there are 3 records (entries).
    Code sets the range to those 3 records as expected, but when all 3 recrods are transferred I get an error:

    Microsoft Dynamics NAV
    The Job Task already exists. Identification fields and values: Job No.='',Job Task No.=''
    OK

    SOLUTION: Deleted blank record in source table.

Answers

  • DamjanDamjan Posts: 20Member
    Accepted Answer
    I have now resolved all the issues, except this one.

    In the codeunit this code is inserted on OnRun trigger:

    //Transfers a record of Job (always only 1 record). This is header table
    JobToRec.CHANGECOMPANY(CompanyTo);
    JobToRec.TRANSFERFIELDS(SourceJob);
    JobToRec.INSERT(TRUE);

    //Finds all records of linked table to header table. SlourcJobTaskF is linked table
    SourceJobTaskF.SETRANGE("Job No.",SourceJob."No."); //SourceJob is header table
    IF SourceJobTaskF.FINDSET THEN
    REPEAT
    TargetJobTaskF.INIT;
    TargetJobTaskF.CHANGECOMPANY(CompanyTo);
    TargetJobTaskF.TRANSFERFIELDS(SourceJobTaskF,FALSE);
    TargetJobTaskF.INSERT;
    UNTIL SourceJobTaskF.NEXT = 0;

    This means that SourceTable is the header table with only 1 records of the job (project) and in lines of the project (job) there are 3 records (entries).
    Code sets the range to those 3 records as expected, but when all 3 recrods are transferred I get an error:

    Microsoft Dynamics NAV
    The Job Task already exists. Identification fields and values: Job No.='',Job Task No.=''
    OK

    SOLUTION: Deleted blank record in source table.
Sign In or Register to comment.