How to validate mandatory fields in a widget for catalog items in Service Portal

patricklatella
Mega Sage

Hi all,

I've created a widget that presents a button on a record producer in Service Portal.  When this button is pressed a modal window opens that presents variables and display values from some of the fields on the form.

Before opening the modal, if there are any mandatory fields not filled in, I want an alert to open first to say there are mandatory fields to fill in (much like OOTB Submit button will do).

Here's the script that opens the modal, and collects values to pass to the HTML.  I've tried g_form.hasMissingFields(), but I keep getting an error that this is not function.  Any ideas?  thanks!

 

c.openModal = function() {

var arr = g_form.getMissingFields();
alert("The ID s of fields that are mandatory are not filled : " + arr);//this is part that's not working

$scope.data.reqName = g_form.getDisplayValue('u_requester_name');
$scope.data.dep = g_form.getDisplayValue('u_department');
$scope.data.phone = g_form.getDisplayValue('u_phone_number');
$scope.data.hpc = g_form.getDisplayValue('u_hpc');
$scope.data.date = g_form.getDisplayValue('u_date_of_notary');
$scope.data.delDoc = g_form.getDisplayValue('u_delivery_of_documents')


c.modalInstance = $uibModal.open({
templateUrl: 'notary-print.html',
scope: $scope
}

 

 
1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

This link may assist you: https://www.servicenowguru.com/system-ui/ui-macros/mandatory-fields-service-catalog-checkout/

Otherwise, one way to do this would be to to have your UI action button check through the fields to see if anything is missing, much like:

if (g_form.getValue('fieldname') == '' || g_form.getValue('fieldname') == '') {

alert("Please fill in the mandatory fields first before continuing");

}

...something like that.

Please mark reply as Helpful/Correct. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

This link may assist you: https://www.servicenowguru.com/system-ui/ui-macros/mandatory-fields-service-catalog-checkout/

Otherwise, one way to do this would be to to have your UI action button check through the fields to see if anything is missing, much like:

if (g_form.getValue('fieldname') == '' || g_form.getValue('fieldname') == '') {

alert("Please fill in the mandatory fields first before continuing");

}

...something like that.

Please mark reply as Helpful/Correct. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

thanks for the link, Mark's script used "gel" which I don't believe will work for my use since this is for Service Portal.

I did come up with this script to look at one particular field and it works...so now I need to build an array that looks collects all the fields, cycles through them and if it finds any that are still mandatory stop the script.

so this here works for the one field.

var phone = g_form.isMandatory('u_phone_number');

if(g_form.isMandatory('u_phone_number') == true){

alert('Please complete all mandatory fields before clicking "Print".');

return;
}

 

 

Yep...or the way I mentioned would work too. I wouldn't waste time setting variables if you aren't going to use them, haha. Just to cut it down a little.

Anyways, seems you're getting along.

If you end up using my method or found it helpful, please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

I actually think yours might work better, because I'm seeing that isMandatory() does not return false if a mandatory field is filled in with a value.