Clean a Zone Value and Write It to a Field¶
An accounts payable team extracts the invoice total with an OCR action, but the raw zone text arrives as "$ 1,234.5O", "(250.00)" or "1 234.50 USD". The ERP that receives the value needs a plain number with two decimals. A Custom Action in the same rule reads the zone text, normalizes it, and replaces the raw value. Values it cannot read safely are flagged for review instead of guessed.
At a glance
- Extension point Custom Action (page action), after an OCR action
- You need Index field
Invoice Total(exact name); optional Index fieldInvoice Total Needs Review; a page rule with an OCR action whose Property: isInvoice Total; permission Use This App - Teaches Reading
state.OcrCache, winning a field from another action with confidence 1, and returning 0 for a value you cannot trust
Set it up¶
- In the Intuitive AI app, open the subclass.
- Open the page rule that finds the total. A typical rule has a Regex Anchor on the label "Invoice Total" and an OCR action on the zone next to it.
- In the OCR action ("Extract from OCR Snippet"), set Property: to
Invoice Total. - Select Add Action: › Custom Action, and paste the script. Keep the Custom Action after the OCR action in the list.
- Save the subclass.
Amounts are read with a period for decimals, as on US documents. A comma followed by exactly two digits at the end is also read as decimals.
The script¶
(function () {
var FIELD = "Invoice Total";
var REVIEW_FIELD = "Invoice Total Needs Review";
if (!state.OcrCache.ContainsKey(FIELD)) {
result.SetResult(0); // The OCR action did not run or is listed after this action.
return;
}
var raw = String(state.OcrCache.Get(FIELD).Text || "");
var cleaned = normalizeAmount(raw);
if (cleaned === null) {
// Leave the raw OCR value in the field and flag it.
result.SetDocumentMetadata(REVIEW_FIELD, "true");
result.SetResult(0);
return;
}
result.SetDocumentMetadata(FIELD, cleaned);
result.SetDocumentMetadata(REVIEW_FIELD, "false");
result.SetResult(1);
function normalizeAmount(text) {
var s = text.replace(/\s+/g, " ").trim();
if (s.length === 0) { return null; }
var negative = /^\(.*\)$/.test(s) || /-\s*$/.test(s) || /^-/.test(s) || /\bCR\b/i.test(s);
// Fix common OCR letter-for-digit swaps, only inside runs of digits.
s = s.replace(/[0-9OoIlSB][0-9OoIlSB.,\s]*[0-9OoIlSB]/g, function (run) {
return run.replace(/[Oo]/g, "0").replace(/[Il]/g, "1").replace(/S/g, "5").replace(/B/g, "8");
});
// Keep digits and separators only.
s = s.replace(/[^0-9.,]/g, "");
if (s.length === 0) { return null; }
// A comma after the last period (as in 1.234,50) is ambiguous here.
if (s.indexOf(".") !== -1 && s.lastIndexOf(",") > s.lastIndexOf(".")) { return null; }
// A comma followed by exactly two digits at the end is a decimal comma.
if (/,[0-9]{2}$/.test(s) && s.indexOf(".") === -1) {
s = s.replace(/,(?=[0-9]{2}$)/, ".");
}
s = s.replace(/,/g, "");
// More than one period means the text is not a single amount.
if ((s.match(/\./g) || []).length > 1) { return null; }
var value = parseFloat(s);
if (isNaN(value)) { return null; }
if (negative) { value = -value; }
return value.toFixed(2);
}
})();
How it works¶
- The OCR action stores its zone OCR in
state.OcrCacheunder its Property name. The Custom Action, listed after it in the same rule, reads that text. - Both actions write
Invoice Total. The engine keeps the value from the action with the higher confidence; on a tie, the custom action wins. The script callsSetResult(1)for a clean value, so its value replaces the raw text. - For a value it cannot read safely, the script returns 0. The OCR action's raw value then stays in the field, and the review flag is set.
- The value is passed as a string from
toFixed(2), so the field always has two decimals.
| Zone text | Written value |
|---|---|
$ 1,234.5O |
1234.50 |
(250.00) |
-250.00 |
1 234.50 USD |
1234.50 |
1.234,50 |
Not written; review flag set |
| empty | Not written; review flag set |
Change normalizeAmount if your documents use a decimal comma. Do not try to guess both conventions in one script.
Test it¶
- Add three or four training documents with messy totals to the subclass.
- Open the class and select Test. Upload the documents and select Start Test.
- In Detailed Test Results, check Extraction Results for
Invoice Total, and the Action Confidence of the Custom Action.
You should see
Invoice Total as a plain number with two decimals and an Action Confidence of 1 for each clean total, and Action Confidence 0 with Invoice Total Needs Review set to true for each ambiguous one.
What can go wrong¶
Watch out
- The field still shows the raw text. The Custom Action is listed before the OCR action, or it is in a different rule. The cache belongs to one rule on one page.
- The value has no decimals. You passed a number. Pass the string from
toFixed(2). - A letter in a vendor code became a digit. The swap step only touches runs that start and end with digit-like characters. If the zone also holds codes, tighten the OCR action's RegEx: so the zone text is only the amount.
- The OCR value wins. Check that the Custom Action called
SetResult(1)and wrote the same field name, spelled exactly the same.