Insert Number Series Problem

IdlenessIdleness Member Posts: 46
Hi

I have searched the forum but I cannot find anything that relates to this.

When I insert a record into a table the first time it is run there is no problem, however the second time it's run (assuming I haven't closed the originating form) then I get an issue.

I have tracked it to here in On Insert on the table.

IF "No." = '' THEN BEGIN
NoSeriesMgt.InitSeries('QC-COFA',mSeries,0D,"No.",mSeries);
END;

The first time "No." is blank so there is no problem. For some reason on the second insert (this is manually run) the "No." isn't blank and of course it throws a "Number already exists" problem.

I am sure this is something simple but any help greatly appreciated.

Answers

  • kapamaroukapamarou Member Posts: 1,152
    Is this the only code in that trigger?
    I think that
    NoSeriesMgt.InitSeries('QC-COFA',mSeries,0D,"No.",mSeries);
    
    is wrong since it uses a constant: 'QC-COFA'. You should avoid this.
    Is this your code?
  • IdlenessIdleness Member Posts: 46
    Yes it's mine, the issue isn't the constant. The issue is why isn't the "No." blank when I come to insert a second record.

    Number series gets updated, everything runs perfectly on first execution.
  • DenSterDenSter Member Posts: 8,305
    We wouldn't know why "No." is not blank, because you're not showing us all of the code. One way around this is to put an assignment to '' right before the call to the number series management codeunit.
  • IdlenessIdleness Member Posts: 46
    Sorry, I just assumed it was something simple.

    A record gets inserted after picking the next number correctly. The next time it's run (assuming the originating screen hasn't been closed and reopened) it fails at the above point.

    The trigger on the table gets fired and "No" isn't blank the second time, it takes the "No" from the previous insert by default.

    The code
    CofAHeader.INIT;
    CofAHeader."Lot No." := LotNumber;
    CofAHeader."Item No." := ItemNo;
    CofAHeader.Description := Description;
    CofAHeader.Quantity := QtyLcl;
    CofAHeader."Active Version" := 1;
    CofAHeader."Creation Date" := TODAY;
    CofAHeader."Batch Multiple" := LotQtyLcl;
    CofAHeader.INSERT(TRUE);
  • AlbertvhAlbertvh Member Posts: 516
    Hi

    Put this before the INIT Statement.

    CLEAR(CofAHeader);
    or
    CofAHeader."No." := '';

    Hope this helps


    Albert
  • IdlenessIdleness Member Posts: 46
    Bless you sir :D
  • AlbertvhAlbertvh Member Posts: 516
    Glad it worked.
    DenSter gave you the tip which on re-reading would also have given you the solution
    One way around this is to put an assignment to '' right before the call to the number series management codeunit.


    Albert
  • DenSterDenSter Member Posts: 8,305
    To elaborate a little on that: INIT initializes all fields EXCEPT PRIMARY KEY FIELDS. If you have primary key values in your record variable, and you do INIT, that will not clear the primary key values.

    Make it a habit of taking care of primary key values every time you use INIT. You can do this by either of two ways:
    1 - Use CLEAR, which destroys the variable, and when INIT initializes the record, the primary key fields don't have a value. This is an implicit way of clearing the primary key fields.

    2 - Do INIT first, and then set the primary key fields manually. This is an explicit way, which is very useful when you want to create a whole batch of records in a table with a compound primary key, and you don't want to have to set all primary key fields every time. For instance, you are creating a new sales invoice:
    SalesHeader."Document Type" := SalesHeader."Document Type"::Invoice;
    SalesHeader.INIT; // At this point, the document type is still Invoice
    SalesHeader."No." := '';
    SalesHeader(INSERT(TRUE); // The OnInsert triger takes care of numbering series
    
  • IdlenessIdleness Member Posts: 46
    Thanks again. I see it now.

    Cheers guys.
Sign In or Register to comment.