locked
Increment a simple variable RRS feed

  • Question

  • Hi,

    in a Until-Loop, i want to increment a variable @a with 5000 for each loop.

    what is the syntax Set Variable: This will fail with error : "The expression contains self referencing variable. A variable cannot reference itself in the expression."

    @string(add(int(variables('a')),5000))

    Thanks

    Regards

    Nicole


    Answer from MSDN


    • Edited by Nicole44 Friday, June 14, 2019 12:46 PM
    Friday, June 14, 2019 12:40 PM

All replies

  • Hello Nicole, and thank you for your inquiry.  I do have a hack to get around this limitation.  Be aware that this hack only works for sequential, not parallel execution.  (Until-loop is good, For-Each when sequential is good, For-Each when not sequential is bad.)

    Solution:

    Have two variables:  CounterVariable, TemporaryVariable

    Inside the For-Each, first increment the Temporary variable like TemporaryVariable = CounterVariable + 1;
    Then set the CounterVariable = TemporaryVariable.

    Here is the code for an example pipeline:

    {
        "name": "Increment Until",
        "properties": {
            "activities": [
                {
                    "name": "Until Loop",
                    "type": "Until",
                    "dependsOn": [
                        {
                            "activity": "Initialize Counter",
                            "dependencyConditions": [
                                "Succeeded"
                            ]
                        }
                    ],
                    "typeProperties": {
                        "expression": {
                            "value": "@greaterOrEquals(int(variables('Counter')),pipeline().parameters.End)",
                            "type": "Expression"
                        },
                        "activities": [
                            {
                                "name": "Set Temporary",
                                "type": "SetVariable",
                                "typeProperties": {
                                    "variableName": "Temporary",
                                    "value": {
                                        "value": "@string(add(int(variables('Counter')),1))",
                                        "type": "Expression"
                                    }
                                }
                            },
                            {
                                "name": "Set Counter",
                                "type": "SetVariable",
                                "dependsOn": [
                                    {
                                        "activity": "Set Temporary",
                                        "dependencyConditions": [
                                            "Succeeded"
                                        ]
                                    }
                                ],
                                "typeProperties": {
                                    "variableName": "Counter",
                                    "value": {
                                        "value": "@variables('Temporary')",
                                        "type": "Expression"
                                    }
                                }
                            }
                        ],
                        "timeout": "7.00:00:00"
                    }
                },
                {
                    "name": "Initialize Counter",
                    "type": "SetVariable",
                    "typeProperties": {
                        "variableName": "Counter",
                        "value": {
                            "value": "@string(pipeline().parameters.Start)",
                            "type": "Expression"
                        }
                    }
                }
            ],
            "parameters": {
                "Start": {
                    "type": "int",
                    "defaultValue": 2
                },
                "End": {
                    "type": "int",
                    "defaultValue": 5
                }
            },
            "variables": {
                "Counter": {
                    "type": "String"
                },
                "Temporary": {
                    "type": "String"
                }
            }
        }
    }


    Friday, June 14, 2019 5:58 PM