Client UI Scripts: trying to display a warning message in Incident form field

CesarV_
Tera Contributor

I am attempting to generate a warning message in the 'On hold reason' field when an incident form meets this criteria prior to saving:

 

  • Caller= Event Management
  • State= On Hold
  • On hold reason= Awaiting Caller

 

I'm new to SMV development work and struggle with scripting. I felt like the below code was going to work but it's not generating the message. I appreciate an feedback on this, details below.

 

Mock up of the intended functionality:

CesarV__0-1745430958661.jpeg

 

Client UI script settings: 

CesarV__1-1745431109890.png

 

 

Current code:

function onLoad() {
    // Check if the form is for a new or existing incident
    if (g_form.isNewRecord() || g_form.getValue('state') == '3') { // 3 = On Hold in ServiceNow
        // Add an onChange listener to the caller_id field
        g_form.addDecoration('caller_id', 'onChange', checkConditions);
        // Add an onChange listener to the state field
        g_form.addDecoration('state', 'onChange', checkConditions);
        // Add an onChange listener to the on_hold_reason field
        g_form.addDecoration('hold_reason', 'onChange', checkConditions);
       
        // Run the check on form load in case values are already set
        checkConditions();
    }
}

function checkConditions() {
    // Get field values
    var caller = g_form.getValue('caller_id');
    var state = g_form.getValue('state');
    var holdReason = g_form.getValue('hold_reason');
   
    // Check if caller contains "Event Management" (sys_user table reference)
    var callerName = '';
    if (caller) {
        var gr = new GlideRecord('sys_user');
        if (gr.get(caller)) {
            callerName = gr.getValue('name') || '';
        }
    }
   
    // State: On Hold = 3, Hold Reason: Awaiting Caller = 3 (default ServiceNow values)
    if (callerName.indexOf('Event Management') !== -1 &&
        state == '3' &&
        holdReason == '3') {
        // Display warning message in the on_hold_reason field
        g_form.showFieldMsg('hold_reason', 'Warning: Incident is Awaiting Caller for Event Management. Please select different On hold reason.', 'error');
    } else {
        // Clear any existing field message if conditions are not met
        g_form.hideFieldMsg('hold_reason');
    }
}

 

2 ACCEPTED SOLUTIONS

J Siva
Tera Sage

Hi @CesarV_ 
You can acheive this by creating one UI policy on the Incident table. PFB.
UI policy:

JSiva_1-1745467272124.png

If you want to show that warning message only on the field changes, then uncheck the "on load" check box.

JSiva_2-1745467344688.png

Else, if you want to show that whenever the condition statifies, then check the "On load" check box.

JSiva_3-1745467410899.png

 

UI policy script:

JSiva_4-1745467474898.png

function onCondition() {
    g_form.showErrorBox("hold_reason", "Warning: Incident is Awaiting Caller for Event Management. Please select different On hold reason");
}

 

Output:

JSiva_0-1745467229247.png

Regards,
Siva

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@CesarV_ 

you should create normal client script i.e. onSubmit on incident table and not catalog client script

function onSubmit() {
    // Get the values of the fields
    var caller = g_form.getValue('caller_id');
    var state = g_form.getValue('state').toString();
    var onHoldReason = g_form.getValue('hold_reason').toString();

    // Check if the conditions are met
    if (caller == 'eventManagementUserSysId' && state == '3' && onHoldReason == '1') {
        // Generate a warning message
    g_form.showFieldMsg("hold_reason", "Warning: Incident is Awaiting Caller for Event Management. Please select different On hold reason", "error");
        return false; // Prevent the form from being submitted
    }

    return true; // Allow the form to be submitted
}

OR

You can use UI Policy for this as per @J Siva 

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

View solution in original post

6 REPLIES 6

@CesarV_ 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

Done, thank you @Ankur Bawiskar!