AL Dynamics 365 Dynamics NAV 2017 Dynamics NAV 2018 Extensions

Custom Workflows in AL – Part 2 Response

In the previous blog, we talked about how we can raise a custom Workflow Event in NAV using AL. Let’s see how to create custom Reponses for Workflows in the new AL environment.

Steps:

  1. Create a new code unit. (in VSCode with Al Extension enabled, typing tcodeunit + ENTER key trigger a code snippet and creates a basic layout of a code unit)
  2. Create a new Procedure ‘ApiEmailValidationResponseCode’ with return type code.

 

  1. Create a new Event subscriber for Codeunit Workflow Response handling , Event OnAddworkflowReponsetoLibrary

Declare a variable of Workflow Response Handling codeunit in the procedure and call the    function AddResponsetoLibrary as shown above.

For the Response option group Parameter, pass Group 50100. Response option group lets you add some user input that the user can choose when they select our Response. To do this follow the steps below.

  1. We need to add a new custom field in a standard table “Workflow Step Argument”

Since we are not touching base code, We can meet this requirement using the new type of objects introduced called TableExtension and PageExtension.

Create a new object of type tableextension as shown below. The snippet to quickly create the layout is ‘ttableext’.

  1. Similarly create a new page extension. This will allow you to add the newly created field from the table extension to the base page “Workflow Response Options”.

This field also needs a Visible=”Response Option Group” = ‘GROUP 50100’; This property will make sure that our custom Response options will only be visible when the user selects our response from the available list.

  1. Return to the Response Codeunit that we created in the beginning of this post.
  2. Create a new subscriber of codeunit “Workflow Response Handling”, Event ‘OnExecuteWorkflowResponse’.

As you can see, This pattern will remain the same for all your responses. The only change would be the Case ‘ApiEmailValidationResponseCode’. Everytime systems detects that your response is being triggered , it will run the function ‘ActualApiEmailValidationWFResponse’ function and pass the parameters to it.

  1. Create a new procedure called ‘ActualApiEmailValidationWFResponse’. This is the one where you write your actual logic that you want to run every time your response is executed.

This function basically checks the Variant Parameter and detects if the Variant is a customer RecRef or User RecRef. Depending on the what the Variant is , It will trigger the email validation api which I have created in a separate codeunit.

  1. Create a new codeunit. Also create a new procedure within that called InitializeApi.

Declare the variables as shown below. AL language now include native httpclient support and we do not need to declare dotnet variable anymore. It also comes packed with native Json support to read and write data to JSON.

I am using a free api to validate emails from https://mailboxlayer.com/. It supports receiving the email to check in the query of the URL. Please replace <youraccesskey> with what your own key which you will receive after registering for the free Api.

The Api returns the result in JSON format. We are only interested in the smtp_check key since that is the one which checks if the Mail server belonging to the user provided email address actually exist on internet.

 

Sample JSON Result:

  1. {
    
     "email": "support@apilayer.com",
    
     "did_you_mean": "",
    
     "format_valid": true,
    
     "mx_found": true,
    
     "smtp_check": true,
    
     "catch_all": false,
    
     "role": true,
    
     "disposable": false,
    
     "free": false,
    
     "score": 0.8
    
    }

    And finally lets create the ShowResult Procedure which shows the output to the user based on the ValidationOption that user selects in the Response.

So, if in the ResponseOption, the Workflow creator selects ‘Error’ then user will be shown an error and won’t be allowed to enter a invalid email. But if the user selects warning then only a warning notification is shown.

Press Ctrl+f5 to package all code in an extension and deploy to Application Server.

Let’s see how it all works together with the custom Events we created in Part 1.

Demo:

Create a new workflow as shown below. For the Event select the custom event we created in part 1 ’User Card Email field is changed’.

On the Response side , Select the custom response we created(‘Check if the format is valid and server actually exist’) and select the EVW_ValidationOption as ‘Error’ in the drop down.

 

Press Ok and Click on the Enabled check box to enable the workflow. Workflow is created.

Now Open User card and try to enter an invalid email address. You will receive an Error as shown below.

In Case the email is fine User receives a success notification.

 

That’s it, we have created a custom event and custom response with user selectable response options in AL and created an Extension for the customization.

Link to Part 1

Download code GoogleDrive

Leave a Reply

Your email address will not be published. Required fields are marked *