> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Step 9. Configure business rules

> Add business rules to validate and normalize extracted field values — adjust doctor IDs with regex and check sick note validity with script rules.

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

## Adjusting the doctor's ID

Adjust the **Doctor ID** field properties to clean up OCR errors and unwanted characters from the pre-recognition results. After the field region is found, a regular expression in the field properties re-extracts the data and removes OCR noise.

<Steps>
  <Step title="Open the Doctor ID field settings">
    1. Click the skill name and 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.
  </Step>

  <Step title="Add a regular expression">
    1. In the **Value** section of the field properties, click the add icon next to the **Regular expression** option.
    2. Paste the following expression into 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**.
    </Note>
  </Step>

  <Step title="Save the changes">
    Click **Save** to close the **Regular Expression Editor**, then click **Save** again to close the field properties.
  </Step>
</Steps>

## Checking if the sick note is valid

A patient must receive a sick note within 3 days of the sickness start date. In field terms: the issue date must be no more than 3 days after the start date. Use a script rule to enforce this constraint.

<Steps>
  <Step title="Open the New Rule dialog">
    Click the add icon under the data form. The **New Rule** dialog opens.

    <Note>
      If you don't see the add icon, switch to the **Reference** section above the document image.
    </Note>
  </Step>

  <Step title="Configure the rule type and fields">
    1. Select **Advanced Script Rule** and click **Next**.
    2. Rename the rule to "Check validity".
    3. Select the "Start Date" and "Date" fields in the list of fields.
    4. Click **Next**.
  </Step>

  <Step title="Add the validity script">
    Paste the following script in the script editor:

    ```javascript theme={null}
    // Create variables for all the fields you're going to access
    var startField = Context.GetField("Start Date") ;
    var dateField = Context.GetField("Date");

    if (dateField && dateField.Value && startField && startField.Value)
    {
       var date = dateField.Value;
       var startDate = startField.Value;

       //Calculate the earliest possible start date
       date.setDate(date.getDate() - 3);

       if (startDate < date)
       {
           Context.CheckSucceeded = false;
           Context.ErrorMessage = "The sick note was issued too late";
       }
    }
    ```

    Click **Save** and examine how the rule works on different documents in the set. To check how the rule handles errors, enter test values in the fields on the data form manually — the rule re-applies each time you change the field value.
  </Step>
</Steps>

## Checking the sick leave duration

Calculate the sick leave duration from the start and end dates. If the document already specifies a duration, the rule verifies it matches; if not, the rule fills the duration field with the calculated value.

<Steps>
  <Step title="Open the New Rule dialog">
    Click the add icon under the data form to create a rule.
  </Step>

  <Step title="Configure the rule type and fields">
    1. Select **Advanced Script Rule** and click **Next**.
    2. Rename the rule to "Check duration".
    3. Select the "Start Date", "End Date", and "Duration" fields in the list of fields. Select the fields in both columns — the rule both reads field values and corrects them if necessary.
    4. Click **Next**.
  </Step>

  <Step title="Add the duration script">
    Paste the following script in the script editor:

    ```javascript theme={null}
    // 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());
       }
    }
    ```

    Click **Save** and examine how the rule works on different documents in the set. To check how the rule handles errors, enter test values in the fields on the data form manually — the rule re-applies each time you change the field value.
  </Step>
</Steps>

## What's next

<CardGroup cols={2}>
  <Card title="Step 10. Test and publish the skill" icon="arrow-right" href="/vantage/documentation/advanced-designer/tutorial/tutorial-step-10">
    Test the configured skill and publish it to the Vantage server.
  </Card>

  <Card title="Tutorial overview" icon="circle-info" href="/vantage/documentation/advanced-designer/tutorial/tutorial">
    Back to the tutorial introduction.
  </Card>
</CardGroup>
