Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

HTML Client Script for Displaying Caution Label in RITM Form - Need Help"

Service now11
Tera Contributor

Hi,

I’m trying to display a caution label at the top of an RITM form when the "Stage" field value is set to "waiting_for_approval". The goal is to ensure that the label appears at the top of the form so that users don’t accidentally fulfill or handle tickets that are not yet approved.

Requirements:

  • Display a warning message at the top of the RITM form when the "Stage" field has the value "waiting_for_approval".
  • Label content:
    CAUTION
    "Ticket is awaiting approval per defined flow. Do NOT fulfill/handle unless manual approval has been achieved."
  • The label should have white text on a red background.
  • "Stage" field label should also be highlighted in red.
  • The label should only be shown when the "Stage" value is "Waiting for Approval"
  • Also red dot flagging in list views

Issues I’ve Encountered: I tried using  .body and .polaris as possible selectors for the form’s main content, but I still don't see the label.

Client Script: 

function onLoad() {
var stage = g_form.getValue('stage');
if (stage == 'waiting_for_approval') { // Assuming internal value for "Waiting for Approval" is '1'
// Create and display the caution label at the top of the form
var cautionLabel = document.createElement('div');
cautionLabel.id = 'caution-label';
cautionLabel.style.backgroundColor = 'red';
cautionLabel.style.color = 'white';
cautionLabel.style.textAlign = 'center';
cautionLabel.style.padding = '10px';
cautionLabel.style.fontWeight = 'bold';
cautionLabel.innerText = 'CAUTION: Ticket is awaiting approval. Do NOT fulfill/handle unless approved manually!';

// Insert caution label at the top of the form's body section
var formBody = document.querySelector('.body'); 
if (formBody) {
formBody.insertBefore(cautionLabel, formBody.firstChild);
}

// Highlight the STAGE field label with a red background
var stageLabel = document.querySelector('label[for="stage"]');
if (stageLabel) {
stageLabel.style.backgroundColor = 'red';
stageLabel.style.color = 'white';
}
}
}

 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@Service now11 

why are you using DOM manipulation?

why not use g_form.addInfoMessage()

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

I am not able to achieve the following,

 

  • The label should have white text on a red background.
  • "Stage" field label should also be highlighted in red.
  • Also red dot flagging in list views

Client script: 

function onLoad() {
    // Check if the 'stage' field is "Waiting for Approval"
    var stage = g_form.getValue('stage');
        if (stage === 'waiting_for_approval') {
          g_form.addInfoMessage('CAUTION: Ticket is awaiting approval. Do NOT fulfill/handle unless approved manually!.');
        var stageLabel = g_form.getControl('stage');
        if (stageLabel) {
            stageLabel.style.backgroundColor = 'red';
            stageLabel.style.color = 'white';
        }
    }
}

Hello @Service now11,

Can you try by using getDisplayBox

g_form.getDisplayBox('fieldname'). style == 'red';

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Shruti

Hello @Service now11,
Can you try by using getDisplayBox

g_form.getDisplayBox('fieldname'). style == 'red';

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Shruti