Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Giving an error message on submit if any field remains empty on Catalog item.

Sam176
Tera Contributor

I tried to write a script to display error message if any fields are empty on submitting catalog item.

function onSubmit() {
if( g_form.getDisplayBox('company').value ==" '' || g_form.getDisplayBox('size').value ==" '' )
{
g_form.addErrorMessage('Please select all the fields');
}
}

But when I test, the error message gets displayed and immediately the page goes to submission confirmation page before the error is resolved.

I want to display the error message and stay on the current page till the error is resolved. After resolving the error, when I try to submit it should go to submission confirmation page.

How can I achieve this criteria?

.

9 REPLIES 9

Brian Lancaster
Kilo Patron

after your g_form.addErrorMessage add return false;

Hi Brian,

Thank you for the response. I have tried adding it.

Now, I am getting error message and I am able to stay on the current page.

But, I see that after resolving the error when I click on Order now button, it is not redirecting me to submission confirmation page. And also if the condition is false, item is not being able to submit.

I mean No Action is performed when I click on Order Now button in both the cases.

I have seen it happen a couple times this way but I was never able to figure out why.  Try to add return true as an else.

if( g_form.getDisplayBox('company').value ==" '' || g_form.getDisplayBox('size').value ==" '' )
{
g_form.addErrorMessage('Please select all the fields');
return false;
}
else{
return true;
}
}

Another thought is if you want all filed will in why not just make them all mandatory?

I tried it. It's still not working.

Yeah that makes sense, even I thought the same but, in this story I am not supposed to use UI Policies. I will try using setMandatory method in the Script.

Thanks.