From 68f28028ec2d379ab0caf1430b8b6abdfeae46e4 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Thu, 10 Sep 2026 08:22:45 -0700 Subject: [PATCH] Add a shared helper for lookup field case normalization (#310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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 - https://github.com/LabKey/ehrModules/pull/1199 — converts the EHR compliance scripts to this helper. - https://github.com/LabKey/onprcEHRModules/pull/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 --- LDK/resources/scripts/ldk/Utils.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/LDK/resources/scripts/ldk/Utils.js b/LDK/resources/scripts/ldk/Utils.js index e67bc056..00cd804f 100644 --- a/LDK/resources/scripts/ldk/Utils.js +++ b/LDK/resources/scripts/ldk/Utils.js @@ -73,6 +73,25 @@ LDK.Server.Utils = new function(){ //normalize to a javascript date object date = new Date(date.getTime()); return new Date(date.getFullYear(), date.getMonth(), date.getDate()); + }, + + /** + * 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. + * 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. + */ + normalizeLookupFields: function(helper, row, errors, lookupFields){ + for (var i=0;i