D365 SysOperation Framework Default Value

Share this:

After adding parameters to a SysOperation Framework batch job, you may want to set those parameters to start with an initial value. In this article, learn how to set a D365 SysOperation Framework default value. By setting a default value that is most appropriate for the data that the job will process, this can save the user time in setting up the batch job. As well as show the user how the developer expected the batch job parameters to be used. Of course, the user can always change these the initial parameter values to something different, and process different data.

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.

Set a Default Parameter Value

After adding these first three classes, we can now add a D365 SysOperation Framework default value.

In this example, I have a batch job that cleans up old data. In the contract class, I have a parameter named ‘parmRetentionDays’. This parameters allows the user to specify how many days of data the system should keep in the table. Then, data older than this number of days will be deleted.

As can be seen, if we do not make any changes, the default value of this parameter will be 0. This is not ideal, as most users do not want to delete all data out of the table. Instead, they likely want to only delete data that is 30 days, or longer. For this purpose, it would be helpful and add safety to have the default value of this parameter be set to 30 instead of 0.

Setting a default parameter value is done by making three changes to the Contract class. I will go through them now.

Implement SysOperationInitializable

First, in order to set a D365 SysOperation Framework Default Value, the contract class must implement the class SysOperationInitializable. It will be easiest to add the code and explain afterwards. Add the following code to the class declaration of your contract class.

[DataContractAttribute]
class rsmTutSysOperationsContract implements SysOperationInitializable
{

Add SysOperationAlwaysInitializeAttribute

Secondly, add the attribute [SysOperationAlwaysInitializeAttribute] above the contract class definition.

[DataContractAttribute]
[SysOperationAlwaysInitializeAttribute]
class rsmTutSysOperationsContract implements SysOperationInitializable
{

Without this attribute, the default value will be set on the parameter the first time the job is run. But then, the next time the user runs the SysOperation Framework batch job, the system will use the last value the user set this parameter to.

To explain, if the user changed it to say 90 days, then the next time the batch job was run for this user, the parameter would default to 90 days, instead of 30 days. This might be desirable. However, often times it is desired that this parameter be set to the same initial value every time it is opened.

Add The Initialize Method

Thirdly, in order to set a D365 SysOperation Framework default value, you need to add a method named ‘initialize’ to the contract class.

    public void initialize()
    {
        retentionDays = 30;
    }

To explain, by adding the ‘implements SysOperationInitializable‘ code to the class definition, the system will now look for and call the method ‘initialize‘ that is added. Note, you must name it ‘initialize‘ to work.

Then, inside the method, set the contract variables to whatever default value you would like it set to. In this example, I have set the retentionDays class variable to 30. If the parameter was an enum, or string, or checkbox, you can set those the same way you would variables of that type.

The Complete Code

That is it! You have learned how to set D365 SysOperation Framework default values for the parameters on the batch job.

D365 SysOperation Framework default value

The complete code will look like this. In my case, the contract name is ‘rsmTutSysOperationsContract’. You can replace this with the name of your contract class.

[DataContractAttribute]
[SysOperationAlwaysInitializeAttribute]
class rsmTutSysOperationsContract  implements SysOperationInitializable
{
    private int retentionDays;

    [DataMember,
        SysOperationLabel(literalStr("Retention days")),
        SysOperationHelpText(literalStr("Data older than the retention days will be cleaned up.")),
        SysOperationDisplayOrder('1')]
    public int parmRetentionDays(int _retentionDays = retentionDays)
    {
        retentionDays = _retentionDays;

        return retentionDays;
    }

    public void initialize()
    {
        retentionDays = 30;
    }
}

Microsoft Documentation

Surprisingly, the only Microsoft documentation on this class I could fine was from a Dynamics AX 2012 whitepaper. Please see page 29 of this pdf file for reference.

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

Additionally, add validation to the parameters. D365 SysOperation Framework Validation

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

In this tutorial, you learned how to set default values for parameters on D365 SysOperation Framework batch jobs. This can help the user use the job in the way it was intended. Additionally, it saves the user time from having to set the parameters themselves to the most commonly used values. However, by having a parameter, the user is still provided with the flexibility of running the job in a way that will process other data. And is often more desirable than hard-coding the underlying query, and not providing a parameter.

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 ↑