Skip to main content
Here are some sample scripts for real-life situations.

Checking for rule errors in a document

The script below checks whether there are any rule errors in the transaction documents. If at least one transaction document contains rule errors, the script returns true. Otherwise, the script returns false.
function hasRuleErrors() {
    for (var i = 0; i < Context.Transaction.Documents.length; i++) {
        var document = Context.Transaction.Documents[i];
        if (document.RuleErrors.length > 0 || document.IsUnknownSkill === true || document.HasSuspiciousSymbols === true)
            return true;
    }
    return false;
}
hasRuleErrors();

Checking whether manual review is required

The following script checks the transaction documents for errors. If at least one transaction document contains any of the following:
  • Rule errors
  • An uncertain document type
  • Uncertainly recognized fields or field characters
The script returns true and sends the documents to manual review. Otherwise, the script returns false and sends the documents to the export step.
function needManualReview() {
    for (var i = 0; i < Context.Transaction.Documents.length; i++) {
        var document = Context.Transaction.Documents[i];
        if (needManualReviewForDocument(document))
            return true;
    }
    return false;
}

function needManualReviewForDocument(document) {
    // Rule errors exist
    if (document.RuleErrors.length > 0)
        return true;
    // Document type is not confident
    if (!document.IsResultClassConfident)
        return true;
    // Some fields are suspicious
    for (var i = 0; i < document.Fields.length; i++) {
        var field = document.Fields[i];
        if (field.IsSuspicious || containSuspiciousFields(field))
            return true;
    }
    return false;
}

function containSuspiciousFields(field) {
    // Check all children for suspicious field
    if (field.Children) {
        for (var i = 0; i < field.Children.length; i++) {
            var childField = field.Children[i];
            if (childField.IsSuspicious || containSuspiciousFields(childField))
                return true;
        }
    }
    // Check all instances for suspicious field
    if (field.Instances) {
        for (var i = 0; i < field.Instances.length; i++) {
            var instanceField = field.Instances[i];
            if (instanceField.IsSuspicious || containSuspiciousFields(instanceField))
                return true;
        }
    }

    return false;
}
needManualReview();