Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

Loop the work items in the consumer robot

You decide to start by implementing a simple consumer that gets the work items produced by the producer robot but does not do anything with them yet:

Small steps!

consumer.robot:

*** Settings *** Documentation Inhuman Insurance, Inc. Artificial Intelligence System robot. ... Consumes traffic data work items. Library RPA.Robocorp.WorkItems *** Tasks *** Consume traffic data work items For Each Input Work Item Process traffic data *** Keywords *** Process traffic data ${payload}= Get Work Item Payload
  • The For Each Input Work Item provides a convenient way for looping all the available work items. It takes a keyword as an argument. ("Hey, work item library! For each work item you find, call this keyword. Thanks!")
  • The Get Work Item Payload keyword gets the payload from the currently loaded work item. You don't have to manually loop or load the work items since the For Each Input Work Item keyword takes care of that automatically.

This time you run the consumer task and select one of the outputs generated by the producer robot as an input for this consumer robot.

The log shows each work item payload:

${payload} = {'traffic_data': {'country': 'VCT', 'year': 2011, 'rate': 3.69293}}

The sales system API expects only the data currently contained inside the traffic_data property. You get the relevant data out of the payload:

consumer.robot:

*** Settings *** Documentation Inhuman Insurance, Inc. Artificial Intelligence System robot. ... Consumes traffic data work items. Library RPA.Robocorp.WorkItems *** 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}[traffic_data]

Now you have the traffic data in a format that the sales system API should accept:

${traffic_data} = {'country': 'VCT', 'year': 2011, 'rate': 3.69293}

DRY - Don't Repeat Yourself - library imports and variables

To use the work item functionality, you imported the RPA.Robocorp.WorkItems. Now you have the same library import on both the producer and the consumer robot. You also use the same work item variable name (traffic_data) in two places.

You decide to take the shared.robot into good use and move the library import and the work item variable name there:

shared.robot:

*** Settings *** Documentation Inhuman Insurance, Inc. Artificial Intelligence System robot. ... Shared settings and code. Library RPA.Robocorp.WorkItems *** Variables *** ${WORK_ITEM_NAME}= traffic_data

Next, you remove the RPA.Robocorp.WorkItems library import from both the producer.robot and the consumer.robot and import the shared.robot as a resource instead.

Finally, you replace the traffic_data instances with ${WORK_ITEM_NAME}. Very DRY!

producer.robot:

*** Settings *** Documentation Inhuman Insurance, Inc. Artificial Intelligence System robot. ... Produces traffic data work items. Library Collections Library RPA.HTTP Library RPA.JSON Library RPA.Tables Resource shared.robot *** Variables *** ${TRAFFIC_JSON_FILE_PATH}= ${OUTPUT_DIR}${/}traffic.json # JSON data keys: ${COUNTRY_KEY}= SpatialDim ${GENDER_KEY}= Dim1 ${RATE_KEY}= NumericValue ${YEAR_KEY}= TimeDim *** Tasks *** Produce traffic data work items Download traffic data ${traffic_data}= Load traffic data as table ${filtered_data}= Filter and sort traffic data ${traffic_data} ${filtered_data}= Get latest data by country ${filtered_data} ${payloads}= Create work item payloads ${filtered_data} Save work item payloads ${payloads} *** Keywords *** Download traffic data Download ... https://github.com/robocorp/inhuman-insurance-inc/raw/main/RS_198.json ... ${TRAFFIC_JSON_FILE_PATH} ... overwrite=True Load traffic data as table ${json}= Load JSON from file ${TRAFFIC_JSON_FILE_PATH} ${table}= Create Table ${json}[value] RETURN ${table} Filter and sort traffic data [Arguments] ${table} ${max_rate}= Set Variable ${5.0} ${both_genders}= Set Variable BTSX Filter Table By Column ${table} ${RATE_KEY} < ${max_rate} Filter Table By Column ${table} ${GENDER_KEY} == ${both_genders} Sort Table By Column ${table} ${YEAR_KEY} False RETURN ${table} Get latest data by country [Arguments] ${table} ${table}= Group Table By Column ${table} ${COUNTRY_KEY} ${latest_data_by_country}= Create List FOR ${group} IN @{table} ${first_row}= Pop Table Row ${group} Append To List ${latest_data_by_country} ${first_row} END RETURN ${latest_data_by_country} Create work item payloads [Arguments] ${traffic_data} ${payloads}= Create List FOR ${row} IN @{traffic_data} ${payload}= ... Create Dictionary ... country=${row}[${COUNTRY_KEY}] ... year=${row}[${YEAR_KEY}] ... rate=${row}[${RATE_KEY}] Append To List ${payloads} ${payload} END RETURN ${payloads} Save work item payloads [Arguments] ${payloads} FOR ${payload} IN @{payloads} Save work item payload ${payload} END Save work item payload [Arguments] ${payload} ${variables}= Create Dictionary ${WORK_ITEM_NAME}=${payload} Create Output Work Item variables=${variables} save=True

consumer.robot:

*** 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}]