Rahman4
Tera Guru

What is a Decision Table?

Decision Builder is a custom app that enables to manage decision tables. A way to organise and create business logic outside of the code. It is a place to create and store logic. It replaces the IF and ELSE conditions and enables to change the logic in one place. So, if there is a logic that may change in future or needs to be used in multiple places e.g., a Flow, a BR, Script includes etc then it is best to create a Decision Table for it and reuse it from everywhere. To be noted that this article is not about basics of the Decision Table. If you don’t know what Decision Builder is then please start from here. There are many good resources about the usage of the Decision Tables from the Flow Designer, however this article will focus on usage of the Decision Builder from the Script which was released in the latest update of the ServiceNow (I believe Utah release). An update-set is attached to this article that includes this Decision Table.

 

Benefits:

  • Build complex decisions easily with decision tables rather than multiple IFs and Else
  • Reusable code from different parts of the platform e.g., Scripts, BRs, Flow etc
  • Increase efficiency by reusing decision logic across multiple applications.
  • Business logic changes in one place
  • Export and import using the Excel.

How to use Decision Table from script?

Unfortunately, there is no boiler plate code snippet generator like Flows and Sub-Flows for Decision Table right now. I have created an idea in ServiceNow idea portal that is still under consideration by ServiceNow. If you feel that the idea worths it to be implemented, then please up-vote the idea here.

 

In this example, I have created a simple hypothetical Decision Table that accepts a User record (reference field) as an input, uses the user’s Job and Department field to decide the type of Laptop that the user may need.

Conditions are as following:

  • If the user’s department is Development and Job Title is “Senior Developer” then they get a Windows and a Mac laptops
  • If the user’s department is Development and Job Title is “Developer” then they get a Windows laptop
  • If the user’s department is Development and Job Title is “IT Support” then they get a Unix laptop
  • If the user’s department is Sales then regardless of their job title they a Mac
  • Everyone else get a Windows (Default)

The decision table is represented as following:

 

Rahman4_0-1691894586558.png

 

The following is an example code that calls the above Decision Table from a script. The script can be copy and pated to a background script for testing. To be noted that you may need to update the existing users job title and department to match the decision (the example below is using sys_ids of the existing demo users like Abel Tutor etc however Job Title and Department needs to be updated to match the above Decision Table).

 

 

printDecision('712150f4530360100999ddeeff7b1227'); // Senior developer
printDecision('5137153cc611227c000bbd1bd8cd2005'); // Developer
printDecision('46b9852ca9fe198101531c8c1e926c86'); // Sales
printDecision('dc6053dcd7011200f2d224837e6103e0'); // IT Support
 
function printDecision(userId){
    var grSysUser = getUser(userId);
    if (grSysUser) {
        var dt = new sn_dt.DecisionTableAPI();
        var inputs = {}
        inputs.u_user = grSysUser; // This is for the input User (reference type). To be noted u_
        var response = 
        dt.getDecision('7521e53e97003110653ffbf6f053afda', inputs); // Decision builder sys_id
        var result = response.result_elements.u_laptop_type; // This is for the output Laptop Type
        gs.log("Laptop: " + result)
        gs.log("-----------------------------------------")
    }
}

function getUser(userId){
    var grSysUser = new GlideRecord('sys_user');
    grSysUser.get(userId);
    gs.log("User: " + grSysUser.name);
    gs.log("Department: " + grSysUser.department.getDisplayValue());
    gs.log("Title: " + grSysUser.title);
    return  grSysUser;
}