What controllers do in UI builder?

AJAY BIRADAR
Tera Contributor

Hi everyone, 

What controllers do in workspace and can anyone explain with relatable example?

1 REPLY 1

Yashsvi
Kilo Sage

Hi @AJAY BIRADAR,

In ServiceNow, controllers play a crucial role in defining the server-side logic for handling client-side events and actions within a particular workspace, such as a ServiceNow portal or a custom application interface. They are primarily used in client scripts to manipulate data, control UI behaviors, and manage user interactions.

Example Scenario: Incident Management:

Problem: You want to customize the behavior of the incident form in ServiceNow when users interact with it.

Solution using Controllers:
1. Create a Client Script: Start by creating a client script that includes a controller. Client scripts are JavaScript-based scripts that execute on the client-side (user's browser) to interact with forms, fields, and UI elements.

// Client script with a controller
   (function() {
       // Define the controller
       var incidentFormController = Class.create();
       incidentFormController.prototype = {
           initialize: function() {
               // Constructor function for initialization
           },

           // Example method to set default values
           setDefaultValues: function() {
               // Access the current form
               var form = g_form.getFormElement();

               // Set default values for specific fields
               g_form.setValue('caller_id', gs.getUserID());  // Sets the current user as the caller
               g_form.setValue('impact', '2');  // Sets default impact to moderate
           },

// Example method to validate inputs
           validateInputs: function() {
               // Validate if certain conditions are met
               var priority = g_form.getValue('priority');
               if (priority == '1' && g_user.hasRole('itil')) {
                   g_form.showFieldMsg('priority', 'High priority selected for ITIL users!', 'info');
               }
           }
       };

       // Instantiate the controller
       var controller = new incidentFormController();
       controller.setDefaultValues();  // Call a method on controller instantiation
   })();

In this script:
- incidentFormController - is a controller defined using ServiceNow's Class.create() method.
- initialize()- serves as the constructor for the controller, allowing you to set up initial conditions.
- setDefaultValues()- is a method that sets default values for fields when the incident form loads.
- validateInputs()- is a method that validates user inputs based on certain conditions.

2. Attach the Client Script: Attach this client script to the incident form in ServiceNow. This can be done through the Form Layout section or via UI Policies depending on the specific customization needs.

3. Execution in Workspace: When a user opens the incident form in the ServiceNow workspace:
- The client script with the controller will execute.
- The setDefaultValues() method will automatically set the current user as the caller and default the impact field to '2'.
- The validateInputs() method will check if a high priority is selected by ITIL users and display an informational message if so.

"Controllers in ServiceNow, such as the one demonstrated, allow developers and administrators to encapsulate logic and behavior that manipulates forms and interacts with users effectively. They streamline processes, ensure consistency in data handling, and enhance user experience by customizing how forms behave and respond to user actions in the ServiceNow workspace."

Thank you, please make helpful if you accept the solution.