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 :
Business Central forum

How can I define a custom function in Subscriber event.?

(0) ShareShare
ReportReport
Posted on by 245

Hello all ,

 How can I define my custom function with parameters in Subscriber Event in Business Central of Cloud version(SaaS).

My function is like below(in Standard Codeunit(5763) NAV2018).

GetResultMessageWeb(VAR MesgP : Text[250])

MessageText := Text003;
IF CounterSourceDocOK > 0 THEN
MessageText := MessageText + Text004;
IF CounterSourceDocOK < CounterSourceDocTotal THEN
MessageText := MessageText + Text005;
//MESSAGE(MessageText,CounterSourceDocOK,CounterSourceDocTotal);
MesgP := STRSUBSTNO(MessageText,CounterSourceDocOK,CounterSourceDocTotal);

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    Use the snippet teventsub and select the object that raises the event you want to subscribe and the event itself.

    The parameters of your method to raise can be inherited from the subscribed event (just press CTRL + SPACE to see the parameters passed by the event).

    For example:

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterCheckSalesDoc', '', false, false)]
    local procedure MyProcedure(var SalesHeader: Record "Sales Header")
    begin
    end;
  • Manikanta_501 Profile Picture
    245 on at

    Thanks for your reply ..

      How can I write my Custom code inside a If and Else Conditions(Standard).Consider Below Standard Code in Business Central.

    Ifandlese.PNG

    I Want to Add my code in END ELSE BEGIN Condition. Can You suggest me the solution. 

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    As said before, in a SaaS environment you cannot write code where you want (you cannot modify existing base code). You need to rethink your code and attach to standard events, for example here to the OnBeforeSalesLineModify event raised by the standard code.

  • Manikanta_501 Profile Picture
    245 on at

    Can we Override the code in Business Central of Cloud Version(SaaS).

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    No. Only subscribe to events raised by the base code.

  • Manikanta_501 Profile Picture
    245 on at

    Actually the standard Code is like below 

    Verride.PNG

    and I wrote Subscriber event for this , Consider below code.

    Verride.PNG

    Here I am using same Code again in my Event ,. Can I Write like this.

  • Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    Yes you can do that. You always need to subscribe to events and here add your code.

  • Manikanta_501 Profile Picture
    245 on at

    Thank you., It's Solved my problem.

  • Manikanta_501 Profile Picture
    245 on at

    I have custom function in Standard Codeunit (Whse.-Post Shipment) and I am using this function from Custom Codeunit in  NAV 2018. Now I am Upgrading this Objects to Business Central of Cloud version(SaaS) . how can I Define my function in Event Subscriber and how can I Call that function from my Custom Codeunit.

    I wrote Custom function in Standard Codeunit(Whse.-Post Shipment) like below.

    3343.Capture_5F00_1.PNG

    And I am Calling that function like below from my codeunit.

     WhsePostShipment.GetResultMessageWeb(MesgL);

     

    Now My question is how can I call this function from my codeunit(or any other object), if I define this under a Subscriber event.

  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    You should have the function defined as a procedure in a custom codeunit and then call that function where you need (for example from your event subscriber and from other codeunits or triggers in your code) by referencing that codeunit: yourCodeunit.YourFunction(....)

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans