How to hide Annotation using UI policy

Nirmala5
Mega Contributor

How to hide a annotation through UI Policy.

or

Is there a field type only to display the label or text. so that I had set visibility through UI Policy

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Nirmala,

Annotation may be hidden using DOM manipulation. DOM manipulation is not recommended because it may stop working after ServiceNow is versioned up.

As an example, I've created a form like below with 3 fields each with annotation. The numeric scale on top is used to show or hide annotation. 0 implies show all annotation.

find_real_file.png

This is the client script used to hide and re-display annotation. Service Portal and UI have different logic and the first "if (window)" is used to determine if this is being called from Service Portal page or UI page.

In the Service Portal page, 'annotation = this.document.getElementsByClassName("help-tag");' is used to get all annotation elements. 'annotation[i]' reference's the ith annotation with i starting from 0. For example, annotation[1] will be the second annotation in the form. ".style.display == 'none'" will hide the annotation while ".style.display = 'initial'" will re-display it.

In the UI page, 'annotation = document.getElementsByClassName("annotation");' is used to get all annotation elements.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var annotation;
    if (window) {
        // ui logic
        if (newValue == 0) { // if 0, show all annotation
            annotation = document.getElementsByClassName("annotation");
            for (var i = 0; i < 3; i++) {
                annotation[i].style.display = 'initial';
            }
        } else {
            annotation = document.getElementsByClassName("annotation");
            if (annotation[newValue - 1].style.display == 'none') {  // if hidden, re-display annotation
                annotation[newValue - 1].style.display = 'initial';
            } else {
                annotation[newValue - 1].style.display = 'none';  // hide annotation
            }
        }
    } else {
        //service portal logic
        if (newValue == 0) {  // if 0, show all annotation
            annotation = this.document.getElementsByClassName("help-tag");
            for (var j = 0; j < 3; j++) {
                annotation[j].style.display = 'initial';
            }
        } else {
            annotation = this.document.getElementsByClassName("help-tag");
            if (annotation[newValue - 1].style.display == 'none') {  // if hidden, re-display annotation
                annotation[newValue - 1].style.display = 'initial';
            } else {
                annotation[newValue - 1].style.display = 'none'; // hide annotation
            }
        }
    }
}

As I've mentioned above, this script uses DOM manipulation so "Isolate script" needs to be disabled.

find_real_file.png

Execution:

A. Service Portal

step 1: initial form. All annotations are displayed

find_real_file.png

step 2: Hide annotation 2.

find_real_file.png

B. UI

step 1: initial form. All annotations are displayed

find_real_file.png

step 2: Hide annotation 2.

find_real_file.png

View solution in original post

4 REPLIES 4

Nirmala5
Mega Contributor

Thank you for sharing the link.

But do you know how to get the ID of annotation

kris29
Tera Contributor

please have a look here

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Nirmala,

Annotation may be hidden using DOM manipulation. DOM manipulation is not recommended because it may stop working after ServiceNow is versioned up.

As an example, I've created a form like below with 3 fields each with annotation. The numeric scale on top is used to show or hide annotation. 0 implies show all annotation.

find_real_file.png

This is the client script used to hide and re-display annotation. Service Portal and UI have different logic and the first "if (window)" is used to determine if this is being called from Service Portal page or UI page.

In the Service Portal page, 'annotation = this.document.getElementsByClassName("help-tag");' is used to get all annotation elements. 'annotation[i]' reference's the ith annotation with i starting from 0. For example, annotation[1] will be the second annotation in the form. ".style.display == 'none'" will hide the annotation while ".style.display = 'initial'" will re-display it.

In the UI page, 'annotation = document.getElementsByClassName("annotation");' is used to get all annotation elements.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var annotation;
    if (window) {
        // ui logic
        if (newValue == 0) { // if 0, show all annotation
            annotation = document.getElementsByClassName("annotation");
            for (var i = 0; i < 3; i++) {
                annotation[i].style.display = 'initial';
            }
        } else {
            annotation = document.getElementsByClassName("annotation");
            if (annotation[newValue - 1].style.display == 'none') {  // if hidden, re-display annotation
                annotation[newValue - 1].style.display = 'initial';
            } else {
                annotation[newValue - 1].style.display = 'none';  // hide annotation
            }
        }
    } else {
        //service portal logic
        if (newValue == 0) {  // if 0, show all annotation
            annotation = this.document.getElementsByClassName("help-tag");
            for (var j = 0; j < 3; j++) {
                annotation[j].style.display = 'initial';
            }
        } else {
            annotation = this.document.getElementsByClassName("help-tag");
            if (annotation[newValue - 1].style.display == 'none') {  // if hidden, re-display annotation
                annotation[newValue - 1].style.display = 'initial';
            } else {
                annotation[newValue - 1].style.display = 'none'; // hide annotation
            }
        }
    }
}

As I've mentioned above, this script uses DOM manipulation so "Isolate script" needs to be disabled.

find_real_file.png

Execution:

A. Service Portal

step 1: initial form. All annotations are displayed

find_real_file.png

step 2: Hide annotation 2.

find_real_file.png

B. UI

step 1: initial form. All annotations are displayed

find_real_file.png

step 2: Hide annotation 2.

find_real_file.png