BCApps icon indicating copy to clipboard operation
BCApps copied to clipboard

[Bug]: codeunit 5510 "Production Journal Mgt" - SetTemplateAndBatchName

Open auzhhv opened this issue 6 months ago • 6 comments

Describe the issue

There is a bug in codeunit 5510 "Production Journal Mgt" in procedure SetTemplateAndBatchName.

        ItemJnlTemplate.SetRange("Page ID", PageID);
        ItemJnlTemplate.SetRange(Recurring, false);
        ItemJnlTemplate.SetRange(Type, PageTemplate);
        if not ItemJnlTemplate.FindFirst() then begin
            ItemJnlTemplate.Init();
            ItemJnlTemplate.Recurring := false;
            ItemJnlTemplate.Validate(Type, PageTemplate);
            ItemJnlTemplate.Validate("Page ID"); // This should validate PageID

When the record is inserted it is missing page id. The next time the same procedure is called it will still not find the record (because of the filters), but try to insert the same record it has already inserted.

Expected behavior

If a record with the page id is missing, it should insert a record with the same page id, and not 0.

Steps to reproduce

View the example, it's a straight forward issue.

Additional context

No response

I will provide a fix for a bug

  • [x] I will provide a fix for a bug

auzhhv avatar May 21 '26 12:05 auzhhv

:robot: AI triage completed. Product group, please validate the triage report (Microsoft internal access only, use Ctrl+Click to open in new tab).


Was this triage helpful? React with :thumbsup: or :thumbsdown: on this comment to provide feedback.

JesperSchulz avatar May 21 '26 13:05 JesperSchulz

It is assumed here that after Init() + Validate(Type), the "Page ID" field is still 0, but It is not. Here's why:

Validate(Type, PageTemplate) fires the OnAfterValidateType event on Table 82. The event subscriber in MfgItemJournalMgt.Codeunit.al handles "Prod. Order" and directly assigns "Page ID" := Page::"Production Journal". When Validate("Page ID") is then called without a second parameter, AL validates with the current field value — which is already Page::"Production Journal" (non-zero). The "Page ID" OnValidate trigger in ItemJournalTemplate.Table.al only acts when "Page ID" = 0. The record is inserted with the correct Page ID, matching the filter. On subsequent calls, FindFirst() succeeds and no duplicate insert is attempted.

I would say all good here.

PredragMaricic avatar Jun 20 '26 10:06 PredragMaricic

It is assumed here that after Init() + Validate(Type), the "Page ID" field is still 0, but It is not. Here's why:

Validate(Type, PageTemplate) fires the OnAfterValidateType event on Table 82. The event subscriber in MfgItemJournalMgt.Codeunit.al handles "Prod. Order" and directly assigns "Page ID" := Page::"Production Journal". When Validate("Page ID") is then called without a second parameter, AL validates with the current field value — which is already Page::"Production Journal" (non-zero). The "Page ID" OnValidate trigger in ItemJournalTemplate.Table.al only acts when "Page ID" = 0. The record is inserted with the correct Page ID, matching the filter. On subsequent calls, FindFirst() succeeds and no duplicate insert is attempted.

I would say all good here.

If you use the event to override PageID, it will not be correct. Would it be a problem to change to Validate("Page ID", PageID)? Then it would work for both? Or check whether PageID is not the default ID and when validate with PageID?

auzhhv avatar Jun 22 '26 09:06 auzhhv

Yes, Validate("Page ID", PageID) could work.

PredragMaricic avatar Jul 31 '26 11:07 PredragMaricic

@auzhhv - Could you please proceed with the fix ?

PredragMaricic avatar Oct 05 '26 08:10 PredragMaricic

Håkon Hvitsten (@auzhhv) - Could you please proceed with the fix ?

Sorry for the late reply. I will look at it this weekend.

auzhhv avatar Oct 06 '26 08:10 auzhhv