Corfu Navision 2016, Development Tips, Functional Tips, How To, Information, Workflow

Workflows in Dynamics NAV 2016

Workflows in Dynamics NAV are represented by workflow events and workflow responses.

The smallest workflow is the pairing of a single event with a single response.

“When something happens, do something” pattern. This is the Event/Response model that makes up the simple but effective design of Workflow for Microsoft Dynamics NAV.

The workflow functionality utilizes several other features as listed below of Dynamics NAV.

Few Listed here below:

Job Queues Setup

E-mail Notification Setup

Work date Setup

User Setup

Approval User Setup

SMTP Mail Setup

In particular, approvals and notifications must be set up so that workflows can send approval requests and process approvals.

Dynamics NAV 2016 introduces two new event concepts. Events and Workflow events. The two are distinct but often coupled together to build solutions.

Dynamics NAV Events allow you to write code which will be called when an event occurs – this is called subscribing to an event.

Workflow Events typically use Platform Events as their trigger, but are richer. Workflow events are registered in the workflow engine and show up in the workflow designer. Microsoft recommends that Workflow events be written at a higher level of abstraction than Platform Events.

Sales Invoice is created or Modified could be example of Event Subscriber, whereas Invoice is Release could be good example for Workflow Event.

Event

For the event we need a new codeunit.

Add helper method for binding our event with the workflow engine.

The method is a simple one which returns and identifying code.

Function Signature:

LOCAL OnMyEventCode() : Code[128]

Function Property:
Workflow-1

Sample Function Code:

EXIT(UPPERCASE(‘OnMyEvent’));

 

Next we add another method which will be called whenever our Event Occurs

Function Signature:

LOCAL [EventSubscriber] OnMyEvent(VAR Rec Object;VAR xRec Object;RunTrigger : Boolean)

Assuming a table is selected on which the Event is triggered.

Function Property & Variables:
Workflow-2

Sample function code:

MESSAGE(‘Event Fired On MyEvent’);

WorkflowManagment.HandleEvent(OnMyEventCode,Rec);

For the code itself, we’ll do two things

  • Show a message box so we can see our event did trigger
  • Call into the workflow engine to have the NAV Workflow engine process any next steps

 

Then we need to add another event subscriber method which is responsible for adding our event to the workflow library. The workflow library is the collection of events which can be seen and managed from the Workflow Setup page.

Function Signature:

LOCAL [EventSubscriber] AddEventToLibrary()

Function Property & Variables:
Workflow-3

Sample function code:

WorkflowEventHandling.AddEventToLibrary(MyEventCode,DATABASE::Object,OnMyFinishTxt,0,FALSE);

Text Constant for Event Description:

OnMyFinishTxt : This will be listed in Workflow Event Window.
Workflow-4

To add a Workflow event we need to do three things:

  • Subscribe to the event.
  • Define a user readable string which describes the event.
  • Call a method in the workflow event handling codeunit, passing in the identifier and the descriptive string.

Save the Codeunit as MyWorkflowEvent with available ID in your Database.

Response

Most of the work is done in the code editor and we will use codeunits for our application objects.

First we’ll make a helper method for binding our response with the workflow engine.

The method is a simple one which returns and identifying code.

Function Signature:

LOCAL RunMyResponseCode() : Code[128]

Function Property:
Workflow-1

Sample Function Code:

EXIT(UPPERCASE(‘RunMyResponse’));

 

Then we need to add another event subscriber method which is responsible for adding our response to the workflow library. The workflow library includes the collection of responses which can be seen and managed from the Workflow Setup page.

Function Signature:

LOCAL [EventSubscriber] AddResponseToLibrary()

Function Property & Variables:

Workflow-5

Sample Function Code:

WorkflowResponseHandling.AddResponseToLibrary(RunMyResponseCode,0,ResponseDescription,’Group 0′);

Text Constant for Response Description:

ResponseDescription : This will be listed in Workflow Responses Window.

Workflow-6

To add a Workflow response we need to do three things:

  • Subscribe to the event which adds new responses to the workflow library
  • Define a user readable string which describes the response
  • Call a method in the workflow response handling codeunit, passing in the identifier and the descriptive string.

The earlier functions were used to add the response to the workflow library.

But we need another method that actually listens to when the response needs to be called.

Function Signature:

LOCAL [EventSubscriber] RunReportResponse(VAR ResponseExecuted : Boolean;Variant : Variant;xVariant : Variant;ResponseWorkflowStepInstance : Record “Workflow Step Instance”)

Function Property & Variables:

Workflow-7

Sample Function Code:

IF WorkflowResponse.GET(ResponseWorkflowStepInstance.”Function Name”) THEN

CASE WorkflowResponse.”Function Name” OF

RunMyResponseCode:

BEGIN

RunMyFunction;

ResponseExecuted := TRUE;

END;

END;

When this code is called it needs to do two things.

Firstly it needs to evaluate if the response should be called (and it does this by checking the code value) and secondly it should return TRUE if it handles the response.

Finally we will add our Response Handling Function RunMyFunction, which will execute actual action which we want Response of this call.

Enable Workflow in System

First we will create our Category so that our Workflow can be identified uniquely.

Workflow-8

Next we will configure our Workflow.

Workflow-9

Here my workflow is listed once configured and saved.

Workflow-10

Now we are good in position to go for testing of our workflow.

 

This was the simplest scenario with bare minimum action required of implementing workflow.

You can use existing Template and define or you can implement your own as described above.

 

I will come up with more details on this topic later in my upcoming posts.

Corfu Navision 2016, Deferral, Functional Tips, How To

Deferral Functionality in Navision 2016

Microsoft Dynamics NAV 2016 offers the ability to automatically defer revenues and expenses over a predefined schedule, enabling companies to easily recognize revenues and expenses in periods other than the period in which the transaction is posted.

The deferral functionality provides several user benefits, including:

  1. Enabling additional financial functionality in Microsoft Dynamics NAV.
  2. Greatly reducing the time and effort required to defer revenues and expenses.
  3. Enabling reporting on deferred amounts for customer, vendor, and account ledgers.
  4. The deferral functionality is available on purchasing and sales documents, as well as general journals.

The first step in setting up deferral functionality is to create a deferral template.

Deferral templates allow us to define settings that will create a default deferral schedule for a document.

The default schedule is built for the document based on the settings defined in the template.

From the Search box find Deferral Template and use related Link to open the Template Card.
Deferral-1

Define the Template values as per the requirement for Expense/Revenue.

Period Description: uses %1..%6 to replace values as (1)Day, (2)Week, (3)Month, (4)Month Text, (5)Accounting Period Name, (6)Year

Users have the option to select a default deferral template for Resources, Items, and Accounts.

When the Resource, Item, or Account is selected on a document for which deferral functionality is available, the default deferral template will automatically be selected for the line.

Fill the Default Deferral Template on Invoicing FastTab for Item & Resource and for Accounts on Posting FastTab.
Deferral-2

Deferral-3

We have the option to view or modify the default deferral schedule created from the template in the Deferral Schedule page.

The user can either change the settings and re-calculate the schedule with new settings, or simply modify the lines of the schedule directly.

Schedule window will be as below:
Deferral-4

Reports:

The Sales Deferral Summary report provides a summary, for each customer, of deferred revenue. The amounts on the report are calculated as of the date entered for the report.

The Purchasing Deferral Summary report provides a summary, for each vendor, of deferred expenses. The amounts on the report are calculated as of the date entered for the report.

The G/L Deferral Summary report provides a summary, for each G/L account, of deferred expenses. The amounts on the report are calculated as of the date entered for the report.

 

I will come up with more details on this in my upcoming posts.

Corfu Navision 2016, Development Tips, Events, How To

Implementing Events in Navision 2016

Recall from my earlier post for description of Events – Events in C/AL Navision 2016

Here in this post we will see how to implement the same.

This could be helpful in situation where throughout the system you wish to have similar behaviour on specific condition. In general we will have to write code in every object with same set of logic, variables, and functions and so on.

Although still in this case too we will require to add code to every object, but only one time.

Think of you need to enhance or make changes in behaviour again you will have to go through each objects and do the required change. But using this feature we can keep it centralized and manage from single place, without going through each objects again.

Here below I am taking example one explained by Microsoft help basic concept, will come up with my version with some more effective usage later sometime.

When users change the address of a customer, you want to check that the address does not include invalid characters, which in this walkthrough is a plus sign (+).

To accomplish this, you will publish an event that is raised when the Address field on page 21 Customer Card is changed.

To handle the event when it is raised, you will add an event subscriber function that includes logic that checks the address value and returns a message to the user if it contains a plus sign.

Publishing the Event

To publish an event, you create a C/AL function that is set up to be an event publisher. An event publisher function can be added in any object, such as a codeunit, page, or table.

In this procedure we will add the event publisher function to a new codeunit, in which you can potentially add more event publisher functions for other events later.

Because you might want to change this event implementation in the future, you decide to create an integration event type.

The event publisher requires a single text parameter for handling the address of the customer.

Create a new codeunit

The codeunit for the event publisher I have created 50000 – Event Publisher.

Now we will add the event publisher function to publish the event.
Events-1

To create the event publisher function to publisher the event

  • Define a function OnAddressLineChanged.
  • Open the properties for the OnAddressLineChanged function, select the function, and then in the View menu, choose Properties. Set the properties as follows:
  • Set the Local property to No.

Setting this property makes the function available to be called from the other objects.

  • Set the Event property to Publisher. This makes the function an event publisher.
  • Set the EventType property to Integration.
  • Close the Properties

Events-2

  • Add a local parameter to the function for the address of the customer as described in the following steps:
  • On the Functions tab, select the OnAddressLineChanged function, and then choose the Locals

The C/AL Locals window opens.

  • On the Parameters tab, in the Name field, enter line.
  • Set the DataType field to Text.
  • Set the Length field to 100.

Events-3

Important

An event publisher function cannot have a return value, variables, or text constants; otherwise you will not be able to compile the function.

The new function appears in the C/AL Editor with the following signature:

[IntegrationEvent] OnAddressLineChanged(line : Text[100])
Events-4

You can now raise the event in the application.

Raising the Event

After creating the event publisher function to publish the event, now we will add code to the application to raise the event where it is required.

In this case, the event will raise when the Address field is changed on the page 21 Customer Card.

Therefore, we will add code to the Address – OnValidate() trigger in C/AL code of the page. Raising an event basically involves calling the event publisher function that publishes the event.

To raise the event

  • In the development environment, open page 21 Customer Card as follows:
  • Add a C/AL variable that specifies the object that publishes the event. In this case, the event publisher object is codeunit 50000 Event Publisher, which contains the event publisher function OnAddressLineChanged that you created in the previous procedure.

Events-5

  • In C/AL code, add the following code on the Address – OnValidate() trigger to raise the event:

Publisher.OnAddressLineChanged(Address);

This calls the event publisher function to raise the event.
Events-6

  • Save and compile the changes to the page.

The event can now be subscribed to and handled.

Subscribing to and Handling an Event

Once an event has been published you can add code to the application that subscribes to and handles the event when it is raised.

For example, in this case when a user changes the address of a customer (the event), you want code that checks that the value does not contain a plus sign.

Subscribing to and handling an event is accomplished by creating a C/AL function that is set up as an event subscriber and subscribes to a specific event (defined by an event publisher function). The event subscription function contains the application logic for handling the raised event.

In this case, we will create an event subscriber function that subscribes to the OnAddressLineChanged function in codeunit 50000 Event Publisher.

Unlike an event publisher function, an event subscriber function can only reside in a codeunit object. This procedure will add the event subscriber function to a new codeunit, in which you can potentially add more event subscriber functions for other events later.

To create a new codeunit

  • In the development environment, create a new codeunit that has the ID 50001 and the name Event Subscriber.

To create the event subscriber function to subscribe to and handle the event

  • Create Function CheckAddressLine.

Events-7

  • Choose Properties. Set the properties as follows:
  • Set the Event property to Subscriber to make the function an event subscriber.
  • Set the EventPublisherObject property to Codeunit My Publishers.

This is the codeunit that contains the event publisher function (OnAddressLineChanged) that you want to subscribe to.

  • In the EventFunction property, select the OnAddressLineChanged integration event.

This field reads all the published events in the event publisher object.
Events-8

Note

When you get a message that asks whether you want to overwrite the edited function’s signature, choose Yes to continue.

  • A local parameter that has the name line and the data type Text has been automatically added to the new CheckAddressLine
  • The new function appears in the C/AL Editor with the following signature:

LOCAL [EventSubscriber] CheckAddressLine(line : Text[100])

You can now add code to handle the event.

  • To handle the event, add the following code to the CheckAddressLine function in the C/AL editor:

IF (STRPOS(line, ‘+’) > 0) THEN BEGIN

ERROR(‘Cannot use a plus sign (+) in the address [‘ + line + ‘]’);

END

This code checks the value of the Address field on page 21 Customer Card when is has been changed and returns a message if the value contains a plus sign.
Events-9

Viewing the New Event Subscription

After you create an event subscriber, you can view information about it in page 9510 Event Subscriptions. This page provides information about all the current event subscriptions in the application. You can open this page directly from the development environment or from a Microsoft Dynamics NAV client.

To view the event subscription from the development environment

  • On the Tools menu, choose Debugger, and then choose Event Subscriptions.

Events-10

To view the event subscription from a Microsoft Dynamics NAV client

  • Start the Microsoft Dynamics NAV client.
  • In the Search box, enter Sessions, and then choose the related link.

Events-11

The Even Subscription Window will look like:
Events-12

Testing the Event

To test the event implementation, you can run page 21 Customer Card from the development environment.

  • In the Address field, add a plus sign, and then choose the OK

The following message appears:

Cannot use a plus sign (+) in the address [].

[] contains the value of the Address field.
Events-13

I will come up with more details on this topic later in my future posts.

Corfu Navision 2016, Development Tips

Using a Timestamp Field in Navision 2016

Each table in Microsoft Dynamics NAV includes a hidden timestamp field.

The timestamp field contains row version numbers for records as maintained in SQL Server.

You can expose the timestamp field in a table, and then write code against it, add filters, and so on, similar to any other field in a table.

However, you cannot write to the timestamp field.

A typical use of the timestamp field is for synchronizing data changes in tables, when you want to identify records that have changed since the last synchronization.

For example, you can read all the records in a table, and then store the highest timestamp value. Later, you can query and retrieve records that have a higher timestamp value than the stored value.

To set up a timestamp field in a table

  1. In the development environment, open the table, and then add a field of the data type BigInteger.

Specify a name for the field, such as timestamp.

You can specify any valid name for field; you are not restricted to use timestamp.

  1. Open the field properties, and then set the SQL Timestamp property to Yes.

For demo purpose I am adding this field to table 330 – Currency Exchange Rate
Timestamp-1

Now when I run the table in RTC I see value automatically populated in this field.
Timestamp-2

I will come up with more details in my future posts.

Corfu Navision 2016, Events, Information

Events in C/AL Navision 2016

By implementing events in C/AL code, you can design applications to react to specific actions or behaviour that occur.

Events enable you to separate customized functionality from the application business logic.

By using events in the application where customizations are typically made, you can lower the cost of code modifications and upgrades to the original application.

Events can be used for different purposes, such as generating notifications when certain behaviour occurs or the state of an entity changes, distributing information, and integrating with external systems and applications.

How Events Work

There are three major participants involved in events: the event, a publisher and a subscriber.

An event is the declaration of the occurrence or change in the application. An event is declared by a C/AL function, which is referred to as an event publisher function. An event publisher function is comprised of a signature only and does not execute any code.

A publisher is the object that contains event publisher function that declares the event. The publisher exposes an event in the application to subscribers, essentially providing them with a hook-up point in the application. An event is raised by adding logic to the application that calls into the publisher to invoke the event (the event publisher function). There are three different event types: business, integration, and trigger events.

Business and integration type events must be explicitly declared and published, which means that you must create event publisher functions and add them to objects manually. On the other hand, trigger events, which occur on table and page operations, are published and raised implicitly by the Microsoft Dynamics NAV runtime. Therefore, no coding is required to publish them.

A subscriber listens for and handles a published event. A subscriber is a C/AL function that subscribes to a specific event publisher function and includes the logic for handling the event. When an event is raised, the subscriber function is a called and it code is run.

How to Implement Events

Implementing events in Microsoft Dynamics NAV consists of the following tasks:

  1. Publish the event. For business and integration events, create and configure a function in an application object to be an event publisher function.
  2. Raise the event. Add code that calls the event publisher function..
  3. Subscribe to the event. At the consumer end, add one or more subscriber functions that subscribe to published events when they are raised.

Will come up with how to practically use it in my upcoming posts.

Corfu Navision 2016, Development Tips, FILTERPAGEBUILDER, How To

Filter Pages for Filtering Tables using FILTERPAGEBUILDER

In C/AL code, you can use the FILTERPAGEBUILDER data type to create a filter page that enables users to set filters on multiple tables. Filter pages contains one or more filter controls, where each filter control can be used to set filters on a specific table. In the Microsoft Dynamics NAV client, filter pages are generated at runtime and run in a modal dialog box.

For demo purpose I have created a List page on Item Table. I have added below Variables and Code on Page.
filterpagebuiler-1
When I Run the Page, First Request page is popup to accept filters. Then my Item List Page is opened applying the filter I provided on the Request Page.
filterpagebuiler-2
FILTERPAGEBUILER Data Type Functions

ADDTABLE Function

Adds a filter control for a table to a filter page.

ADDRECORD Function

Adds a filter control for a table to a filter page as specified by a record data type variable.

ADDRECORDREF Function

Adds filter control for a table to a filter page as specified by a recordref variable.

ADDFIELD Function

Adds a table field to the filter control for a table on filter page.

ADDFIELDNO Function

Adds a table field to the filter control for a table as specified by the field number.

GETVIEW function (FilterPageBuilder)

Gets the filter view (which defines the sort order, key, and filters) for the record in the specified filter control of a filter page.

SETVIEW Function

Sets the current filter view, which defines the sort order, key, and filters, for a record in a filter control on a filter page.

RUNMODAL Function (FilterPageBuilder)

Builds and runs the filter page.

COUNT Function (FilterPageBuilder)

Gets the number of filter controls that are specified in the FilterPageBuilder object instance.

NAME Function (FilterPageBuilder)

Gets the name of a table filter control that is included on a filter page based on an index number that is assigned to the filter control.

Corfu Navision 2016, How To, Information, Posting Preview

Posting Preview in Navision 2016

On every document and journal that can be posted, you can choose the Preview Posting button to review the different types of entries that will be created when you post the document or journal.

Below I have shown Example from Sales Invoice Window, similarly you will get in other related Pages too.
PreviewPosting-1

From Actions TAB you will get this option for Preview Posting.

You will get below Navigation Window where you can Navigate Entries which will get posted upon Posting.
PreviewPosting-2
Give it a try, very useful feature introduced in Navision 2016.

Corfu Navision 2016, How To, Information, Permission, User Group

Record Permissions and Apply Permissions Sets to User Groups in Navision 2016

Administrators can record new permission sets in Microsoft Dynamics NAV 2016.

Recording permissions is based on the code coverage functionality.

You can access the various windows and activities in the Microsoft Dynamics NAV Windows client or the Microsoft Dynamics NAV Web client that you want users with this permission set to access.

You must carry out the tasks that you want to record permissions for. Then, you can apply the new permission set to a group of users.

A user group is a combination of roles and users. If you want to change permissions for the user group, the changes are automatically applied to the users who are members of the group.

To help you manage permissions in Microsoft Dynamics NAV for your company, you can set up one or more user groups, add permissions sets to the groups, and then add users to the groups. You can add more than one permission set to a user group.

In the User Groups window, you can set up user groups, add and remove permissions, and you can apply changes to all or specific user groups.
Permission-1

Permission-2

Permission-3

Permission-4

Permission-5
If the default permission sets that are provided with Microsoft Dynamics NAV are not sufficient or not appropriate for your organization, then you can create new permission sets. If the individual object permissions that define a permission set are not adequate, then you can modify a permission set.

You can create a permission set manually, or you can record permissions by navigating in the application.
Permission-6

Permission-7

Permission-8
This starts a recording process that is based on the code coverage functionality in Microsoft Dynamics NAV. You can now access the various windows and activities in the Microsoft Dynamics NAV Windows client or the Microsoft Dynamics NAV Web client that you want users with this permission set to access. You must carry out the tasks that you want to record permissions for.

When you want to finish the recording, return to the Permissions window, and then, on the Actions tab, choose Stop.

Choose Yes to add the recorded permissions to the new permission set, or choose No to cancel.

If you choose Yes, the objects that you accessed are added to the window. In Microsoft Dynamics NAV 2016, only the objects are recorded, so you must specify if users must be able to insert, modify, or delete records in the recorded tables.

C/AL Editor, Corfu Navision 2016, Information

Redesigned C/AL Editor in Navision 2016

The C/AL Editor in the Microsoft Dynamics NAV Development Environment has been redesigned to give you more coding capabilities. Coding in the new C/AL editor is like before except you benefit from new features such as IntelliSense, name completion, change tracking, improved syntax highlighting and colorization. The new design has a look-and-feel that resembles the Debugger regarding breakpoints.

Name Completion:  Editor-1

Now Editor suggest the Variable Names which are available.

IntelliSense:  Editor-2
Now Editor Suggest available options for the variables.
Editor-3

Change Tracking:  Editor-4

Improved syntax highlighting and colorization:
Editor-5
If you still want to use old version editor, you can use the old version of the C/AL Editor by running the Microsoft Dynamics NAV Development Environment from a command prompt and setting the useoldeditor parameter.
Editor-6

finsql.exe useoldeditor=yes

useoldeditor Specifies whether to use the C/AL Editor that was available in Microsoft Dynamics NAV 2015 and earlier versions. The C/AL Editor was redesigned in Microsoft Dynamics NAV 2016.

To use the old editor, specify the parameter as useoldeditor=yes or useoldeditor.

To use the new editor, omit the parameter or specify it as useoldeditor=no.

Corfu Navision 2016, Information

Changes in C/AL Behaviour C/AL Statements in Nav 2016

The following table lists the summary of new, removed, or changed C/AL statements in Microsoft Dynamics NAV 2016.

Will come with more details in my later posts.

Statement Description
FOREACH New statement.

Iterate through a .NET Framework collection or array.

BREAK New statement.

Terminates the iteration statement in which it used.

Important

If your solution has used FOREACH or BREAK as names of variables in earlier versions of Microsoft Dynamics NAV, you must change the names before you upgrade to Microsoft Dynamics NAV 2016.

Alternatively, you can enclose the variable names in quotation marks. If you do not, and you import an object that has this code in text format, you cannot compile the object.