checkbox mandatory in widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2022 01:33 AM
Can anyone please help me on the below issue.
I have created one widget and in the widget i have created the checkboxes like the below.
I have created the variable "this request is" custom with lable type and attached the widget. Need to make mandatory the widget so that if anyone out of the four box is checked the request can be submitted or else the request will not get submit.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2022 02:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2022 03:39 AM
Hi,
then you can use approach mentioned by Markus
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2022 02:46 AM
You need the following artifacts for this (i assumed that the checkbox configuration is stored as JSON):
Widget HTML:
<div>
<input type="checkbox" ng-model="checkboxConfig.checkbox1" ng-change="update()" />
<input type="checkbox" ng-model="checkboxConfig.checkbox2" ng-change="update()" />
</div>
Widget Client controller:
api.controller=function($scope) {
// handle 'Default value' and external sources
// e.g. a client script has g_form.setValue('our field', true)
$scope.$watch('page.field.value', function (newValue, oldValue, scope) {
$scope.checkboxConfig = JSON.parse(newValue);
});
// mandatoryness does not work OOTB with custom widgets!
// this is *only* for UI
$scope.page.field.mandatory = true;
$scope.page.field.mandatory_filled = function () {
var numChecked = 0;
for (var checkbox in $scope.checkboxConfig) {
if ($scope.checkboxConfig[checkbox]) {
numChecked++;
}
}
return numChecked > 0;
};
$scope.update = function () {
$scope.page.g_form.setValue($scope.page.field.variable_name, JSON.stringify($scope.checkboxConfig));
};
};
The mandatory handling however is just UI stuff, you still need a onSubmit Catalog Client Script, and i guess there is no way around that (unfortunately):
UI Type: All
Script:
function onSubmit() {
var checkboxes = JSON.parse(g_form.getValue('checkboxes'));
for (var checkbox in checkboxes) {
if (checkboxes[checkbox]) {
return true;
}
}
g_form.addErrorMessage('At least one checkbox has to be selected!');
return false;
}