Skip to content

Commit 68f2802

Browse files
Add a shared helper for lookup field case normalization (#310)
## Rationale Eleven trigger scripts across the EHR compliance and ONPRC modules had each copied the same loop for case-normalizing lookup values on write, so this adds the single shared implementation they can call instead. The helper takes the LookupValidationHelper as an argument rather than constructing one, because that class caches each lookup target's allowable values per instance: callers keep creating it at script scope, where the target table is read once per batch instead of once per row. ## Related Pull Requests - LabKey/ehrModules#1199 — converts the EHR compliance scripts to this helper. - LabKey/onprcEHRModules#1885 — converts the ONPRC scripts. This must merge and deploy first. A consumer script that requires the new function against an un-updated LDK fails at script compile time. ## Changes - Adds `LDK.Server.Utils.normalizeLookupFields`, which replaces each named field's value with the canonically-cased value from its lookup target and reports a field error for any value the target does not contain. - Documents why the validation helper is a parameter, since creating one per call would silently turn a per-batch read of each lookup target into a per-row read. ## Tasks - [x] Claude Code Review - [x] Code Review
1 parent c5baffb commit 68f2802

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)