logo

NJP

Practical UIB Examples: The Comparator Component

Import · Oct 08, 2023 · article

The Comparator component allows to compare content side-by-side by highlighting the differences between the original and changed content. It can also be used to edit or translate the changed content in place.

26_final_result_small.png

To illustrate the component what’s not better than to leverage the ServiceNow knowledge base? So, to get started I created an article and edited it to have 3 versions.

1_kb_articles.png

Next, we'll create a page.

Create a page.

If you already have a page open, skip this section and go directly to the section Configure the comparator component

In App Engine Studio, open your experience (workspace or portal) and click "Open in UI Builder"

2_open_UI_Builder.png

In the UI Builder view, click the name of your experience to open the Experience Overview.

3_Click_UIB_Overview.png

Note: The view might look slightly different based on your ServiceNow version but the concepts are the same.

Then click the (+) icon to create a new page.

4_click the (+) icon to create a new page.png

Then, click on "Create from scratch instead"

5_Create-from-Scratch.png

1) Give the page the name: “Compare Article Versions” and 2) Click Continue

6_Name-the-page.png

Next up, we want our page to display comparisons between different article versions—and not just for one article, but any article. So, to make this possible we'll add a page parameter that will allow us to specify which record to open when the page is rendered.

Click Add,

Set the parameter's name to "kbNumber",

Click "Looks good"

7_Add-Page-Parameter.png

Next, we'll accept the Default variant, Click Create at the bottom of the page.

8_Create_Default_Variant.png

To make the most of UI Builder's real-time WYSIWYG preview capabilities, let's set up a test value for our page parameter. This will allow us to preview what the page will look like as we build it.

Click the page parameter

Set the value to your kb number

Click Apply

9_Set-Page-Parameter.png

Now, we're ready to...

Configure the comparator component

Add the component to the page.

10_Add-the-component.png

Set the Data the component expects

Each component comes pre-configured with static data to get a preview of the end result. Let’s inspect that.

Go to the Config panel on the right, and Click on Edit on the Comparison data.

11_Edit_Component-Data.png

The comparison data is shown as an array of objects. Initially, the static data format wasn't immediately clear to me, so I went to check the documentation of the component. Go check it out, it really helps in understand ing how to component works. In short, each item in the list corresponds to an element in your record that you wish to compare using the comparator component.

12_Component_Data.png

Now, with a clearer understanding of the required data format, the next step is transforming our data into this format. We have a knowledge article that needs to be adapted to match the component’s format requirements. In the Next Experience framework, the go-to tool for this task is the Data Resource.

Create the Data Resource

What Are Data Resources? Data Resources define what data to fetch for a page's components. Using Data Resources decouples data fetching from hard-coded component configuration. Fetching data dynamically makes components reusable across pages and experiences as long as both the experience and user have access to the data.

Click the Data icon

13_click_Data.png

Then click the + Add button in the Data resource instances section.

Select the type “Transform” for the new Data Resource.

14_Create-New_Transform.png

This opens a new window to create the Data Broker Server Script.

NOTE: The term D ata Broker is another name for a Data Resource.

Give it a name: getKBChangedContent

A description: Returns the changes made between two versions of a Knowledge Base (KB) article

The properties are the input for the Data Broker, similar to how parameters work in a function. We’re looking to compare two versions of a knowledge article, so we need to set three properties: 'kb_number', 'version_1', and 'version_2'."

[
  {
    "name": "kb_number",
    "label": "KB Number",
    "description": "Number of the Knowledge Base (KB) article",
    "readOnly": false,
    "fieldType": "string",
    "mandatory": true,
    "defaultValue": ""
  },
  {
    "name": "version_1",
    "label": "Version 1",
    "description": "the initial version of the KB article to be compared",
    "readOnly": false,
    "fieldType": "string",
    "mandatory": true,
    "defaultValue": ""
  },
  {
    "name": "version_2",
    "label": "Version 2",
    "description": "the subsequent version of the KB article for comparison.",
    "readOnly": false,
    "fieldType": "string",
    "mandatory": true,
    "defaultValue": ""
  }
]

and for the Script, copy the following:

function transform(input) {

    var kb_number = input.kb_number;
    var version_1 = input.version_1;
    var version_2 = input.version_2;

    var kb_record = new GlideRecord('kb_knowledge');
    kb_record.addQuery('number', kb_number);
    kb_record.addEncodedQuery(`number=${kb_number}^version.version=${version_1}^ORversion.version=${version_2}`);
    kb_record.orderBy('sys_created_on');
    kb_record.query();

    var results = [];

    if (kb_record.next()) {
        var originalContent = {
            short_description: kb_record.short_description + "",
            text: kb_record.text + ""
        };

        kb_record.next();

        var changedContent = {
            short_description: kb_record.short_description + "",
            text: kb_record.text + ""
        };

        results.push({
            changedContent: changedContent.short_description,
            contentType: "string",
            heading: "Short description",
            name: "short_description",
            originalContent: originalContent.short_description
        });

        results.push({
            changedContent: changedContent.text,
            contentType: "html",
            heading: "Article body",
            name: "text",
            originalContent: originalContent.text
        });
    }

    return results;
}

Click Submit.

Now, let's add the data resource to our page.

Go back to the page "Compare Article Versions”

Note: In versions prior to Vancouver, after adding a new data resource, a notification prompts you to refresh the page to make that resource available for use. Starting with the Vancouver version, this step has been streamlined; the data resource becomes automatically available without the need for a page refresh.

Click the + Add button

In the search field, type "getkb" (or the name of your data transform)

Select the transform

④ Click Add

15_Add-the-new-Transform.png

Next, UI Builder is going to display a message saying: You don’t have permission to select this data resource. Contact your admin for more information.

16_ACL_Error.png

Note: In ServiceNow, security takes the front seat. This means that not everyone has the green light to execute queries, especially those that could potentially be a risk in terms of performance and security. So, what’s the next step? It's time to either step into the admin role ourselves or touch base with the admin team to secure the necessary permissions to run this data transform.

With our admin hat on, we’ll follow these steps:

Select the data resource

Click on Open record

17_open_data-resource.png

This opens the data transform page,

Right-click on the record header

Click on Copy sys_id

18_Copy_SysId.png

Next, elevate your role to security admin by following the steps below:

Click the user profile icon in the header

Click on Elevate role

Check the box security_admin

Click Update

19_elevate_role_security_admin.png

Create an Access Control

Click All

Type Access control

Click on Access control (ACL)

20_Search-ACL.png

Click New to create a new ACL record and set the following

Type : UX Data Broker

Operation: execute

Name: paste the sys_id of the data transform you copied earlier (if the Name field appears as a drop down , click the blue right triangle ▸, to change the mode of the field to Text field)

Click the Submit button

21_Create_ACL.png

Go back to the page "Compare Article Versions” and

Switch the setting for When to evaluate this data resource back and forth to refresh the Data Resource.

You will see the error disappear and the data resource returns an empty list "[]"

22_no-more-error-in-the-data-resource.png

Let's pass real values to the data resource:

set the KB number

"1.0" for the first version

"2.0" for the second version

See the data resource successfully returning data

23_set_data-resource-values.png

Pass dynamic data to the Component

Now we can configure the component to make it display data about our kb article.

To do that:

Select the component in the page outline panel

In the component config panel, click the "Data" icon

Type "@data" then "." (dot), then the name of your data resource (here: getkbchangedcontent_1) then "." (dot), then "output"

And voila, the component now displays data from your record.

24_bind_data.png

Click Save, open the page and you should see the final result.

25_final_result.png

From there the sky is the limit. For example, we can add two Dropdown components to allow users to select which version to compare.

27_next.png

View original source

https://www.servicenow.com/community/next-experience-articles/practical-uib-examples-the-comparator-component/ta-p/2693292