Initialize and increment variable are not working as expected in logic app.

Shreya Gupta 20 Reputation points
2024-04-10T07:09:35.5766667+00:00

I have an array of strings and I want to divide it into 4 arrays. To do so I am initializing a counter and 4 arrays and adding elements in each array based on the counter once counter reaches 4 I am resetting it to 0. I am using below code. I have observed that initialize counter is not initializing the counter to 0 and increment counter also does not seem to be incrementing by 1 but some random value.

            "foreachstring": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "ResetCounter": {
                                "inputs": {
                                    "name": "ccidcounter",
                                    "value": 0
                                },
                                "type": "SetVariable"
                            }
                        },
                        "else": {
                            "actions": {
                                "IncrementCCIDCounter": {
                                    "inputs": {
                                        "name": "ccidcounter",
                                        "value": 1
                                    },
                                    "type": "IncrementVariable"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@variables('ccidcounter')",
                                        4
                                    ]
                                }
                            ]
                        },
                        "runAfter": {
                            "SwitchOnCounter": [
                                "SUCCEEDED"
                            ]
                        },
                        "type": "If"
                    },
                    "SwitchOnCounter": {
                        "cases": {
                            "Case_0": {
                                "actions": {
                                    "AppendToArr1": {
                                        "inputs": {
                                            "name": "arr1",
                                            "value": "@items('foreachstring')"
                                        },
                                        "type": "AppendToArrayVariable"
                                    }
                                },
                                "case": 0
                            },
                            "Case_1": {
                                "actions": {
                                    "AppendToArr2": {
                                        "inputs": {
                                            "name": "arr2",
                                            "value": "@items('foreachstring')"
                                        },
                                        "type": "AppendToArrayVariable"
                                    }
                                },
                                "case": 1
                            },
                            "Case_2": {
                                "actions": {
                                    "AppendToArr3": {
                                        "inputs": {
                                            "name": "arr3",
                                            "value": "@items('foreachstring')"
                                        },
                                        "type": "AppendToArrayVariable"
                                    }
                                },
                                "case": 2
                            },
                            "Case_3": {
                                "actions": {
                                    "AppendToArr4": {
                                        "inputs": {
                                            "name": "arr4",
                                            "value": "@items('foreachstring')"
                                        },
                                        "type": "AppendToArrayVariable"
                                    }
                                },
                                "case": 3
                            }
                        },
                        "default": {
                            "actions": {}
                        },
                        "expression": "@variables('ccidcounter')",
                        "type": "Switch"
                    }
                },
                "foreach": "@body('ParseJSONFromCosmosQuery')?['value']",
                "runAfter": {
                    "initialize_counter": [
                        "SUCCEEDED"
                    ]
                },
                "type": "Foreach"
            },
            "initialize_arr1": {
                "inputs": {
                    "variables": [
                        {
                            "name": "arr1",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "ParseJSONFromCosmosQuery": [
                        "SUCCEEDED"
                    ]
                },
                "type": "InitializeVariable"
            },
            "initialize_arr2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "arr2",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "initialize_arr1": [
                        "SUCCEEDED"
                    ]
                },
                "type": "InitializeVariable"
            },
            "initialize_arr3": {
                "inputs": {
                    "variables": [
                        {
                            "name": "arr3",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "initialize_arr2": [
                        "SUCCEEDED"
                    ]
                },
                "type": "InitializeVariable"
            },
            "initialize_arr4": {
                "inputs": {
                    "variables": [
                        {
                            "name": "arr4",
                            "type": "array",
                            "value": []
                        }
                    ]
                },
                "runAfter": {
                    "initialize_arr3": [
                        "SUCCEEDED"
                    ]
                },
                "type": "InitializeVariable"
            },
            "initialize_counter": {
                "inputs": {
                    "variables": [
                        {
                            "name": "ccidcounter",
                            "type": "integer",
                            "value": 0
                        }
                    ]
                },
                "runAfter": {
                    "initialize_arr4": [
                        "SUCCEEDED"
                    ]
                },
                "type": "InitializeVariable"
            }
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,141 questions
0 comments No comments
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,451 Reputation points
    2024-04-10T08:52:28.94+00:00

    @Shreya Gupta Thanks for reaching out.

    It looks like you are updating the counter inside the for each loop.

    Can you please make sure that you are running your for each loop in sequential. By default, it runs parallel and you can set the Degree of Parallelism slider to 1 as documented here in your for each loop and see if it helps you to get the expected output.

    If not, can you please share your workflow screenshot, so it is easier to understand what are the different action and which action is inside and outside the loop.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.