web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

SMS integration logic to trigger after posting via the Post button

(6) ShareShare
ReportReport
Posted on by 35
I added SMS integration logic to trigger after posting via the Post button, and it works correctly — the browser opens and the SMS is sent to the customer.
However, I noticed that the SMS is also being sent when using Posting Preview, which should not happen. Can anyone advise why the Preview Posting process is triggering the same SMS logic? I urgently need the correct approach to prevent SMS from sending during Preview Posting.
The code unit reference is attached below. Your quick guidance would be greatly appreciated.
 
codeunit 50331 "Sales Order Post SMS Auto"
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)]
    local procedure OnAfterPostSalesDoc(
        SalesHeader: Record "Sales Header";
        GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line";
        SalesShptHdrNo: Code[20];
        SalesInvHdrNo: Code[20])
    var
        SMSCodeunit: Codeunit "Sales Order SMS Browser";
        Customer: Record Customer;
        PostedSalesInvHdr: Record "Sales Invoice Header";
    begin
        // ✅ Send only for invoices
        if SalesInvHdrNo = '' then
            exit;
        if PostedSalesInvHdr.Get(SalesInvHdrNo) then begin
            PostedSalesInvHdr.CalcFields("Amount Including VAT"); // ✅ ensure amount is loaded
            if Customer.Get(PostedSalesInvHdr."Sell-to Customer No.") then begin
                Customer.CalcFields(Balance, "Balance (LCY)"); // ✅ ensure balance is accurate
                if Customer."Phone No." <> '' then
                    SMSCodeunit.SendSMSViaBrowser(
                        PostedSalesInvHdr."Sell-to Customer Name",
                        PostedSalesInvHdr."No.", // ✅ Posted Invoice No.
                        PostedSalesInvHdr."Amount Including VAT", // ✅ Actual Invoice Amount
                        Customer."Balance (LCY)", // ✅ Up-to-date Remaining Balance
                        Customer."Phone No.")
                else
                    Message('⚠️ No phone number found for customer %1', Customer."No.");
            end else
                Message('⚠️ Customer not found.');
        end;
    end;
}
I have the same question (0)
  • Verified answer
    Pallavi Phade Profile Picture
    5,420 Super User 2026 Season 1 on at
    Namaste  @Mian Shabir 7
     
    If you want to skip preview posting you have to set the event in below way 
     
     
    Please put the condition Preview Mode - False in your code and rest should work . please check in sandbox
     
    Regards
    Pallavi Phade
  • Verified answer
    Aman Kakkar Profile Picture
    2,977 Super User 2026 Season 1 on at
    Hi,
     
    The reason your SMS code is running during Preview Posting is because you subscribed to an event that is also triggered in preview mode.

    The event OnAfterPostSalesDoc has a PreviewMode parameter, but you did not include it in your subscriber signature, so BC calls your subscriber even during preview.
     
    To fix it, you must ensure that your procedure must match the full signature of the event, and then you can use the PreviewMode parameter to conditionally exit when the call is from Preview Posting -
     
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)]
    local procedure OnAfterPostSalesDoc(
        var SalesHeader: Record "Sales Header";
        var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line";
        SalesShptHdrNo: Code[20];
        RetRcpHdrNo: Code[20];
        SalesInvHdrNo: Code[20];
        SalesCrMemoHdrNo: Code[20];
        CommitIsSuppressed: Boolean;
        InvtPickPutaway: Boolean;
        var CustLedgerEntry: Record "Cust. Ledger Entry";
        WhseShip: Boolean;
        WhseReceiv: Boolean;
        PreviewMode: Boolean)
    begin
        
        if PreviewMode then
            exit;
    
        // … your SMS logic …
    end;
    
     
    Do mark as verified if this helps.
    Aman K
  • Verified answer
    OussamaSabbouh Profile Picture
    12,808 Super User 2026 Season 1 on at
    Hello,
     
    Posting Preview runs the same posting codeunit, so your subscriber fires there too.
    To stop SMS during preview, just add:
     
    if not GuiAllowed then
        exit;
     
    (or if SalesHeader.IsTemporary() then exit;)
     
    Real posting → SMS
    Preview posting → skipped.
     
    Regards,
    Oussama Sabbouh
  • Verified answer
    Gerardo Rentería García Profile Picture
    25,555 Most Valuable Professional on at

    Hi, good day
    I hope this can help you, and give you some hints

    #msdyn365bc - Customize Preview Posting.

    Best Regards
    Gerardo

  • Verified answer
    YUN ZHU Profile Picture
    99,055 Super User 2026 Season 1 on at
    Hi, As other experts have mentioned, the event you subscribed to includes the PreviewMode parameter, which you can use directly to skip.
     
    Thanks
    ZHU

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,926 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,158 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 533 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans