The Goal of this blog post is to demonstrate the power of workflows and how they can be incorporated into customizations we do going forward. Workflows are more than just replacement for old Approvals. I think of them as wrapper to the new Eventing Model but with extra steps where user can tweak the execution flow according to their specific needs.
Microsoft has included lots of pre-built templates for Workflows. Here I am only interested in custom ones. So, let’s create a scenario we can follow to create our own Workflow.
Scenario: Email Validation. Use an external API and send our Emails there to check If the Server exist. This way we can avoid lots of fake / dummy emails.
Now Email is entered in lots of locations in NAV such as User Card, Customer, Approval Setup etc. We would like our Workflow to work in all those areas with as much shared code as possible.
Secondly, Since Workflows can be customized by the user, lets add an option where the user can choose if they want to either just warn the user when the email is invalid or completely Stop the user from entering an invalid email by throwing an error.
The goal here is to not touch the base NAV code. That means we can’t edit any standard code unit, page or table so the custom code can remain standalone. Luckily using AL we have the ability to extend base pages and tables without doing any change in the base objects.
Finally write the custom code in AL language using Visual Studio code and generate an extension for this functionality.
Create the Events.
- We will follow the pattern laid out by Microsoft for custom events. In a new code unit, create new Functions called ‘ApiCustomerEmailValidationCode’ and ‘ApiUserEmailValidationCode’. Both contain no code except one single line returning the name of the functions in Uppercase as shown below.
- Next, we need to add these events to library. This step basically creates the entry in Workflow Events table so that users can see your events when they try to create a new workflow. Create a new subscriber function. Add the following code. For the example we are creating two events each for Customer Card and User Card email fields.
- Up until this point we added our Event to Library but we haven’t created a trigger through which system knows that our Event has occurred, and it should start the workflow and execute the response.
To do the above, I created two subscriber functions and using the Eventing model (Don’t confuse the Eventing model with Workflow Events) I basically created a hook to the OnBeforeValidate Events of the Emails fields on these tables.
- Now we have created the Events required for this use case. Let’s try them out before we go further.
- Build the extension and open Dynamics NAV (You can also try in the webclient). Search for workflows and open workflow page.
- Click on New to create a new Workflow.
- Enter the details like below.
- In the ‘When Event’ drop down, Select the Event we just created i.e. ‘Customer email field is changed’
- Now you should have something like below.
- In the select response, select ‘Show Message %1’.
- Now Enter a message like shown below and Press OK.
- Select the Enabled field.
- Workflow Created. To check it, go to customer card and try changing the value of email. You will get a Prompt like this.
We have completed the first part of the blog post, we have created a custom event and tested it by triggering a Standard Response. In Next part, we will see how to create our own custom Responses.