Dev Space

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

Set a constant in a Workflow using Python SDK

paulfound
11 - Bolide

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

3 REPLIES 3
tlarsen7572
11 - Bolide
11 - Bolide

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'
paulfound
11 - Bolide

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.

paulfound
11 - Bolide

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