power-automate

Set object if condition met in Compose Action


SetProperty() is not updating an object as expected.

I initialized an object HasAtLeast1AttachmentsWith = {"CapitalCall":"False"}.

I then create a compose action with the expression below

if(condition,setProperty(variables('HasAtLeast1AttachmentsWith'),'CapitalCall','True'),0)

I ran a test where the if condition returns true and the compose output correctly shows {"CapitalCall":"True"}. However, HasAtLeast1AttachmentsWith remains as {"CapitalCall":"False"} / was not updated by setProperty. Does anyone know why this is? I need the compose action to update the object if the condition holds.

For my use case, I am going to be having several updates to the object each based on a distinct logical expression. Is it not possible to handle this all in a single compose action using expressions?


Solution

  • setProperty() doesn't update the variable directly, it returns the value of the variable after setProperty() has been executed over it.

    You need to use a pattern where you use a Compose to execute the setProperty() expression and then pass the result of that compose back into your variable using a Set Variable action.

    enter image description here

    Initialize Object

    This creates a basic object with a single property called Property1

    Compose Set Property

    This is where you run the setProperty() expression, this is what I have in that step ...

    setProperty(variables('Test Object'), 'Property1', 'Value1 Updated')
    

    Set Test Object

    This is a Set Variable operation which sets the variable Test Object using the output of the previous step.

    This is the end result ...

    enter image description here

    Bottom line, you can't self reference in the PowerAutomate expression framework, hence why you use a Compose in the middle and also why you can't use Set Variable to do what the compose step is doing, they have to be separated.