No. Series control - Inserting Multiple Records

doddwelldoddwell Member Posts: 65
Hello

I have a table called Retro Proposal. The Retro Proposal Insert Trigger uses the NoSeriesManagement Code Unit function InitSeries.

I have a code unit called Retro Management. A function in the Retro Management code unit loops through some records and creates Retro Proposal Records. If there is only one record to create, all is well. If there is more than one record to create I get an error "The Retro Proposal Already Exists".

What's the standard way of getting around this? Is it to call RetroProposal.INSERT(FALSE) and get the Retro Management code unit to get the numbers from NoSeriesManagement rather than getting the insert trigger to get the numbers?

Thanks for any advice, Simon

Comments

  • MBergerMBerger Member Posts: 413
    Empty the field which the numberseries fills before inserting, it is probably still filled with the number from the previous record ( INIT does not initialize primary key fields )
  • doddwelldoddwell Member Posts: 65
    Hello

    I'm not sure what you mean - how do I "empty" the field? This is the Code Unit Snippet (it's part of a repeat loop so lots of records could get created):


    IF recSalesInvoiceHeader.FINDFIRST THEN BEGIN
    recRetroDiscProposalHeader.INIT;
    recRetroDiscProposalHeader."Sell-to Customer No." := CustomerNumber;
    recRetroDiscProposalHeader."Retro. Disc. Header No." := recRetroDiscSchedule."Retro. Disc. Header No.";
    recRetroDiscProposalHeader."Start Date" := recRetroDiscSchedule."Start Date";
    recRetroDiscProposalHeader."End Date" := recRetroDiscSchedule."End Date";
    recRetroDiscProposalHeader."Posting Type" := recRetroDiscSchedule.Type;
    recRetroDiscProposalHeader."Retro Disc. Schedule No." := recRetroDiscSchedule."No.";
    recRetroDiscProposalHeader.INSERT(TRUE);
    END;

    This is the code in the Insert Trigger on the Proposal Table:

    IF "No." = '' THEN BEGIN
    RetroDiscSetup.GET;
    RetroDiscSetup.TESTFIELD(RetroDiscSetup."Retro Discount Nos.");
    NoSeriesMgt.InitSeries(RetroDiscSetup."Retro Discount Nos.",xRec."No. Series",0D,"No.","No. Series");
    END;

    Thanks very much.
  • KishormKishorm Member Posts: 921
    After the...

    recRetroDiscProposalHeader.INIT;

    ...you need to add...

    recRetroDiscProposalHeader."No." := '';

    ...to clear the value so that it is set again to the next number in OnInsert trigger.
  • geordiegeordie Member Posts: 655
    As MBergerm said, INIT function doesn't initialize primary key fields, so when you create the second record and recRetroDiscProposalHeader.INSERT(TRUE) is called, "No." field is still filled with the no. of the first record and code inside the IF statement isn't executed.

    Substitute INIT with a CLEAR function and should work.
  • KishormKishorm Member Posts: 921
    As Geordie said, CLEAR will also work (instead of INIT) but note that this also has other side effects, e.g.

    - filters will be cleared
    - current key will be reset
    - most importantly in your case, and fields with an InitValue property specified will NOT be set to the InitValue value but instead will be set to the default value (e.g. 0 for integer/decimal, false for Boolean etc...)

    ...for these reasons it's better to leave the INIT there and then explicitly clear the "No." Field value instead of using the CLEAR statement.
  • mdPartnerNLmdPartnerNL Member Posts: 802
    Kishorm wrote:
    As Geordie said, CLEAR will also work (instead of INIT) but note that this also has other side effects, e.g.

    - most importantly in your case, and fields with an InitValue property specified will NOT be set to the InitValue value but instead will be set to the default value (e.g. 0 for integer/decimal, false for Boolean etc...)

    ...for these reasons it's better to leave the INIT there and then explicitly clear the "No." Field value instead of using the CLEAR statement.

    Off-Topic: Are you sure about this? After a CLEAR of GL Entry for example, the field "Open" is still yes.
  • KishormKishorm Member Posts: 921
    EDIT: Oops, I was wrong, The Microsoft documentation says it does initialise the record...

    http://msdn.microsoft.com/en-us/library/dd355114.aspx
  • doddwelldoddwell Member Posts: 65
    Thanks for all of your replies. I tried both methods and both worked:

    Method 1:
    recRetroDiscProposalHeader.INIT;
    CLEAR (recRetroDiscProposalHeader."No.");

    Method 2:
    CLEAR (recRetroDiscProposalHeader);

    Based upon the comments, I have opted for Method 1
Sign In or Register to comment.