Skip to main content
Having configured the Extraction Rules activities, we can add business rules to validate and normalize the field values.

Adjusting the doctor’s ID

First, we’re going to adjust the “Doctor ID” field properties. The Extraction Rules activity works with pre-recognition results which may contain OCR errors and unnecessary characters. Having found a field region, we can additionally adjust the field value by specifying a regular expression in the fields properties. The data will be re-extracted, and OCR errors will be eliminated.
  1. Click on the skill name and then go to the Fields tab.
  2. Expand the “Doctor” group and open the “Doctor ID” field settings by clicking the settings icon next to it.
  3. In the Value section of the field properties, click the add icon next to the Regular expression option.
  4. Paste the following expression to the Regular Expression Editor: [0-9]{1}[\/.-]{1}[0-9]{5}[\/.-]{1}[0-9]{2}[\/.-]{1}[0-9]{3}
Note: This editor uses common notation that differs from the notation used in the Extraction Rules Activity Editor. For more information, click Syntax help in the Regular Expression Editor.
  1. Click Save to close the Regular Expression Editor and then click Save to close the field properties.

Checking if the sick note is valid

The patient should get the sick note within 3 days after they first got sick. This means that the issue date must be no later than 3 days after the start date of the sickness. To check this condition we need to set up a script rule
  1. Click the add icon under the data form. The New Rule dialog will open.
Note: If you don’t see the add icon, switch to the Reference section above the document image.
  1. Select Advanced Script Rule and click Next.
  2. Rename the rule to “Check validity”.
  3. Select the “Date”, “Start Date”, and “End Date” fields in the list of fields.
  4. Click Next.
  5. Paste the following script in the script editor:
// Create variables for all the fields you're going to access
var dateField = Context.GetField("Date");
var startField = Context.GetField("Start Date");
var endField = Context.GetField("End Date");

var issueDate = dateField.Value;
var startDate = startField.Value;

//Check if the "Start Date" field was found on the document
if (startDate && issueDate)
{
   //Check if the issue date is not more than 3 days after the start date
    if ((issueDate.getTime() - startDate.getTime()) / 3600000 / 24 > 3)
    {
        Context.CheckSucceeded = false;
        Context.ErrorMessage = "The sick note was issued too late";
    }
}
  1. Click Save and examine how the rule works on different documents in the set. To check how the rule handles errors, you can enter test values in the fields on the data form manually. The rule will be re-applied each time you change the field value.

Checking the sick leave duration

We’re going to calculate the sick leave duration using the start and end sickness dates. If the document specifies a duration, we’ll check if it is equal to the calculated duration. If the document doesn’t contain a duration, we’ll write the calculated value into the corresponding field.
  1. Click the add icon under the data form to create a rule.
  2. Select Advanced Script Rule and click Next.
  3. Rename the rule to “Check duration”.
  4. Select the “Start Date”, “End Date”, and “Duration” fields in the list of fields. It’s important to select the fields in both columns since we aren’t going to only read field values, but also correct them if necessary.
  5. Click Next.
  6. Paste the following script in the script editor:
// Create variables for all the fields you're going to access
var startField = Context.GetField("Start Date") ;
var endField = Context.GetField("End Date");
var durationField = Context.GetField("Duration");

var startDate = startField.Value;
var endDate = endField.Value;

//Check if the "Start Date" and "End Date" fields were found on the document
if (endField && endDate && startField && startDate)
{
   //Calculate the sick leave duration
   var length = (1 + (endDate.getTime() - startDate.getTime()) / 3600000 / 24);

   //If the duration field was not found or could not be parsed as a number, pass the calculated value to the field
   if (!durationField.Value)
       durationField.Value = length; 

   //If the duration field was found, compare its value with the calculated duration
   else if (durationField.Value != length)
   {
       Context.CheckSucceeded = false;
       Context.ErrorMessage = "The value of the \"Duration\" field does not match the actual sick leave duration";
       durationField.AddSuggestion(length.toString());
   }
}
  1. Click Save and examine how the rule works on different documents in the set. To check how the rule handles errors, you can enter test values in the fields on the data form manually. The rule will be re-applied each time you change the field value.