Skip to main content

Notifications

Dynamics 365 Community / Blogs / Navision 360 / Using Globals in events- Mi...

Using Globals in events- Microsoft Dynamics NAV 2017

Zaid Tariq Profile Picture Zaid Tariq 2,266

To use the global variable in the event subscriber method, declared in the event publisher codeunit, you have to perform following steps:

  1. Go the the property of the publisher event of type integration(only).
  2. In the property windows, change the property GlobalVarAccess to Yes.
  3. Go to the subscriber event method and create a parameter with the exact same name and type as that of the global variable declared in the event publisher code unit. You need to add manually in the parameter list each variable for each global variable you wish to access. If you set the parameter as Var in the event subscriber method then you change the value of global variable declared in publisher codeunit.
    Again, you need to note that if the publisher object removes a global var you access, or changes it’s name, you will not get a compile error, but your subscriber will not get executed.

Demo

Scenario 1: Create a Global variable in custom codeunit which is publishing and raising an event, get the value of this global variable in the subscriber method. Got a fair idea? So try it first by yourself and then come back to see how I performed it.

Steps:

  1. Create a new codeunit. Also create, publish and raise an event.
    1_2D00_create_2D00_a_2D00_codeunit_2D00_and_2D00_an_2D00_event.jpg
  2. Create a new global variable in this codeunit.
    2_2D00_create_2D00_a_2D00_new_2D00_global_2D00_variable.jpg
  3. Write the business logic.
    3_2D00_Write_2D00_the_2D00_logic.jpg
  4. Create a new codeunit and a susbcriber method(you may create the subscriber method in the same codeunit).
    4_2D00_Create_2D00_a_2D00_new_2D00_CU_2D00_and_2D00_event_2D00_subscriber.jpg
  5. Define a parameter MyGlobal of type Text  in the subscriber method.
    5_2D00_create_2D00_a_2D00_global_2D00_variable.jpg
  6. Write the business logic in the subscriber method.
    6_2D00_Write_2D00_business_2D00_logic.jpg
  7. Run the codeunit(CU50001) raising the event.
    7_2D00_run_2D00_CU.jpg
  8. See the output in RTC.
    8_2D00_result_2D00_of_2D00_CU.jpg

Try By Yourself: Create a Global variable in custom codeunit which is publishing and raising two events, get the value of this global variable in the 1st event subscriber method and change the value of that global variable. Now get the new value in the second event subscriber method and display in MESSAGE(). Got a fair idea? So try it first by yourself don’t forget to share your success with me!  

Reference: 
http://navision360.azurewebsites.net/2017/02/using-globals-in-events-microsoft-dynamics-nav-2017/

Comments

*This post is locked for comments

  • Zaid Tariq Profile Picture Zaid Tariq 2,266
    Posted at

    Thanks @Jalmaraz

  • Jalmaraz Profile Picture Jalmaraz 667
    Posted at

    I like this post. As you told this possibility is both, good and dangerous, if global is deleted you could have a problem. Great post thank you.