Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ServiceNow business rule for server-side field validation based on form views.

This ServiceNow business rule provides comprehensive server-side validation for multiple form fields when users access specific form views. The script ensures data integrity by validating that critical fields contain expected values before allowing record submission, making it perfect for enforcing business rules and data consistency across your ServiceNow instance.

What This Script Does:

The business rule automatically validates multiple fields against predefined expected values when a specific form view is accessed. Key features include:

View-Based Validation: Only triggers when accessing a specified form view
Multiple Field Support: Validates multiple fields simultaneously with customizable criteria
Required Field Checking: Ensures mandatory fields are not empty or null
Value Validation: Confirms fields contain expected values according to business rules
User-Friendly Messaging: Provides clear, consolidated error messages explaining all validation failures
Server-Side Security: Performs validation on the server to prevent client-side bypassing
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(function executeRule(current, previous /*null when async*/ ) {
var TARGET_VIEW_NAME = 'your_target_view_name';
var FIELD_VALIDATIONS = [
{field: 'field1', expectedValue: 'value1', errorMsg: 'Field 1 validation failed'},
{field: 'field2', expectedValue: 'value2', errorMsg: 'Field 2 validation failed'},
{field: 'field3', expectedValue: 'value3', errorMsg: 'Field 3 validation failed'}
];

var gURI = "";
try {
gURI = gs.action.getGlideURI();
} catch(e) {
return;
}

var view_name = gURI.get('sysparm_view');

if (view_name == TARGET_VIEW_NAME) {
var validationErrors = [];

for (var i = 0; i < FIELD_VALIDATIONS.length; i++) {
var validation = FIELD_VALIDATIONS[i];
var fieldValue = current.getValue(validation.field);

if (gs.nil(fieldValue)) {
validationErrors.push(validation.field + ' is required');
continue;
}

if (fieldValue != validation.expectedValue) {
validationErrors.push(validation.errorMsg);
}
}

if (validationErrors.length > 0) {
gs.addErrorMessage(gs.getMessage("Validation failed: {0}", validationErrors.join(', ')));
}
}
})(current, previous);
Loading