Nav 2016 Events and Workflow Walk Through – 8

This is the eight and last of a multi part series on Nav 2016 events and workflows. We will take everything we learned so far and create a full new workflow.

First part : What are events and the advantages they provide.

Second part: What are the builtin events and how to subscribe to events.

Third Part: Creating event subscribers

Fourth Part : What are workflows and how to make a workflow step

Fifth part: Making a workflow response

Sixth Part: Assigning workflow step and response combinations

Seventh Part: Assigning workflow step predecessors

Eighth Part: Putting it all together

All Together Now

What have we learned so far? What are events and why they are useful. How to subscribe to an event. How to create our own events. How to make a workflow step. How to make a workflow response. How to assign a step/response combination. Lastly, how to assign workflow step predecessors.

Now lets put it all together and create a workflow from start to finish. This will fill in all the little details we skipped over when we were making all the little parts.

Our workflow will set the Assigned User ID when ever some one manually releases a purchase header.  You could do this change in the base object very easily. But the whole point of events and workflows is to minimize the amount of base object changes. We are only going to add in a new event and one line of code inside base objects. This will also allow for easy future changes because the event is already there and can be used in other ways.

From the top

The first thing we need to do is figure out all our workflow steps and responses that we will need to make. Using a simple table in excel with 3 columns works quite well.  Our example workflow is quite simple, but doing this can really help out when you have many steps and responses to consider.

table

Workflow Step

First lets setup the workflow step. In codeunit 415 Release Purchse Document we need to add a new event at the start of the PerformManualRelease function. To do this lets make a new function in the codeunit 415 called OnBeforeManualReleaseDoc. We need to add one parameter, the purchase header we are working with. Then lets set this function up as the publisher with the following properties.

publisher

Next we need to set up when this event will happen. Go to the start of the PerformManualRelease function inside codeunit 415 and fire the event.

publisher2

The Release Purchase Document codeunit should only have the two changes above in it. As you can see we want to minimize any change to the base objects. All other changes we make from now on will be in a completely separate codeunit.

In a new codeunit create a new function called BeforePurchaseManualReleaseDocCode. This function is simply to return a text variable so it can be accessible outside this codeunit. This function should return something along the lines of ‘ONBEFOREPURCASEMANUALRELEASE’. Next create a function called BeforePurchaseManualReleaseDoc. This function will be our event subscriber and workflow step. The properties of the function are as follows:

sub1

This function will also require a Workflow Management variable. The code for this is simply:

This function activates our workflow step and will cause the responses to happen.

Next we need to add our workflow step to the list of possible steps. Create a new function called AddWorkflowSteps. This new function will subscribe to OnAddWorkflowEventsToLibrary from the Workflow Event Handling codeunit. We will also need a Workflow Event Handling codeunit variable.

Our function should look something like this:

addsteps

Workflow Response

With the step out of the way we can now add our workflow response.

First we need our function that returns the response code. Create a new function that like the following:

responsecode

Then we need a function that will execute our response. This function needs to take in the Purchase Header as well as the Workflow Step Instance. We also need to add in a Workflow Step Argument Variable. Our function will look something like:

response

Now that we have our response we need to add it to the library.

response2

Next we need to add a function to run our response. This function will subscribe to OnExecuteWorkflowResponse from the Workflow Response Handling codeunit. In this function we need to look at the workflow step instance and see if we need to run our response. The function will look something like this:

runresponse

The last bit of coding is setting up the step/ response combination. Create a new function that will subscribe to OnAddWorkflowResponsePredecessorsToLibrary from the Workflow Response Handling codeunit. In this function we just want to set up the ability to have our response added to the step we made before.

combo

The Workflow

With all the coding done we can now make our new workflow. First lets make a new category for our Workflow. You don’t always have to do this if there already is a suitable one. We are going to make one just for the purposes of this walk through. Run the Workflow Category page (page 1508). Then add a new record, i’m going to give mine a code of TEST and a description of “Workflows in progress”. When ever you make a workflow you can always change the category later. This way if someone else sees my workflow they know that its still not done and might be changed in the future.

Catagory

Now lets start making our workflow. Go to the workflows page. By simply going to this page a bunch of things just happened. Our event got added to the library. Our response got added to the library. And the combination setup was ran. On the workflows page create a new workflow. Set the code to something similar to PURCH-USER. Set the description to something meaning full, “Set the pruchase order user to the current user.”. Finally set the category to the new category we just made TEST.

setup

Now we can add in our event step and response. Under “When Event” select “Before Manual Purchase Doc Release.”. You can setup a condition if you want. I’m going to leave mine set to “Always”. For the response we want to select the response we just made. In the end your workflow should look something like this:

workflow

You could add more response if you like. For example displaying a message can usually help with debugging sometimes if you don’t know if your responses are going off or not.  Also, don’t for get to enable your workflow once you are done creating it.

Test it Out

Now lets see this in action. Make sure your workflow is enabled. Go to an open purchase order and try to release it. What should happen is the Assigned User ID will get set to the current user.

You might think we did a lot of work for such a simple change. But we added a lot of extra value to the solution.

We added a new event that we can hook into at any time for other tasks.

We added a new workflow step that could be used for other workflows.

We added a new response that could be used for other workflows.

We gave the user the ability to turn this feature on and off.

We minimized the impact of existing code, making any upgrades or future changes to the base objects much easier.

Workflows and events might take more setup, but you will definitely save time in the long run.

This is the last workflow/ events walk through, if you want to learn about any of these steps in more detail check out any other the other parts:

First part : What are events and the advantages they provide.

Second part: What are the builtin events and how to subscribe to events.

Third Part: Creating event subscribers

Fourth Part : What are workflows and how to make a workflow step

Fifth part: Making a workflow response

Sixth Part: Assigning workflow step and response combinations

Seventh Part: Assigning workflow step predecessors

 

One thought on “Nav 2016 Events and Workflow Walk Through – 8

  1. I receive an error when I try to add the event step. “Function Name must have a value in Workflow Step: Workflow Code=PURCH-USER,ID=405.It cannot be zero or empty”. I follow all the steps and I cannot figure out the problem.

Leave a Comment