D365 SysOperation Framework Validation

Share this:

In this article, learn how to add D365 SysOperation Framework validation to ensure that parameters are set correctly. In a previous lesson you learned how to add parameters to a batch job. While adding parameters provides flexibility to the user, it may allow the user to run the batch job in a way it was not intended. By adding validation to the parameters, the system will not actually run the batch job unless the parameters are correct. Additionally, the system can show a message to the user, explaining the issue.

Review

In a previous article, I explained how to develop a batch job using SysOperation Framework in D365. Before adding a user interface to your batch job you need to first follow the steps in that article. As a reminder, you need to create three classes in Visual Studio and add them to a project.

  1. First, create a Contract class. This was empty in the last article. In this article, we will add code to this class. This will contain the parameters that will be shown in the D365 SysOperation Framework user interface.
  2. Secondly, you need to create a Service class. One that extends the class SysOperationServiceBase. This class contains the code that performs some kind of process. In this article, we will allow a developer to use the parameters from the Contract class to affect how the processing code works.
  3. Thirdly, you need to create a Controller class. This class really just configures how the batch job works. It tells the system what method on the Service class to run.

Add Parameter Validation

After adding the first three classes, we can now add D365 SysOperation Framework validation to the parameters.

In this example, I have a batch job that has two parameters. A ‘from date‘ and a ‘to date‘. It would be a problem if a user were to accidentally set the ‘to date‘ to a date that was before the ‘from date‘. This wouldn’t make any sense. Therefore, we should add validation to ensure that the user cannot run the batch job unless the ‘to date‘ is after the ‘from date‘.

Before proceeding, I wanted to make one quick note. Most of the time is not great to have a batch job with explicit ‘from’ and ‘to’ date parameters. This is because if the batch job were to be setup on a recurrence, it would be processing the same date range every time it ran. Instead, you should setup your batch job to be relative to the current date. That way, each time the batch job runs, it has an opportunity to process an ever changing set of data. However, there are still some cases where this example may make sense. And batch jobs do not always have to run on a recurrence.

In order to add validation to parameters, two changes need to be made.

Implement SysOperationValidatable

First, in order to add D365 SysOperation Framework validation, the contract class must implement the class SysOperationValidatable. Again, it will be easiest to add the code and explain afterwards. Add the following code to the class declaration of the contract class.

[DataContractAttribute]
class rsmTutSysOperationsContract implements SysOperationValidatable
{

Add Validate Method

Secondly, add a method named ‘validate‘ to the Contract class.

    public boolean validate()
    {
        boolean isValid = true;

        if (!dateFrom)
        {
            isValid = checkFailed("From date is required.");
        }

        if (!dateTo)
        {
            isValid = checkFailed("To date is required.");
        }

        if (dateFrom && dateTo && dateFrom > dateTo)
        {
            isValid = checkFailed("From date cannot be greater than to date.");
        }

        return isValid;
    }

The ‘validate‘ method gets called when the user clicks the ‘ok‘ button. It returns a boolean value. If the method returns ‘false’, then the system will not allow the user to close the form. Any messages will be shown on the dialog box.

D365 SysOperation Framework validation on parameters

To see Microsoft’s documentation on this functionality, please see the article here.

Validate Method On Controller Class

There is another way of adding D365 SysOperation Framework validation. Instead of implementing the SysOperationValidatable class, and adding the ‘validate‘ method to the Contract class, you can actually just add a ‘validate’ method to the Controller class. For example, add this code to the Controller class.

    protected boolean validate()
    {
        rsmTutSysOperationsContract    contract;
        boolean                         isValid = true;
        contract = this.getDataContractObject();

        if (!contract.parmDateFrom())
        {
            isValid = checkFailed("From date is required.");
        }

        if (!contract.parmDateTo())
        {
            isValid = checkFailed("To date is required.");
        }

        if (contract.parmDateFrom() 
            && contract.parmDateTo() 
            && contract.parmDateFrom() > contract.parmDateTo() )
        {
            isValid = checkFailed("From date cannot be greater than to date.");
        }
        return isValid;
    }

This ‘validate’ method is also called when the user presses the ‘ok‘ button.

In this example, my Contract class is named ‘rsmTutSysOperatoinsContract‘. You will need to replace this with the name of your contract class. The code then gets the contract class object, and assigns it to a local variable. Then, the system calls the ‘parm‘ methods to reach the values stores in the contract class and performs validation.

Parameters, Defaults, Validation, Mandatory and UIBuilder

Please see these other articles for more advanced SysOperation Framework functionality.

First, learn how to add parameters to your batch jobs. D365 SysOperation Framework Parameters

Next, override the drop-downs to show only validate options. D365 SysOperation Framework Override Lookups

After adding parameters to a SysOperation Framework batch job, you may want to set those parameters to start with an initial value. Learn how to set a D365 SysOperation Framework default value.

Lastly, learn to add D365 SysOperation Framework mandatory parameters that show a red outline and a star to indicate the field must be populated.

For another great reference on how to create classes using the SysOperation Framework in D365, see this article.

Conclusion

By adding D365 SysOperation Framework validation on parameters, you allow the flexibility of parameters while still protecting the user from values that either do not make sense or would be harmful for the batch job to use. Additionally, you can show messages to the user to explain why their selection is not valid for the particular batch job.

Peter Ramer
Peter Ramer is a part of the Managed Application Services team at RSM working on Microsoft Dynamics 365. He focuses on the Retail and Commerce industries. When he is not solving problems and finding ways to accelerate his clients' business, he enjoys time with his three kids and amazing wife.

Share this:

Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑