Hi Tony,
Thanks for using our Community Forum!
Today, the workflow.properties variables are not exposed to the Function Code. As you have mentioned, you will need to create a function input for each variable.
You could also make use of this handy function in your pre-process script to allow for an extensible function input:
def dict_to_json_str(d):
"""Function that converts a dictionary into a JSON string.
Supports types: basestring, unicode, bool, int and nested dicts.
Does not support lists.
If the value is None, it sets it to False."""
json_entry = u'"{0}":{1}'
json_entry_str = u'"{0}":"{1}"'
entries = []
for entry in d:
key = entry
value = d[entry]
if value is None:
value = False
if isinstance(value, list):
helper.fail('dict_to_json_str does not support Python Lists')
if isinstance(value, basestring):
value = value.replace(u'"', u'\\"')
entries.append(json_entry_str.format(unicode(key), unicode(value)))
elif isinstance(value, unicode):
entries.append(json_entry.format(unicode(key), unicode(value)))
elif isinstance(value, bool):
value = 'true' if value == True else 'false'
entries.append(json_entry.format(key, value))
elif isinstance(value, int):
entries.append(json_entry.format(unicode(key), value))
elif isinstance(value, dict):
entries.append(json_entry.format(key, dict_to_json_str(value)))
else:
helper.fail('dict_to_json_str does not support this type: {0}'.format(type(value)))
return u'{0} {1} {2}'.format(u'{', ','.join(entries), u'}')
inputs.custom_json_str = dict_to_json_str({
"wf_var_one": workflow.properties.wf_var_one,
"wf_var_two": workflow.properties.wf_var_two,
"person": {"id": 1001, "email": "tom@example.com", "active": True}
})
import json
custom_input_dict = json.loads(kwargs.get("custom_json_str"))
if custom_input_dict.get("wf_var_one"):
print custom_input_dict.get("wf_var_one")
print custom_input_dict["person"]["email"]
------------------------------
Shane Curtin
Integrations Engineer - IBM Resilient
------------------------------
Original Message:
Sent: 03-05-2019 10:06 AM
From: Anthony Bufort
Subject: How to Access Workflow Vars From Within Functions
Hello All,
I know how to access workflow variables using workflow.properties notation within pre and post-processing scripts, but how do I access workflow variables within my functions?
The only way I can see this happening at the moment is for me to set a function-input variable using a pre-processing script that sets the input's value to a workflow.properties.somevar value. I don't want to have to set up input variables to functions every time I want access to workflow vars. I just want to be able to access them within downstream functions once a previous function has set them, in a global-like fashion you could say.
Is there a module I need to import in the code that allows me to do that?
Thanks,