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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Carlo,



Here is the updated 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=='' && pnum=='')


{


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



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


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



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



return false;



}



else 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;



}



}



}

































I have modified the script and it is now working.



function onSubmit() {



g_form.clearMessages();



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.');


}



else if (pnum==''){


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


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


}


return false;


}


}


Hello Carlo,



With the script you shared, is this "


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." working fine?


Yes, Pradeep!



Thanks,   Nitesh and Pradeep! Awesome!