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

Populate the the field info message

DeepikaR1661877
Tera Contributor

Hi All,

I have one requirement in incident form, when the source is "ABC" then need to select the approve field only 2 entries at the same time need to populate the field info message also. here i had done some coding , in client script -->on change, code is working fine i can't add more that two members, but the field message will not display because of the i m setting this line--->g_form.setValue('u_approvers', oldValue);

Now i need to populate the field messge and also only add the two entries , any one suggestion me how to add this?

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var source = g_form.getValue('u_source');
if (source === 'abc') {
var approvers = g_form.getValue('u_approvers').split(',');
if (approvers.length > 2) {
g_form.showFieldMsg("u_approvers", ('You can only select up to 2 approvers.'));
g_form.setValue('u_approvers', oldValue);
}
}
}

4 REPLIES 4

Satishkumar B
Giga Sage

@DeepikaR1661877 Remove the brackets "g_form.showFieldMsg('u_approvers', 'You can only select up to 2 approvers.');"& try below code.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var source = g_form.getValue('u_source');
    if (source == 'abc') {
        var approvers = g_form.getValue('u_approvers').split(',');
        if (approvers.length > 2) {
            g_form.showFieldMsg('u_approvers', 'You can only select up to 2 approvers.');
            g_form.setValue('u_approvers', oldValue);
        }
    }
}

 

 

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution ✅!! If this helps you to understand.

…………………………………………........................................................................................

Sid_Takali
Kilo Patron

Hi @DeepikaR1661877 Try below code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var source = g_form.getValue('u_source');
    if (source === 'abc') {
        var approvers = g_form.getValue('u_approvers').split(',');
        if (approvers.length > 2) {
            g_form.showFieldMsg('u_approvers', 'You can only select up to 2 approvers.', 'info');
            g_form.setValue('u_approvers', oldValue);
        } else {
            g_form.hideFieldMsg('u_approvers');
        }
    }
}

Ankur Bawiskar
Tera Patron

@DeepikaR1661877 

only minor mistake in your code; just correct that and it will work

g_form.showFieldMsg("u_approvers", 'You can only select up to 2 approvers.'); // the message should not be wrapped within round brackets

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

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

swathisarang98
Giga Sage

Hi @DeepikaR1661877 ,

 

Remove the brackets and change the order keep the field message after setValue as below,

 

if (isLoading || newValue === '') {
return;
}
var source = g_form.getValue('u_source');
if (source === 'abc') {
var approvers = g_form.getValue('u_approvers').split(',');
if (approvers.length > 2) {

g_form.setValue('u_approvers', oldValue);
g_form.showFieldMsg("u_approvers", 'You can only select up to 2 approvers.');
}
}
}

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang