Struggling to Create Business Rules Using Scripts? See how ChatGPT Can Help!

AI for effortless creation of simple scripting rules in common scenarios.

Jana Sprušilová
Consultant Tester
?
Learn About
Check icon A checkmark inside a circle signifying "yes" Minus icon A minus inside a circle signifying "no" PROS Icon A plus symbol representing positive aspects or benefits. CONS Icon A minus symbol representing negative aspects or drawbacks.

Why Should I Consider Script in No-code/Low-code

DecisionRules is designed to be user-friendly, allowing business users to create and modify rules without requiring deep programming knowledge. We have been advocating the idea of low-code/no-code platforms for the past couple of years,  however, it may still happen that a programmatic/scripting approach might be suitable for more complex logic or data handling. Such an approach can sometimes even simplify, for example, the work with other rules. By using a scripting rule, your other rules can remain simple and easy to manage, instead of increasing in complexity.

Benefits of Scripted Solutions

Scripting rules are powerful for automating decision-making processes within a business environment. They allow you to define conditions and actions using code, enabling flexible and customizable logic tailored to specific logic requirements. However, crafting effective scripting rules can sometimes be challenging, especially for those who may not be familiar with coding in general.

Fortunately, with the assistance of AI like ChatGPT, you can streamline the process of writing scripting rules, leveraging its language understanding capabilities and coding assistance to create efficient and accurate rules.

ChatGPT is an effective tool that can do wonders thanks to its advanced models. To better understand the code that AI returns, it is convenient to know the basic components of a scripting rule. These can include rule input and output model properties, rule variables and how to access those, syntax special cases, or best practices in general. For more information about scripting rules please refer to the dedicated section in our documentation.

Creating the right prompt

With the ChatGPT model, which can search and read web pages such as our documentation, you simply submit a prompt to create our scripting rule along with your specific task of what logic the scripting rule will be evaluating. For a more precise answer, you can enter a prescription of what the scripting rule looks like. See examples below.

Generic prompt with scripting rule description

About Scripting Rule:

Scripting Rule is a type of business decision rule, where users can create rules and conditions using JavaScript code. The code is written in Monaco editor. Input and output model represented in JSON format body.

Every rule have input schema as follows:

{

    "inputProperty": {}

  }

and output:

{

    "outputProperty": {}

  }

Input and output can be accessed in code using dot notation - from example above input.inputProperty and can be stored in variables declared. Addressing output properties works the same way, output.outputProperty. Assigning values to output properties ensures passing those values to the final output of the rule.

In the Rule Settings there can be rule variables set, variables with fixed values. Example taxRate = 0.15. Rule variables are accessed using dot notation: ruleVariables.taxRate.

Special syntax

There are some forbidden functions and methods such as:

- console.log(), for logging to Scripting Rule console use log() instead.

At the end of each script created, there has to be a code line: "return output;" to ensure that Scripting Rule results will be passed to rule output.

Common use cases of Scripting Rule

1. Random Numbers

Let's say we need to generate a random number from a given range. 

Use the previously mentioned generic prompt with the following prompt, which contains a specific description of the task.

Prompt

Task: I need to create a simple scripting rule in the DecisionRules.io app. This scripting rule should generate random numbers from the range given in input. Input model is: 

{

  "Random_range_from": {},

  "Random_range_to": {}

};

Output model is:

{

  "Rand": {}

}

Response

As you can see, ChatGPT will automatically include an explanation of the line of code. When we copy the generated code into a prepared rule which has a set input-output model, we can see that the code matches our settings and no lines are underlined, so there is no error. Now we can simply save the rule and test it in the Test Bench. Running the rule with a specified range of numbers returns a random number corresponding to the range.

2. Working with arrays

A common task for scripting rules is to process data in an array. In this case, we'll analyze a list of transactions to determine several metrics, such as the total amount, average transaction value, and the top three highest transactions. We'll use an array of “transactions” object, and return an object with the computed metrics. You can use the following task description as a prompt.

Prompt

Task: We analyze a list of transactions to determine several metrics, such as the total amount, average transaction value, and the top three highest transactions. As an input model we'll use property "transactions" and return an object with the computed metrics: “totalAmount”, “averageTransactionValue”, “topThreeTransactions”. Use DecisonRules.io Scripting Rule and some simple array functions.

Response

In the response above, you can see that ChatGPT will automatically create some control in front of the logic we specify, to see if an array arrives at the input at all. If no array is coming in, it prints an error message to the log.

Further, in the generated code, you can see the use of functions that work with the input array. Now you can copy generated code and paste it into the rule designer. After saving and running the rule with a test set of data, it can be seen that the generated functions work well and correctly pass the calculated values to the defined outputs.

Conclusions

In conclusion, ChatGPT can be a valuable ally in the process of writing scripting rules, offering guidance to help users create simple and effective rules for their business needs. By leveraging ChatGPT's capabilities, you can streamline rule development and unlock the full potential of scripting rule automation.

In the examples above, ChatGPT helps clarify the structure and provides assistance in performing the necessary calculations. With support, writing the scripting rule became a straightforward process, enabling you to quickly implement the desired logic in a programmatic way. This combination of low-code accessibility and AI-enhanced coding assistance ensures that you can efficiently address a wide range of business logic requirements.

Enjoyed the read? Let’s take the next step together.