The DecisionTableAPI is a server-side class in ServiceNow that lets a script read, create, update, delete and evaluate decision tables. Create an instance with new sn_dt.DecisionTableAPI(), pass a decision table’s sys_id and an object of input values to getDecision or getDecisions, and run the script under an account that holds the decision_table_crud_api role.
What a decision table is built from
A decision table moves branching logic out of application code. Use one when an outcome depends on several factors, such as a location, a date and a record field. Each factor becomes a decision input, and the table checks those inputs against if-then rules to return an answer.
| Part | Platform table | What it holds |
|---|---|---|
| Decision table | sys_decision | The decision’s name and the table that stores its answers |
| Decision input | sys_decision_input | One value the table reads, with a type and an optional default |
| Decision | sys_decision_question | One rule: a condition over the inputs, linked to one answer |
| Answer | sys_decision_multi_result_element | The outcome returned when a rule matches |
Answer records can live on any table, chosen when you create the decision table, and several decisions can point at the same answer. Inputs come in several types: string, integer, true/false, choice, reference, date and date/time.
Evaluating a decision from a script
Both evaluation methods take the decision table’s sys_id first and an inputs object second.
| Method | Returns | Behaviour |
|---|---|---|
| getDecision(decisionID, inputs) | One answer | Returns one correctly evaluated answer |
| getDecisions(decisionID, inputs) | An array of answers | Returns all correctly evaluated answers |
The inputs argument is optional. Without it, getDecision returns the first default answer and getDecisions returns all default answers. The method returns an error when it finds neither a default answer nor a correctly evaluated answer, so handle that case before you read the result.
Each key in the inputs object is the input’s technical name: lower case, prefixed with u_, with spaces replaced by underscores. An input labelled Incident becomes u_incident. The value must match the input’s type. When a table supports multiple answers, the response lists each one in a result_elements field, and the getDecisions example reads u_user from it. Use getDecision when one outcome is wanted, and getDecisions when the calling code must handle all matching outcomes.
Date and time inputs
Date/time values must use the format yyyy-MM-dd HH:mm:ss. In the Australia release, evaluation applies no time zone conversion. Comparisons run against the internal UTC value, inputs do not accept offsets, and neither the user’s nor the system’s time zone is applied. A between condition compares the literal values as passed.
A time a person enters in their own zone therefore needs converting first. The Australia example does this with GlideDateTime: it sets the display value the user entered and passes the internal value read back with getValue.
Reading and changing tables from script
The read methods return JSON. For the record-level calls a script makes beside this API, see the Glide Record guide. getAll returns all decision tables, getDecisionTable returns one, and getInputs, getQuestions and getAnswers return the inputs, decisions and answers attached to a table.
Create the table first, then its inputs, because conditions and questions refer to those inputs. Pass the table’s sys_id into each child-record call. createDecisionTable takes a name, a scope and an optional accessibleFrom value of package_private or public. Its sys_id then goes into the child-record calls: createInputs, createInputChoices, createQuestions, createConditions, createAnswerElements and createAnswerElementChoices.
- createInputs accepts String, Integer, True/False, Choice, Reference, Date and Date/Time. Reference inputs need a reference table name, and String inputs default to a maxsize of 250.
- createAnswerElements accepts Boolean, Choice, Currency, Decimal, Due_date, Glide_date, Glide_date_time, Glide_duration, Integer, Longint, Reference and String, with the same 250 default and reference rule.
- createConditions takes a decision input sys_id, an optional default operator and a label. Valid operators are the ones used for filters and queries.
- createQuestions takes an active flag, a condition, an answer array of name and value pairs, a defaultAnswer flag and an order.
- The choice methods take the parent’s sys_id, a label, a value and an optional order.
The update methods change named properties on one record, and all their fields are optional. updateInput covers active, defaultValue, label, mandatory, maxsize, order and readonly. updateQuestion covers active, answer, condition, defaultAnswer, label and order. The delete methods remove one record by sys_id.
Write methods return a status of Success or Failure and an errors array. Each error has a message and a type, and the type listed is INTERNAL_ERROR. Child-creation calls also return a results array with a status for each record, so check each record’s status as well as the top-level status.
Building the table in Workflow Studio
Build new tables in Workflow Studio, which puts authoring, configuring and monitoring on one page. Go to All > Process Automation > Workflow Studio, select New, then Decision table, and fill in the name, application, accessibility and draft authoring settings. Turn on draft authoring if you expect to revise the table after it is published.
Select Build decision table and add inputs. Input types include choice, currency, date, date/time, decimal, due date, integer, long, reference, string, string (full UTF-8) and true/false. Adding more than 30 inputs can slow the application while you work, so split very wide decisions.
Add a condition column for each input. Each one needs a default operator, which all input types require except true/false. For a reference input, the data-to-evaluate setting decides whether the column tests the referenced record or one of its fields. A choice list must live on a table in the same application scope as the decision table.
Result columns set the outputs. When no alternate result is set, a currency result becomes 0.00 and a true/false result becomes false. Add more result columns to return several values per row. Then fill in the decision rows, with an operator and value in each condition cell and an outcome in each result cell. For a complex condition, open the row in decision rule view. Advanced rows can be edited in Excel but show as read-only there, so split complex decisions into simpler rows where you can. Save, then publish. Since the Xanadu release, you can also create a table’s structure inside a flow or playbook and populate it afterwards.
Calling a decision from flows, playbooks and code
Create Code Snippet on a published table produces two snippets. One calls getDecision and returns the first match, and the other calls getDecisions and returns all matches. With draft authoring on, publish before copying. Otherwise saving is enough. Paste the snippet into your script and assign a value to each input variable. For the wider concept, see script includes explained.
Make a decision flow logic replaces nested if logic with branches, and it needs an external decision table. For the broader flow context, see the Flow Designer guide. Choose First decision that matches or Run all decisions that match. With Use Branches on, each answer gets its own branch block. Clearing the option removes those blocks and the Include Otherwise option, and reselecting it does not restore them. Include Otherwise is an else branch, and it does not run when the table has a default answer.
With branches off, the flow outputs answer records: one for a first match, a list for a run-all. A run-all also outputs ordered IDs sorted by each decision’s order value, plus a count.
The Make a Decision-First Match playbook activity runs the rules, returns the first matching rule by rank, and lets the process continue. To get results from all matching rules, place the table in a subflow. Admin, playbook.admin and pd_author users can add the activity.
Order matters. In classic decisions, the order value sets the sequence in which the flow evaluates decisions, and that sequence can decide which answer a first-match flow returns.
Permissions and release changes
Scripts that call the API need the decision_table_crud_api role, in both scoped and global server-side scripts. Building in Workflow Studio needs the admin or decision_table_admin role, or delegated developer permissions. Creating a code snippet needs no specific role.
The Australia patch 5b release notes let delegated developers create, read, edit and delete decision tables in applications they have delegated developer access to. The same release applies the access control lists on the referenced table to decision table exports, so someone who cannot access those records cannot export them through the table. The Zurich patch X notes list the same export change.
Decision tables support the Localization Framework, but Excel editing does not extend to localized content, so that feature is tied to English instances.
Frequently asked questions
Which role does a script need to use the DecisionTableAPI?
The script needs the decision_table_crud_api role, in both scoped and global server-side scripts. The role is required to use the API at all.
What does getDecision return when no inputs match?
Without inputs, it returns the first default answer. It returns an error when it finds neither a default answer nor a correctly evaluated answer.
What is the difference between getDecision and getDecisions?
Both evaluate the same inputs. getDecision returns one answer, and getDecisions returns an array of all correctly evaluated answers. Without inputs, the first returns the first default answer and the second returns all default answers.
How should date inputs be formatted?
Use yyyy-MM-dd HH:mm:ss. Because the Australia release applies no time zone conversion during evaluation, convert a user’s local value to the internal UTC value before the call.
Can I still build decision tables in the classic environment?
Yes. In the classic environment you can build a table for your decision answer records and associate an answer record with each decision. Workflow Studio is the better starting point, since it puts authoring, configuring and monitoring on one page.
Do decision table exports respect access rules?
Yes. In the Australia release, exports apply the access control lists on the referenced table, and the Zurich patch X notes list the same change.