Populate info message when location field is empty.

Ritika22
Tera Contributor

Hi Team,

 

I have a reference field named asset tag which refers hardware table in a catalog item. Also i have an another field called location which is autopopulated according to the location value in hardware table with respect to corresponding asset tag in hardware table. I want populate a message that location field is empty when location field is empty. I have written a catalog client script but it is working in both scenereo that it is showing message both when location field is empty as well as when location field is not empty. 

 

Please take a look on the pics. for more info.

 

Thank you

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ritika22 

you should write onChang catalog client script on Asset tag variable and populate location and if location empty then show message

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

    g_form.clearMessages();

    if (newValue == '') {
        g_form.clearValue('location'); // location variable name here
    }

    var ref = g_form.getReference('asset_tag', callBackMethod); // requestedFor variable name
}

function callBackMethod(ref) {
    if (ref.location)
        g_form.setValue('location', ref.location); // location variable name here
    else
        g_form.addInfoMessage('Location empty please fill');
}

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

3 REPLIES 3

Anand Kumar P
Giga Patron
Giga Patron

Hi @Ritika22 ,

 

Try below script 

 

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

    if (isLoading) {

        return;

    }

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

    if (!lct) {

        g_form.addErrorMessage('Location is empty please fill it.');

    } else {

        g_form.clearMessages();

    }

}

 

 

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

Omkar Mone
Mega Sage

Hello @Ritika22 ,

 

If your client script is on location field change, then just do below - 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
		g_form.addInfoMessage("No value");
        return;
    }
    g_form.addInfoMessage("value");

    //Type appropriate comment here, and begin script below

}

 

Let me know if this worked.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ritika22 

you should write onChang catalog client script on Asset tag variable and populate location and if location empty then show message

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

    g_form.clearMessages();

    if (newValue == '') {
        g_form.clearValue('location'); // location variable name here
    }

    var ref = g_form.getReference('asset_tag', callBackMethod); // requestedFor variable name
}

function callBackMethod(ref) {
    if (ref.location)
        g_form.setValue('location', ref.location); // location variable name here
    else
        g_form.addInfoMessage('Location empty please fill');
}

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