Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

g_form.showFieldMsg() Disappears After Field Value Selection

PoojithaHasthi
Tera Contributor

We are using an onChange Client Script to display a g_form.showFieldMsg() message when certain conditions are met. The message is displayed correctly initially. However, once the user enters/selects a value in the field, the field message disappears automatically.

Our requirement is for the message to remain visible at all times as a dynamic hint whenever the condition is satisfied, even after the field value is selected or modified. Since a standard dynamic hint is not available for this use case, we are trying to use showFieldMsg().

Could you please advise:

  • Is this expected behavior of g_form.showFieldMsg()?
  • Is there a recommended approach to keep the message persistently visible while the condition remains true?
  • Are there any alternative solutions that can behave like a dynamic hint on the form?
3 REPLIES 3

Aditya_hublikar
Giga Sage

Hello @PoojithaHasthi ,

 

Refer this client script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    var fieldName = 'u_incident';
    var placeholder = "##placeholder##"
    if (isLoading || newValue === '') {
        g_form.showFieldMsg(fieldName, placeholder, 'info');
        return;
    }

    g_form.hideFieldMsg('fieldName', true);
    g_form.showFieldMsg(fieldName, placeholder, 'info');

    //Type appropriate comment here, and begin script below

}

 

Aditya_hublikar_0-1788498891826.png

Aditya_hublikar_1-1788498921753.png

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,
Aditya

PoojithaHasthi
Tera Contributor

In our implementation, the message is shown when:

  • Policy = FJ-GDSL-GD Security Policy (87bfefaa1bdb1150b3891065bb4bcb39)
  • Control Objective includes FJ-SECPL-AUP-02 (e7eafe5233fbba9412deaf5c2d5c7ba9)
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
g_form.showFieldMsg(
'valid_to',
'Use the GitHub Copilot license expiry date if it occurs before the maximum exception duration; otherwise use the maximum exception duration end date.',
'warning'
);
return;
}
 
var match = (g_form.getValue('policy') === '87bfefaa1bdb1150b3891065bb4bcb39' &&
g_form.getValue('control_objectives').split(',').indexOf('e7eafe5233fbba9412deaf5c2d5c7ba9') > -1);
 
g_form.hideFieldMsg('valid_to', true);
 
if (match) {
g_form.showFieldMsg(
'valid_to',
'Use the GitHub Copilot license expiry date if it occurs before the maximum exception duration; otherwise use the maximum exception duration end date.',
'warning',
true
);
}
}

 

However, when the condition matches, the message is displayed initially but disappears once the user clicks into or updates the Valid To field. Even if Valid To remains empty and the condition is still true, the message does not reappear.

Our expectation is that the message should remain visible as long as the Policy and Control Objective condition is satisfied. Is this expected behavior of showFieldMsg(), or is there a recommended OOB approach for a persistent field-level hint/message?

Ankur Bawiskar
Tera Patron

@PoojithaHasthi 

My thoughts

-> yes this is standard OOTB behavior

-> Why not use Form annotation and show a text just above or below that field which guides user about the message

-> Another approach is to below

a) keep existing onChange as it is and no change

b) Create another onChange on valid_to and add script and validation there also

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) return;

    var fieldName = 'valid_to';

    // Re-evaluate the same condition you use in the other onChange
    var policy = g_form.getValue('policy');
    var controlObjectives = g_form.getValue('control_objectives') || '';

    var match = (policy === '87bfefaa1bdb1150b3891065bb4bcb39' &&
                 controlObjectives.split(',').indexOf('e7eafe5233fbba9412deaf5c2d5c7ba9') > -1);

    // Always hide first to avoid duplicates
    g_form.hideFieldMsg(fieldName, true);

    if (match) {
        g_form.showFieldMsg(
            fieldName,
            'Use the GitHub Copilot license expiry date if it occurs before the maximum exception duration; otherwise use the maximum exception duration end date.',
            'warning'
        );
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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