Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

Notification Issue on Posted Sales Invoice Page Based on Open Invoices in AL Extension

(1) ShareShare
ReportReport
Posted on by 2

Hello everyone,

I'm currently working on an AL extension for Business Central where I need to trigger a notification on the "Posted Sales Invoice" page when the invoice is not closed (i.e., when the invoice is still open). Here is the code I've written:

al
pageextension 50101 InvoiceStatusNotification extends "Posted Sales Invoice"
{
    trigger OnOpenPage()
    var
        PostedSalesInv: Record "Sales Invoice Header";
        InvoiceStatusNotification: Notification;
        Text001: Label 'This invoice is still open and unpaid.';
        Text002: Label 'Trigger is firing correctly';
        OpenInvoice: Text;
    begin
        // Check if the record is found
        if PostedSalesInv.Get(Rec."No.") then begin
            // Debug message to check the value of the Closed field
            Message('Closed Field Value: %1', PostedSalesInv."Closed");

            if PostedSalesInv.Closed = false then begin
                // Create the notification
                InvoiceStatusNotification.Message(Text001);
                InvoiceStatusNotification.Scope := NotificationScope::LocalScope;
                // Add a data property for the invoice number
                InvoiceStatusNotification.SetData('InvNumber', PostedSalesInv."No.");
                // Add an action that calls the ActionHandler codeunit
                InvoiceStatusNotification.AddAction(OpenInvoice, Codeunit::"InvoiceActionHandler", 'OpenInvoice');
                // Send the notification to the client
                InvoiceStatusNotification.Send();
                // Debug message to confirm notification is sent
                Message('Notification Sent');
            end;
        end else begin
            // Debug message if record is not found
            Message('Record not found for Invoice No.: %1', Rec."No.");
        end;
    end;
}

Despite my efforts, the notification appears even on closed invoices. The goal is for the notification to appear only when the invoice is open. Here are some details:

  1. The Closed field value always shows false even when the invoice is actually closed.

  2. My goal is to send a notification when the invoice is open (i.e., not closed).

I've tried using the OnAfterGetRecord trigger and am currently using the OnOpenPage trigger, but neither seems to work consistently. Any insights or suggestions on why the notification might not be triggering correctly and how to resolve this issue would be greatly appreciated.

Thanks in advance for your help!

  • Suggested answer
    Bilal Haider Profile Picture
    Bilal Haider 41 on at
    Notification Issue on Posted Sales Invoice Page Based on Open Invoices in AL Extension

    Your code looks fine, the field Closed is a flow field. Please add PostedSalesInv.CalcFields(Closed) after you get PostedSalsInv, then it will work.
  • Suggested answer
    Inkey Solutions Profile Picture
    Inkey Solutions 1,829 on at
    Notification Issue on Posted Sales Invoice Page Based on Open Invoices in AL Extension
    Message('Closed Field Value: %1', PostedSalesInv."Closed")

    In the "PostedSalesInv.Closed" field, it always takes the value "No" even when the current record's "Closed" value is "Yes" in Business Central. As a result, the notification is triggered every time.

    You can use the condition PostedSalesInv.Closed = Rec.Closed for comparison instead of PostedSalesInv.Closed = false to handle the current record properly.

    Other solution:

    Refer to the current record instead of the variable.

    pageextension 50101 InvoiceStatusNotification extends "Posted Sales Invoice"
    {
        trigger OnOpenPage()
        var
            InvoiceStatusNotification: Notification;
            Text001: Label 'This invoice is still open and unpaid.';
            Text002: Label 'Trigger is firing correctly';
            OpenInvoice: Text;
        begin
            Message('Closed Field Value: %1', Rec."Closed");
            if Rec.Closed = False then begin
                // Create the notification
                InvoiceStatusNotification.Message(Text001);
                InvoiceStatusNotification.Scope := NotificationScope::LocalScope;
                // Add a data property for the invoice number
                InvoiceStatusNotification.SetData('InvNumber', Rec."No.");
                // Add an action that calls the ActionHandler codeunit
                InvoiceStatusNotification.AddAction(OpenInvoice, Codeunit::"InvoiceActionHandler", 'OpenInvoice');
                // Send the notification to the client
                InvoiceStatusNotification.Send();
                // Debug message to confirm notification is sent
                Message('Notification Sent');
            end;
        end;
    }

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,759 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans