Catalog Client Script Error Message

carlocsa
Kilo Expert

Hi ServiceNow Gurus!

I have a client script that checks if 2 fields are empty. This is my script:

function onSubmit() {

var ogroup = g_form.getValue('u_owner_group');

var pnum = g_form.getValue('purchase_number');

if(g_form.getValue('state')=='3'){

if (ogroup=='undefined' || ogroup==''){

g_form.setMandatory('variables.u_owner_group',true);

g_form.addErrorMessage('Owner Group is a mandatory field.');

return false;

}

else if (pnum==''){

g_form.setMandatory('variables.purchase_number',true);

g_form.addErrorMessage('Purchase Number is a mandatory field.');

return false;

}

}

}

The script above works but I want to modify it because of this "error".

Example 1: I am raising a request and left owner group and purchase number empty. I save the request. The error is "Owner Group is a mandatory field". It does not display "Purchase Number is a mandatory field." error message.

Example 2: When I populate the Owner Group and save the request, "Owner Group is a mandatory field" does not disappear" and "Purchase Number is a mandatory field".

Please help!

Thank you very much.

Carlo

8 REPLIES 8

Guys, what is the reason why a reference field is returning 'undefined' when the field is empty/null?


Hi Pradeep,



The script looks absolutely fine. Just one doubt, shouldn't undefined be without quotes? As 'undefined' is a string.



>> if (ogroup=='undefined' || ogroup==''){


Right and actually speaking at client side it will always return blank so the below statement should be ok.


if (ogroup=='' && pnum=='')


Yes. agreed!