Problem using RecordRef and Changecompany

TbiTbi Member Posts: 33
Hi.

I am trying to copy a lot of tables from one company to another using RecordRef. But it seems that the OPEN method doesn't work as planned.

This is the code I am trying to execute, (I have set filter on just one table here to test..)
rTables is of record 2000000028 with information about tables.
sOldCompany := 'Company1';
sNewCompany := 'Company2';
CLEAR(rTables);

rTables.SETRANGE("Tabellnr.", 3);
rTables.FIND('-');


REPEAT
   CLEAR(rrRecordRef);
   rrRecordRef.OPEN(rTables."Tabellnr.");
   rrRecordRef.FIND('-');
   REPEAT
       CLEAR(rrRecordRefNC);
       rrRecordRefNC.OPEN(rTables."Tabellnr.", FALSE, sNewCompany);
       rrRecordRefNC.INIT;
       rrRecordRefNC := rrRecordRef.DUPLICATE;
       rrRecordRefNC.INSERT;                                //Gets an error here...
       rrRecordRefNC.CLOSE;
   UNTIL rrRecordRef.NEXT = 0;

   rrRecordRef.CLOSE;

UNTIL rTables.NEXT = 0;


I always get the error that the post already exists, but the Company2 is empty. So does anyone have an idea what I am doing wrong here?

Tbi.

Answers

  • krikikriki Member, Moderator Posts: 9,094
    The DUPLICATE, copies the source-variable to the destination-variable, but in THE SAME COMPANY. This is what creates the error.
    You have to copy the fields 1 by 1.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • TbiTbi Member Posts: 33
    Thanks for fast reply.

    Now I have added some code to loop trough the fields in the table and copying the values, but it still gives me the same error..
    Am I missing something here?
    sOldCompany := 'Company1';
    sNewCompany := 'Company2';
    CLEAR(rTables);
    
    rTables.SETRANGE("Tabellnr.", 3);
    rTables.FIND('-');
    
    REPEAT
       CLEAR(rrRecordRef);
       rrRecordRef.OPEN(rTables."Tabellnr.");
       rrRecordRef.FIND('-');
       REPEAT
           CLEAR(rrRecordRefNC);
           rrRecordRefNC.OPEN(rTables."Tabellnr.", FALSE, sNewCompany);
           rFields.SETRANGE(rFields.TableNo, rTables."Tabellnr.");
           rFields.FIND('-');
           rrRecordRefNC.INIT; 
          
           //Loops trough fields..
           REPEAT
             CLEAR(frFieldRef);
             frFieldRef := rrRecordRefNC.FIELD(rFields."No.");
             frFieldRef.VALUE := rrRecordRef.FIELD(rFields."No.").VALUE;
           UNTIL rFields.NEXT = 0;
    
           rrRecordRefNC.INSERT;                                //Gets an error here...
           rrRecordRefNC.CLOSE;
       UNTIL rrRecordRef.NEXT = 0;
    
       rrRecordRef.CLOSE;
    
    UNTIL rTables.NEXT = 0;
    
  • TbiTbi Member Posts: 33
    I found the problem....

    I had forgotten to set a filter on Companyname on rTables, so it tried to insert into the same table more than once.. :whistle:

    So I just added the line:
    rTables.SETRANGE(rTables.Selskapsnavn, sOldCompany);
    

    And then it works correct..

    - Tbi
  • RenanRenan Member Posts: 1
    Thanks Tbi and kriki.

    I was also trying to migrate data from one company to another using RecordReference but I wasn't succeeding, I even used the DUPLICATE function but then I couldn't move forward, but now I did. Thanks!
Sign In or Register to comment.