Transferfields

upasanisandipupasanisandip Member Posts: 405
Hi all,
I want to use TRANSFERFIELD.


SalesOrderEX.SETFILTER(SalesOrderEX."Sales Order No", SalesHeader."No.");
IF SalesOrderEX.FIND('-') THEN
BEGIN
SalesInvoiceEX.INIT;
SalesInvoiceEX.TRANSFERFIELDS(SalesOrderEX);
SalesInvoiceEX.INSERT;
END;

Is the above code correct?


thanks.

Comments

  • WaldoWaldo Member Posts: 3,412
    Like the help says:
    Use this function to copy all matching fields in one record to another record.
    It just copies over all matching fields (field number / data type). So the code should work, if both your tabledefinitions are alike.

    comments:
    - You code is also going to copy over the primary key fields.
    - If your record contains a blob field, you should calculate the Blob field before the transferfields... .

    It should work (if all reqs are fullfilled) ... what is wrong with it?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • krikikriki Member, Moderator Posts: 9,110
    TRANSFERFIELDS does NOT copy the primary-key fields (if you also want this to happen, you need to add a secondary parameter : TRUE [see also the online-help for TRANSFERFIELDS])

    But I suppose you want to automatically generate a new primary key. In this case this will be the code (I also cleaned up your code a little):
    SalesOrderEX.RESET;
    SalesOrderEX.SETCURRENTKEY(Sales Order No");
    SalesOrderEX.SETRANGE(Sales Order No",SalesHeader."No.");
    IF SalesOrderEX.FINDFIRST THEN BEGIN
      CLEAR(SalesInvoiceEX); // INIT does NOT clean the primary key fields
      SalesInvoiceEX."Document Type" := SalesInvoiceEX."Document Type"::Invoice;
      SalesInvoiceEX.INSERT(TRUE); // because "No." is blank, it will generate a new number.
      SalesInvoiceEX.TRANSFERFIELDS(SalesOrderEX);
      SalesInvoiceEX.MODIFY(FALSE);
    END;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • WaldoWaldo Member Posts: 3,412
    I'm afraid you're wrong.

    From help:
    If the InitPrimaryKeyFields parameter is set to true, and the records are in the same table, the TRANSFERFIELDS function will change both the timestamp and the Primary Key fields of the destination record.

    If the InitPrimaryKeyFields parameter is set to true, and the records are not in the same table, the TRANSFERFIELDS function will change the Primary Key fields of the destination record (if the fields fulfill the conditions specified above) but the timestamp of the destination record will not be changed.

    ...

    InitPrimaryKeyFields
    Data type: Boolean
    Default value: True

    IfI understand correctly, the default value is true, and if it is true, it will change the primary key fields of the destination record (for me, that copying over...).

    Anyway, I did one simple test:
    lrecCustomer.GET('10000');
    ltmprecCustomer."No." := '203049';
    ltmprecCustomer.INSERT;
    MESSAGE(ltmprecCustomer."No.");
    ltmprecCustomer.TRANSFERFIELDS(lrecCustomer);
    MESSAGE(ltmprecCustomer."No.");
    
    And the second message shows '10000'... .

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • krikikriki Member, Moderator Posts: 9,110
    And I always thought the the default was FALSE. :oops:
    Luckily in my programming, I always put code for changing fields (including the primary key) I want to change AFTER the TRANSFERFIELDS. \:D/
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • WaldoWaldo Member Posts: 3,412
    kriki wrote:
    And I always thought the the default was FALSE. :oops:
    Luckily in my programming, I always put code for changing fields (including the primary key) I want to change AFTER the TRANSFERFIELDS. \:D/

    I do this myself as well ... .
    Makes things much clearer.

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • DenSterDenSter Member Posts: 8,305
    Defensive programming :mrgreen:
  • WaldoWaldo Member Posts: 3,412
    Well,
    a few years ago, I started the concept "lazy programming" at our company. Pretty interesting tips&tricks already :)

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • krikikriki Member, Moderator Posts: 9,110
    Waldo wrote:
    Well,
    a few years ago, I started the concept "lazy programming" at our company. Pretty interesting tips&tricks already :)
    Care to write a "How to be a lazy programmer?" :mrgreen:
    Or a topic in tips&tricks?

    Maybe others can add tips to it.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • WaldoWaldo Member Posts: 3,412
    It is already planned in my OneNote :wink: .
    When I'm on a plane or something, I will write it out... .

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • DenSterDenSter Member Posts: 8,305
    That should really be called "how to make things easier on yourself"
  • kinekine Member Posts: 12,562
    :-) I am definitely lazy programmer, I am able to spent 5 hours of programming to develop tool, which will solve some problem in 2 minutes (without the tool the problem will be solved manually in 2 hours...) :-) - but yes, next time I will have same problem it will be just 2 minutes to solve it. :mrgreen:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • WaldoWaldo Member Posts: 3,412
    That's exactly what i was talking about ...
    I have added a download some time ago ... the table generator .. that is one of the tools that made our live easier.

    I have a feeling we're going a bit off topic here ... :oops:

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
Sign In or Register to comment.