SDK: Rule Steps (Basic)
  • 16 Dec 2024
  • 3 Minutes to read
  • Dark
    Light

SDK: Rule Steps (Basic)

  • Dark
    Light

Article summary

Overview

You can easily create new rules to be used in Decisions by creating public methods in a class file project. This article will show you how to accomplish this.

Decisions has a Public GitHub Repository with various SDK examples available.

Example

This example will add an attribute to a method to make a Flow step available as a Rule Verb. 

It is important to know that this method was used primarily for an example. Developers can write any type of method that is logical for their specific use case.

The example code is the same code used in the Basic Flow Steps article. At the beginning of the code, add the attribute [AutoRegisterMethodsAsRuleSteps(true, "SDK")]. This attribute will allow the code to show as a Rule Verb within the SDK category in the Rule Designer. This attribute and others are further explained in the AutoRegister Attribute Glossary article.

In order for the method to be picked up by Decisions as a Rule Verb, it must return a boolean. The name of the method will be the name of the Rule. Also, the inputs and outputs of the method will become the inputs and outputs of the rule.


After writing the method, add it to Decisions by following the steps in the SDK Basics.

Log into the Decisions Studio and create a new Rule to open the Rule Designer.  The method is now available in the Rule Designer as a Rule Verb. 


Adding Parameters 

Developers are able to add parameters to the method to make the Rule more complex and easier for business users to use. In the following screenshot, the following code was added to the above example.  

It is important to know that this method was used primarily for an example. Developers can write any type of method that is logical for their specific use case. The following code is named "IsInvoiceWithoutShippingOverAmount". The anchor data is a data type called "Invoice" that was created in the Creating Custom Data Types article and the input is a decimal. There is nothing else in this code that makes it useful from a business logic point of view.  


public bool IsInvoiceWithoutShippingOverAmount(Invoice myInvoice, decimal Amount)
        {
            return false;
        }


After writing the method, add it to Decisions by following the steps in the SDK Basics .

The new Rule Verb is now available to use only when the anchor data is the type "Invoice". When the Rule verb is applied, the decimal input then populates. Keep in mind, multiple parameters can be added to a Rule Step to make the Rule more complex. 


Property Classification

The following example shows how to add property value to a Rule. This rule evaluates the length of a string to see if its length is less than a number value.
The property of the rule would be written like this:

 [WritableValue]
private int number;
[PropertyClassification(0, "Number", "Settings")]
public virtual int Number
{
    get { return number; }
    set
    {
        number = value;
        OnPropertyChanged("Number");
    }
}


The run method would use the property like this:

 public override bool Run(RuleStepExecutionData data)
{
   return (((string)data.Data[this.AnchorData.Name]).Length < number);
}

This property would show up in the rule designer as a value that can be hardcoded.

We may also want to add some design-time validation to our rule which would prevent the user from entering a number value less than 1. To do this, you will need to configure your class to inherit from and implement the IValidationSource interface. You'll also want to add InvalidateVerbInfo(); to you, the property is set so that the validations show up correctly in the designer when the value of your property is changed. The property and implemented IValidationSource would look like the following.

 [WritableValue]
private int number;
[PropertyClassification(0, "Number", "Settings")]
public virtual int Number
{
    get { return number; }
    set
    {
        number = value;
        OnPropertyChanged("Number");
        InvalidateVerbInfo();
    }
}
public ValidationIssue[] GetValidationIssues()
{
    List issues = new List();
    if (number < 1)
        issues.Add(new ValidationIssue(this, "Number must be greater than 0", null, BreakLevel.Fatal, "Number"));
    return issues.ToArray();
}

Was this article helpful?