Populate message when variable value is empty.

b__BiswabhusanD
Tera Contributor

Hi

There is a catlog item , In catlog item thre is two varibale "asset tag" type is refence and "location" type is refence.

once asset tag is choce then location is autopopulate.

 

my requirement is

When "location" varibale value is empty then showing error massage, if varibale contain any value then message is not showing.

 

i write below onchange client script but not working properly please modifay this script.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var abc = g_form.getValue("location");
    if (abc == "" || !abc) {
        g_form.addErrorMessage('Please select location value');

    } else {
        g_form.clearMessages();
    }
}
 
2 REPLIES 2

Anand Kumar P
Giga Patron
Giga Patron

Hi @b__BiswabhusanD ,

Use below script it will work, your script not working because when you are using clear messages need to remove newValue in script.

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading) {

        return;

    }

    var locationValue = g_form.getValue('location');

    if (!locationValue) {

        g_form.addErrorMessage('Please select a location.');

    } else {

        g_form.clearMessages();

    }

}

 

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Ankur Bawiskar
Tera Patron
Tera Patron

@b__BiswabhusanD 

why not make location variable as mandatory and you won't require any script?

update script as this

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

    if (newValue == '') {
        g_form.addErrorMessage('Please select location value');
    } else {
        g_form.clearMessages();
    }
}

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