Skip to main content

Exercise 3: Field Decorator Declarative Action

Declarative actions add custom functionality to your Workspace forms, fields, lists, and related lists without writing custom scripts or learning APIs.

Goal​

In this exercise, you'll add a field decorator declarative action. In the HR Agent Workspace, you have a requirement to add a field decorator (icon button) to the HR Service field that will pop open a modal showing the most recent 10 cases opened in that category.

There are a few ways you could solve this problem of getting more context around what people are asking related to a specific service, and this exercise will teach you how to solve it with a declarative action.

Add Demo Data​

You'll need to create a quick case for demo data.

  1. In the platform view, go to Workspaces > HR Agent Workspace.

  2. Click on the plus sign and create a new HR Case.

  3. Choose Skip verification and fill out the form:

    • COE: HR Case

    • HR Service: General Inquiry

  4. Click Create Case.

  5. Create a second case.

Build the modal and event in UIB

Create a new record page variant​

  1. Open UI Builder (UIB) by going to All > UI Builder or click the icon at the top left of the UIB window to get to the UIB homepage.

  2. Open the HR Agent Workspace experience.

  3. Switch your scope to the Agent Workspace for HR Case Management.

    When duplicating a variant you want to do it in the same scope as the original variant.

  4. Scroll down to the Record page and, mouse over Case SRP, click the three vertical dots icon to the far right, and choose Duplicate Variant.

  5. Name the variant Case SRP Acme copy and click Create.

    Since you're duplicating the page and owning the new variant it's probably helpful to come up with some sort of naming convention to easily tell your variants apart from the ServiceNow shipped pages.

  6. Click the Settings pill at the top of the page, change the order to 0, and click the Save button.

  7. Switch back to the editor view and give the page some test values by clicking the table pill just under the experience name at the top of your window.

  8. Set the table required parameter to sn_hr_core_case, let the sysId auto-fill, and choose Apply.

    You'll now see some of the components appear in the middle of the page since they've got data.

  9. Save the UI Builder page.

Add a modal​

Now you'll add a new modal to the page that will show a list of related records.

  1. In the content tree, mouse over modals toward the top under Overlays, choose the plus sign to the right, and choose Custom.

  2. Rename the modal by clicking the (i) icon next to Custom 1 in the right-hand panel to the following:

    • Component label: Related cases modal

    • Component ID: related_cases_modal

  3. Click Apply.

  4. Collapse all the modals in the content tree on the left except for your new modal, and click +Add component under the Heading 1 section of your modal.

  5. Add a Stylized text component.

  6. Remove the preset using the preset dropdown on the config panel and clicking Remove in the popup.

  7. Change the Text property to data binding.

  8. Choose the Formulas tab and the CONCAT formula.

  9. Double-click into value1 and type "Related records for " (with the quotes)

  10. Now click into the second value, click into the Data types tab, and choose Data resource > record > form > fields > hr_service > displayValue, and choose Apply.

  11. In the Body 2 section in the content tree, choose + Add component and add a List - Simple.

  12. Choose None for the preset to configure it manually.

  13. Configure the component's properties in the Configure panel as follows:

    • Table: HR Case

    • Edit fixed filter: <data binding> HR service | is | Data resource > record > form > fields > hr_service > value

    • Columns: <add one at a time>

      • Number

      • Subject person

      • Description

    • Maximum rows: 10

  14. Now add a Button component to the modal's footer.

  15. Remove the preset.

  16. Change the button's label to Close.

  17. Click into the Styles tab and give the button a margin of L.

  18. Click into the Events tab and choose Add Event Handler.

  19. Choose Open or close modal dialog.

  20. Choose the Related cases modal for the Modal and click Add.

  21. Save the UI Builder page.

Add a handled event to the page​

In this section, you'll create a handled page event. This will alow the declarative action to tie into it and take some action based on what the event does. Handled events can be used for any number of things including if you need to call the same series of event handlers more than once, you could do that from the handled event and define it once.

  1. Click on Body in the content tree.

  2. Click into the Events tab on the right.

  3. Scroll all the way down to the Handled events section at the very bottom of the Events tab and choose +Add.

  4. Set the Event label to HR Service Related Records and click Add.

  5. Scroll back up in the Events tab and choose the + Add event mapping button.

  6. Choose the HR Service Related Records event you just created.

  7. Choose the Open or close modal dialog event, set the following, and click Add.

    • Open modal dialog: true

    • Modal: Related cases modal

  8. Save the page.

Create the declarative action​

Now you'll use the declarative actions framework to create an action that will show up on the HR Service field as an icon button that will trigger the event you just created.

  1. In your Classic environment browser tab, go to All > Now Experience Framework > Declarative Actions > Create New Action.

  2. Choose Field decorator on the page that comes up and fill out the form as follows:

    • Action label: HR Service Related Records

    • Action name: hr_service_related_records

    • Implemented as: UXF Client Action

    • Table: HR Case [sn_hr_core_case]

  3. Specify client action: <click the magnifying glass and choose **New** to create a new client action>

    • Key: HR_SERVICE_ACTION

    • Label: HR Service Related Records

    • Applicable to: UXF, Field

    • Payload:

      {
      "referenceField": "{{fieldName}}",
      "referenceKey": "{{referenceKey}}",
      "referenceTable": "{{referenceTable}}",
      "fieldLabel": "{{label}}",
      "parentTable": "{{tableName}}",
      "parentSysId": "{{recordSysId}}",
      "encodedRecord": "{{encodedRecord}}",
      "serializedChanges": "{{serializedChanges}}",
      "fieldType": "{{fieldType}}",
      "value": "{{value}}",
      "displayValue": "{{displayValue}}",
      "dependentField" : "{{dependentField}}",
      "dependentValue": "{{dependentValue}}"
      }
  4. Click Submit.

  5. Set the following fields:

    • Decorator applies to: Specific field

    • Field name: HR service

    • Icon: related-list-fill

    • Tooltip: Other cases for this HR Service

    • Description: Shows the other cases for this HR Service

  6. Right-click on the header and choose Save.

  7. Choose Advanced view under Related Links below the form fields.

  8. When the form reloads click on the Conditions tab.

  9. Set the Record Conditions to: HR Service | is not empty

  10. Right-click on the header and Save the form.

  11. Scroll down to and click on the UX Add-on Event Mappings related list.

  12. Choose New and fill out the form as follows:

    • Event mapping name: HR Service Related Records

    • Source element ID: form

    • Parent Macroponent: Case SRP Acme copy

    • Target Event: HR Service Related Records

    • Target Payload Mapping:

      {
      "container": {
      "fields": {
      "container": {
      "encodedRecord": {
      "binding": {
      "address": [
      "encodedRecord"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      },
      "label": {
      "binding": {
      "address": [
      "fieldLabel"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      },
      "referenceField": {
      "binding": {
      "address": [
      "referenceField"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      },
      "referenceKey": {
      "binding": {
      "address": [
      "referenceKey"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      },
      "serializedChanges": {
      "binding": {
      "address": [
      "serializedChanges"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      },
      "size": {
      "type": "JSON_LITERAL",
      "value": "md"
      },
      "sysId": {
      "binding": {
      "address": [
      "parentSysId"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      },
      "table": {
      "binding": {
      "address": [
      "parentTable"
      ]
      },
      "type": "EVENT_PAYLOAD_BINDING"
      }
      },
      "type": "MAP_CONTAINER"
      },
      "params": {
      "type": "JSON_LITERAL",
      "value": {}
      },
      "route": {
      "type": "JSON_LITERAL",
      "value": "modal_ref_lookup"
      },
      "size": {
      "type": "JSON_LITERAL",
      "value": "lg"
      }
      },
      "type": "MAP_CONTAINER"
      }

  13. Choose Submit.

  14. Now to test this open a case in the HR Agent Workspace and notice the new icon in the HR service field next to the preview icon. When you click it the modal should appear, but sometimes declarative actions have caching issues. You may need to clear the instance cache by appending cache.do to the url after .service-now.com/, clearing browser cache, and closing and reopening windows.