Skip to content

Commit b551e42

Browse files
Create fieldValidationinBR.js
1 parent f131127 commit b551e42

File tree

1 file changed

+39
-0
lines changed
  • Server-Side Components/Business Rules/Field Validation based on form view in Server side

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(function executeRule(current, previous /*null when async*/ ) {
2+
var TARGET_VIEW_NAME = 'your_target_view_name';
3+
var FIELD_VALIDATIONS = [
4+
{field: 'field1', expectedValue: 'value1', errorMsg: 'Field 1 validation failed'},
5+
{field: 'field2', expectedValue: 'value2', errorMsg: 'Field 2 validation failed'},
6+
{field: 'field3', expectedValue: 'value3', errorMsg: 'Field 3 validation failed'}
7+
];
8+
9+
var gURI = "";
10+
try {
11+
gURI = gs.action.getGlideURI();
12+
} catch(e) {
13+
return;
14+
}
15+
16+
var view_name = gURI.get('sysparm_view');
17+
18+
if (view_name == TARGET_VIEW_NAME) {
19+
var validationErrors = [];
20+
21+
for (var i = 0; i < FIELD_VALIDATIONS.length; i++) {
22+
var validation = FIELD_VALIDATIONS[i];
23+
var fieldValue = current.getValue(validation.field);
24+
25+
if (gs.nil(fieldValue)) {
26+
validationErrors.push(validation.field + ' is required');
27+
continue;
28+
}
29+
30+
if (fieldValue != validation.expectedValue) {
31+
validationErrors.push(validation.errorMsg);
32+
}
33+
}
34+
35+
if (validationErrors.length > 0) {
36+
gs.addErrorMessage(gs.getMessage("Validation failed: {0}", validationErrors.join(', ')));
37+
}
38+
}
39+
})(current, previous);

0 commit comments

Comments
 (0)