Skip to tutorial content
Rate module Not yet rated

UI Policy Scripts

3 min read
On this page

In this lesson, you will learn:

  • When a UI policy needs a script
  • How Execute if true and Execute if false work
  • How to write a simple UI policy script with g_form

1 When actions aren’t enough

UI policy actions can make fields mandatory, visible, or read only. But what if you want to show a message, or set a value? Actions can’t do that. A little script can! πŸ“

Key term: UI policy script

Client-side code that runs when a UI policy’s condition is true or false. Turn it on with the Run scripts checkbox.

Two ways a UI policy can change a form. UI policy actions, with no code, can make fields mandatory, visible, read only, or clear them. UI policy scripts, with code, can also show messages, set values, and do anything else g_form can do. They use Execute if true when the condition matches, and Execute if false when it doesn't.
Figure 1. Use actions first. Add a script only for what actions can’t do.

2 Turning scripts on

  1. Open the UI policy. If you don’t see Run scripts, switch to Advanced view (Related Links).
  2. Check Run scripts. A Script tab appears.
  3. Pick where it runs in Run scripts in UI type: Desktop, Mobile / Service Portal, or All.
  4. Fill in Execute if true, Execute if false, or both.

3 An example

When Category is Network, SNWR wants a helpful message under the Configuration item field.

Execute if true:

function onCondition() {
  g_form.showFieldMsg('cmdb_ci',
    'Pick the router or switch that is affected.', 'info');
}

Execute if false:

function onCondition() {
  g_form.hideFieldMsg('cmdb_ci');
}

πŸ“ Note: g_form is the client-side helper for forms. You’ll learn many more g_form methods in Chapter 36.

πŸ’‘ Good to know: Execute if false only runs when Reverse if false is checked.

⚠️ Heads up: Don’t use a script for things actions can already do, like making a field mandatory. Actions are easier to read and maintain.

⭐ Remember: Run scripts β†’ Execute if true / Execute if false β†’ each is an onCondition() function.

4 Let’s try it: add a hint

  1. Open your Network needs a CI UI policy. Switch to Advanced view.
  2. Check Run scripts. Paste the two scripts above into Execute if true and Execute if false. Update.
  3. Open a new incident and set Category to Network. Do you see the message under Configuration item?
  4. Change Category to Software. Did the message go away?

5 Quick recap

  • Use scripts only when actions can’t do the job
  • Check Run scripts to see the Script tab
  • Execute if true runs when the condition matches
  • Execute if false runs when it doesn’t (needs Reverse if false)
  • Scripts use g_form to change the form

6 Check yourself

Think of your answer first. Then tap to check.

1 Which checkbox shows the Script tab on a UI policy? Show answer

Run scripts.

2 What’s the function name in both script boxes? Show answer

onCondition().

3 You only need to make a field read only. Script or action? Show answer

An action. Keep scripts for things actions can’t do.

Nice work! You finished this lesson. πŸŽ‰

Was this lesson helpful?

Have a question or doubt?

Ask here and get help from instructors and fellow learners.

0 / 300

Only logged-in learners can post.