What is the best practice for assigning a dynamic global variable?

For instance, what is the best practice for assigning a global variable for today’s date that can then be used in every keyword.

The *** Variables *** section seems to only allow strings.

The code for this variable is:
${current_date}= Get Current Date local

I understand that Set Global Variable is an available task, but where should this be placed in the task file as a best practice (if it should be placed in the task file at all)?

BuiltIn library | Robocorp documentation

This link has very useful information, but I do not see where it says to declare these variables.

Good option is to place it in the Suite Setup that is automatically executed before any task. It seems that we don’t have any good example about this in docs site, but here is an example:

*** Settings ***
Library  DateTime
Suite Setup  Set Global Variables

*** Tasks ***
Minimal task
    Log  ${current_date}

*** Keywords ***
Set Global Variables
    ${current_date}=  Get Current Date  local
    Set Global Variable    ${current_date}
1 Like

It allows also other types, however you cant use keywords in that section

1 Like

Couple other ways

1. Using variables file to set dynamic global variables. This is my favorite way, because it is more flexible to set variables in Python syntax.

tasks.robot

*** Settings ***
Variables       myvariables.py

Minimal task
    Log To Console    TODAY: ${TODAY}

myvariables.py

from robot.libraries.DateTime import get_current_date

TODAY = get_current_date('local')

2. You can use inline Python evaluations in the *** Variables *** section

*** Variables ***
${TODAY}      ${{datetime.datetime.today()}}
1 Like