Skip to content

Commit 6a08214

Browse files
committed
Add a shared helper for lookup field case normalization
Nine trigger scripts across the compliance and SSU modules had each copied this loop verbatim. The LookupValidationHelper is passed in rather than created internally so callers keep it at script scope, where its cache of allowable values is read once per batch instead of once per row.
1 parent c5baffb commit 6a08214

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

LDK/resources/scripts/ldk/Utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ LDK.Server.Utils = new function(){
7373
//normalize to a javascript date object
7474
date = new Date(date.getTime());
7575
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
76+
},
77+
78+
/**
79+
* Replaces each named field's value with the case-normalized value from its lookup target, adding an error for any value the target does not contain.
80+
* The helper must be created at script scope: it caches each target's allowable values per instance, so creating one per call would re-read the target table for every row.
81+
*/
82+
normalizeLookupFields: function(helper, row, errors, lookupFields){
83+
for (var i=0;i<lookupFields.length;i++){
84+
var f = lookupFields[i];
85+
var val = row[f];
86+
if (!LABKEY.ExtAdapter.isEmpty(val)){
87+
var normalizedVal = helper.getLookupValue(val, f);
88+
89+
if (LABKEY.ExtAdapter.isEmpty(normalizedVal))
90+
errors[f] = 'Unknown value for field: ' + f + '. Value was: ' + val;
91+
else
92+
row[f] = normalizedVal;
93+
}
94+
}
7695
}
7796
}
7897
}

0 commit comments

Comments
 (0)