Troubles and Doubts using Work Items locally

Hi, I’m trying to use Work Items but I can’t understand it yet, I have seen the example in the page, also see the youtube video, and read the example on Robocorp Portal, but I still don’t understand how to use them locally.
My first doubt is about the files in the folder devdata , I have this in my env.json file:

{
  "RPA_SECRET_MANAGER": "RPA.Robocloud.Secrets.FileSecrets",
  "RPA_SECRET_FILE": "/home/<my_user>/vault.json",
  "RPA_WORKITEMS_ADAPTER": "RPA.Robocorp.WorkItems.FileAdapter",
  "RPA_INPUT_WORKITEM_PATH": "%{ROBOT_ROOT}/devdata/work-items-in/work-items-in.json",
  "RPA_OUTPUT_WORKITEM_PATH": "%{ROBOT_ROOT}/devdata/work-items-out/work-items-out.json"
}

The last 3 lines were copied from the work items example, so I don’t know if they should be there, but I require the access to the vault.json so I don’t know if I can have them in the same file or it is not recommended (so far I can use the vault and the items with this file). I have two folder inside devdata: work-items-in and work-items-out.

In work-items-in i have work.items-in.json file:

[
    {
        "payload": {
            "produccion": {
                "pedidos": {
                  "pedido1": "cosas1",
                  "pedido2": "cosas2"
                }
            },
            "pedidos": {
                "datos_empresa": {
                    "nombre": "minombre",
                    "id": 123456798
                 },
                "pedidos": {
                  "pedido1": "cosas3",
                  "pedido2": "cosas4"
                }
            }
        },
        "files": {
            "orders.xlsx": "orders.xlsx"
        }
    }
]

I don’t know why it has to be a list when in the example of the page it also shows something like it could be a dictionary, but if it change the content above to a dictionary I am not able to read anything. Also, that is, more or less, the structure from where I want to start to add the information I will process.
In work-items-out i have work-items-out.json, but I don’t know If i should have it or the library will create it by itself or what. For now, I have this:

{
    
}

Ok, now, the process I’m doing is retrieving information from the body of an email (therefore the use of the vault.json). I process the body and create several dictionaries that I want to add to my work item, but I don’t know how. I have been doing this to test the methods from RPA.Robocorp.WorkItems :

*** Settings ***
Documentation   Template robot main suite.
Library         Collections
Library         Triggers
Resource        keywords.robot
Variables       MyVariables.py
Library         RPA.Robocorp.Vault
Library         RPA.Robocorp.WorkItems

*** Task ***
My Task
    ${secret}=    Get Secret    MyMailCredentials
    Authorize    ${secret}[username]    ${secret}[password]
    @{emails}  List Messages    71    
    # Retrieving the body from the email I want
    FOR  ${email}  IN  @{EMAILS}
        Log  ${SPACE} - ${email}    console=True
        Log  ${SPACE} - ${email}[Body]    console=True
        ${body}=    Set Global Variable  ${email}[Body]
    END
    # I do this to check which keywords from the library work
    # This return the file in the work.items-in.json file
    ${names}=    List work item files
    Log    Work item has files with names: ${names}
    # This returns produccion and pedidos, which are the keys from payload
    ${variables}=    List work item variables
    Log    Available variables in work item: ${variables}
    # This returns all elements in payload
    ${payload}=    Get work item payload
    Log    Entire payload as dictionary: ${payload}
    # Here it return: Releasing item '0' with COMPLETED state and exception: None, and ${lengths} is
    # only with None
    @{lengths} =     For Each Input Work Item    Log    payload
    Log   Payload lengths: @{lengths}
    # This one returns: WorkItem(id=0, payload={'produccion': {'pedidos': {' ... sas3', 'pedido2': 'cosas4'}}}, files=1) which I guess if fine
    ${input} =    Get Current Work Item
    # Here the troubles begins, it returns:  	EmptyQueue: No work items in the input queue. I don't know why the keyword above seems like everything was ok
    ${input}=    Get input work item
    # If i comment the line above the error from next keyword is: Can't create any more output work items since the last input was released, get a new input work item first
    # I don't know what to do in this case.
    ${output}=   Create output work item
    # Commenting the 2 lines above this keyword works, but I am not sure what changes.
    Set current work item    ${input}

    # Doing some changes I was able to use Set work item variables , Set work item variable, and Save Work Item, but not Get Input Work Item    work-items-in.json
    # but it was saved in work-items-in.json, while I believe It was going to be saved in work-items-out.json so I can use the code in another 
    # execution only editing the output file, not the input (which I am seeing as the template for edition to pass information for the next Task)

The next Tasks would read the information in the work item and do a little more processing.

Ok This is a long post, but I have a lot of doubts while using Work Items locally, so I hope everything was cleared and someone can help me. Thanks

Note: I am using rpaframework==11.6.0

  1. These 2 environment variables can be removed out of the env.json when working with VSCode and Robocorp Code extension. The extension handles those paths internally.
"RPA_INPUT_WORKITEM_PATH": "%{ROBOT_ROOT}/devdata/work-items-in/work-items-in.json",
"RPA_OUTPUT_WORKITEM_PATH": "%{ROBOT_ROOT}/devdata/work-items-out/work-items-out.json"
  1. The workitems.json file is list of dictionaries, because that way you can have multiple
    work items within one .json file. Thus you are able to simulate situation of one task consuming multiple work items. One dictionary represents one work item (with the payload and files).

  2. To create output work items you need to use 3 keywords. First call Create Output Work Item like you have already done, then call Set Work Item Variable to set your variable into a output item and finally Save work item (which will actually create that item).

I would probably use this kind of structure on the task which should be reading multiple input work items and should create a output work item for each of those.

*** Keywords ***
Log Payload
    ${payload} =    Get Work Item Payload
    Log To Console    \nmessage: ${payload}[message]
    Create Output Workitem for next step    ${payload}[number]

Create Output Workitem for next step
    [Arguments]    ${data}
    Create output work item
    Set work item variables    number=${data+1}
    Save work item

*** Tasks ***
Minimal task
    For Each Input Work Item    Log Payload

As a run result VSCode extension shows output work item file. You can copy this as a new input work item and rename it to for example step1-output-as-input-to-step2 (long descriptive name to show you what it basically means). And when running your step2, you can select this step1-output-as-input-to-step2 input work item for the run. You can also select to run task with output item (so the copy&rename is not necessary, but it is helpful if you want to go and iterate with just step2).

I hope that clears it a bit ?

1 Like

Ok let me answer by number like you did:

  1. Ok perfect, in case of using Robocorp Lab those lines must be there, right?
  2. Ok, now I understand why they are that way. Thanks
  3. Thanks with that i was able to create and output file but this adding in the for cycle this:
@{emails}  List Messages    71    
    # Retrieving the body from the email I want
    FOR  ${email}  IN  @{EMAILS}
        Log  ${SPACE} - ${email}    console=True
        Log  ${SPACE} - ${email}[Body]    console=True
        Create Output Work Item
        Set work item variables    Body=${email}[Body]
        Save work item
    END

The thing I don’t understand is that when the code arrives again to the next Create Output Work Item
between Get input work item and Set current work item it fails the same way. But at least I was able to create the output file.

How could I make this? I am not sure.

Thanks for answering, you are clearing some doubts but some others still remains :grin:.

The local development with Work Items is really optimized for VSCode I am sorry to say. Myself I haven’t even yet tried to use Work Items with the Lab so I can’t help you much on that.

In your code snippet you were basically trying almost all library keywords in sequence, but that is not the real use case.

And this sequence…

    ${input}=    Get input work item
    # If i comment the line above the error from next keyword is: Can't create any more output work items since the last input was released, get a new input work item first
    # I don't know what to do in this case.
    ${output}=   Create output work item
    # Commenting the 2 lines above this keyword works, but I am not sure what changes.
    Set current work item    ${input}

Means basically getting input work item, creating output work item and then getting back to input item without adding anything to output work item and not saving it. Is that what you want to do?

1 Like

Ohh I was not understanding anything about it, I was just checking the keywords and I though that way it should not fail, I though it would give me information about the input work item (which it did) and then I was going to create an output work item to add information to it. But I see that the orders of this code could be the reason for it to fail.

Ohh don’t worry, so far I believe those lines are a must in the Lab :rofl: and what your are saying is really helpful