community
cancel
Showing results for 
Search instead for 
Did you mean: 

Dev Space

Customize & extend the power of Alteryx. SDKs, APIs, custom tools, and more!
SOLVED

Set a constant in a Workflow using Python SDK

Hi,

 

I was wondering if anyone knows how to set a workflow constant using the python sdk?

 

I can read the constant with something like this:

 

if self.alteryx_engine.get_constant(0, 7)[0] == 'User.Test':
    return self.alteryx_engine.get_constant(0, 7)[2] 

 

but if I want to update the value, what command would i use? I tried:

 

self.alteryx_engine.get_constant(0, i)[2] == 'foo'

self.alteryx_engine.set_constant(0, i)[2] == 'foo'

 

 but these where guesses and not very good ones.

 

Thanks

I have not tried updating workflow constants, and am not sure if it is possible.  But at first glance, I see 1 possible issue with your code.  '==' checks for equality; it does not assign a value.  Try using a single equals sign:

 

self.alteryx_engine.get_constant(0, i)[2] = 'foo'

Typo! thanks. 

 

unfortunately still doesn't work. because you can't assign a value to a function, obviously.

 

think you might be right in think it might not be possible.

So after a bit of messing, I came up with this solution, not perfect as it only updates the file if closed or once it has been reopened, but it will work for my needs. 

 

I thought I would post in case anyone else had a similar issue.

 

import xml.etree.ElementTree as ET

filename = 'c:\\myfilepath\\myworkflow.yxmc'

tree = ET.parse(filename)
root = tree.getroot()


def UpdateConstant(elementName, replaceWith):
    replaced = False
    for constant in root.find(".//Constants"):
        if constant.find("./Name").text == elementName:
            constant.find("./Value").text = replaceWith
            replaced = True
            tree.write(filename)
        else:
            pass
    return replaced


print(UpdateConstant('Foo_Name', 'Foo_Value'))

 

 

Thanks