Validate business data before passing it on

The sales system API expects a three-letter country code. You remember seeing a longer code in the raw data when reviewing the data.

Re-quoting a quote from an earlier chapter:

A business exception can be caused by invalid data, for example. Such an exception might need manual handling since retrying will not help if the data is invalid.

You decide to validate the traffic data before passing it to the sales system API. If the robot spots invalid data, it can flag the work item in question as a business exception. Those exceptions can be managed in Control Room. An action might be correcting the data manually or marking the work item complete (essentially ignoring it).

One simple validation you decide to implement is checking that the country code is indeed three letters long. Shorter or longer country code will be considered invalid.

You will need the RPA.JSON library for reading the traffic data JSON. Since the same library is also needed in the producer, it is time to move it to the shared.robot file so that you don't need to duplicate the library import:


*** Settings ***
Documentation       Inhuman Insurance, Inc. Artificial Intelligence System robot.
...                 Shared settings and code.

Library             RPA.JSON
Library             RPA.Robocorp.WorkItems

*** Variables ***
${WORK_ITEM_NAME}=      traffic_data


*** Settings ***
Documentation       Inhuman Insurance, Inc. Artificial Intelligence System robot.
...                 Consumes traffic data work items.

Resource            shared.robot

*** Tasks ***
Consume traffic data work items
    For Each Input Work Item    Process traffic data

*** Keywords ***
Process traffic data
    ${payload}=    Get Work Item Payload
    ${traffic_data}=    Set Variable    ${payload}[${WORK_ITEM_NAME}]
    ${valid}=    Validate traffic data    ${traffic_data}

Validate traffic data
    [Arguments]    ${traffic_data}
    ${country}=    Get Value From Json    ${traffic_data}    $.country
    ${valid}=    Evaluate    len("${country}") == 3
    RETURN    ${valid}
  • The Validate traffic data keyword encapsulates the logic and returns a boolean (True or False).
  • The Get Value From Json keyword gets the value of the country property - the supposedly three-letter country code.
  • The Evaluate keyword evaluates Python expressions - in this case: len("${country}") == 3 that returns True if the condition is true and False if not.

Great! Now you can check if the traffic data is valid before sending it to the sales system API.