g_form.getMissingFields() alternative in service portal?

Ahmmed Ali
Mega Sage

Hello All,

Hope all are doing great.

For catalog item in platform view, we can use g_form.getMissingFields() to get mandatory fields which are empty. But I want same functionality in portal, unfortunately above method is does not work in portal. Is there any other way to achieve same?

My use case: I have a button in portal and when I click that button, It should do next task only when there are no mandatory fields on the form which are empty.

 

Also When we try to redirect user to any other URL in catalog form (When user has already filled the form but did not submit), it will popup a alert for confirming to leave current page and data will be lost. How can we avoid the popup. the data will be auto-saved in my use case before redirecting user to another URL. Hence I do not want to show the alert.

 

Thanks

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali
1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

A quick example of a way to check for empty mandatory fields

function filterForMissingMandatoryFields(field){
		//check if field is mandatory
		if($scope.page.g_form.isMandatory(field)){
			//return if there is no value
			return !$scope.page.g_form.getValue(field);
		}
	}

	$scope.getMissingFields = function(){
		//get all fields
		var fieldList = $scope.page.g_form.getFieldNames();

		//return only mandatory fields that have no value
		return fieldList.filter(filterForMissingMandatoryFields)
	}

	//Then call this function when you want to check 
	//$scope.getMissingFields() //this returns an array of empty mandatory fields
	
	//of if calling from an ngClick just do this on the clickable element
	// ng-click="getMissingFields()"

If the return is empty after calling it, it means all mandatory fields are populated.

 

Hope this helps

View solution in original post

7 REPLIES 7

Satyanarayana C
ServiceNow Employee
ServiceNow Employee

Hi,

If you mark a field as mandatory on the catalog item, it will be auto detected in service portal. You need not do anything to handle mandatory fields separately.

Thanks

Hello,

Thank you for response.

Mandatory fields will have validation by default when user tries to submit the form.

 

But in my form, user clicks on button which is in custom widget(added through Macro type variable). When user clicks on button, first I need to check if there are any mandatory field which are yet to be filled, and then carry out button functionality.

 

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

ChrisBurks
Mega Sage

In the custom widget try using a combination of .getFieldNames(),  .isMandatory(), and finally .getValue().

$scope.page.g_form.getFieldNames() will provide you an array of the variables.

Iterate through the names passing each name into .isMandatory() to see if the variable is mandatory

If variable mandatory use .getValue() to see if it has a value.

 

Swapnil Soni1
Giga Guru

Hi,

g_form.getMissingFields():

  • This method returns an array of comma-separated names of all the fields that are mandatory and are not filled.
  • Usually this method wouldn't be much use to you, because submit () function will call this method to check if all the mandatory fields are filled or not.
  • What it Returns: Returns array of comma-separated names of all the fields that are mandatory otherwise an empty array If all the mandatory fields are filled.
  • Where to use: You can write this code at any client-side scripts like UI Policies or client scripts
  • Limitations: This method will not work on Service Portal but will work on Native UI for a catalog item.

for more info please check the link below

https://www.servicenowguru.com/system-ui/ui-macros/mandatory-fields-service-catalog-checkout/

 

Please mark correct or helpful if this works for you.

Thanks