-1

I am learning Logic App. Below is the logic app standard workflow I am working on:

enter image description here

enter image description here

enter image description here

I have 2 questions here.

  1. Stored Proc action returns 900 rows in total, so the foreach loop iterates 900 times. For testing purposes, I need foreach to iterate only 50 or 100 records. Is there any way I can limit the foreach loop to iterate only 100 records?

    I know I can achieve this through Stored Proc itself by modifying the select query but I am just curious to know if this can be achieved from workflow because we can limit Do-Until loop in workflow.

  2. I want to compare val2 with valTest i.e., if valTest == val2 (something like below image)

    enter image description here

    But I am not able to get valTest and val2 values (for each iteration) inside if condition.

I tried below approaches:

  • To get val2 value inside if condition: items('For_each')?['val2']

  • To get valTest value inside if condition: outputs('Select_valTest')?['valTest']

But condition throws some error. How can I get val2 and valTest values inside if condition so that I can compare both?

Note that enter image description here action (body) is using Liquid Template whose output is as below

[
  {
    "valTest": "54322"
  },
  {
    "valTest": "13916"
  },
  {
    "valTest": "13856"
  },
  {
    "valTest": "13617"
  },
  {
    "valTest": "00255"
  },
  {
    "valTest": "00254"
  },
  {
    "valTest": "14948"
  },
  {
    "valTest": "14947"
  }  
]

Output of enter image description here action (Result Result Sets) is as below:

[
  [
    {
      "val1": null,
      "val2": "00197"
    },
    {
      "val1": null,
      "val2": "00203"
    },
    {
      "val1": null,
      "val2": "00205"
    },
    {
      "val1": "AAA",
      "val2": "13946"
    },
    {
      "val1": "ABB",
      "val2": "13947"
    },
    {
      "val1": "ZACD",
      "val2": "13948"
    },
    {
      "val1": "null",
      "val2": "00255"
    },
    {
      "val1": "TTTT",
      "val2": "14947"
    }
  ]
]

Output of enter image description here action (body) is as below:

[
    {
      "val2": "00197"
    },
    {
      "val2": "00203"
    },
    {
      "val2": "00205"
    },
    {
      "val2": "13946"
    },
    {
      "val2": "13947"
    },
    {
      "val2": "13948"
    },
    {
      "val2": "00255"
    },
    {
      "val2": "14947"
    }
]

Update

After creating and running suggested workflow, I get below error:

enter image description here

Also, I think that the action "select(valTest)" (Liquid Template action) shall come inside for-each loop because I have to compare each val2 with each valTest.

As you see that the order of my output value for val2 and valTest is not same (though some values are same) Example: valTest: "00255" and val2: "00255". Upon comparison, every time the condition is FALSE. How can I tackle that?

Please also check your 'Append to array variable' action's value (items('For_each')['val2']). Do I need to replace val2 with something else when using single for each loop?

halfer
  • 19,824
  • 17
  • 99
  • 186
timz_123
  • 435
  • 1
  • 9
  • 47

1 Answers1

0

I want to compare val2 with valTest i.e., if valTest == val2 (something like below image)

After reproducing from my end, Below is something you can do to achieve your requirement.

Here is the complete flow of my Logic app

enter image description here

I am trying to parse the Select Val2 action step and Val Test Step and then trying to compare both the values. In your case you can add parse JSON step for the actual SQL Execute SP to Get val1 and val2 and Select val2 from SP steps.

enter image description here

Parse Json of ValTest

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "valTest": {
                "type": "string"
            }
        },
        "required": [
            "valTest"
        ]
    }
}

Parse Json of action (body)

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "val2": {
                "type": "string"
            }
        },
        "required": [
            "val2"
        ]
    }
}

For demonstration purpose I'm trying to append the resultant values to a variable.

enter image description here

Results:

enter image description here

Stored Proc action returns 900 rows in total, so the foreach loop iterates 900 times. For testing purpose, I need foreach to iterate only 50 or 100 records. Is there any way I can limit the foreach loop to iterate only 100 records ?? I know I can achieve this thru Stored Proc itself by modifying the select query but I am just curious to know if this can be achieved from workflow coz we can limit Do-Until loop in workflow.

For this you can use Until loop where you can set the Count limit with the required number of items along with the condition. Below is the flow that you can use.

enter image description here

Results:

enter image description here

Code view of this logic app is as below

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": [
                    [
                        {
                            "val1": null,
                            "val2": 1
                        },
                       ...
                        {
                            "val1": null,
                            "val2": 899
                        },
                        {
                            "val1": null,
                            "val2": 900
                        }
                    ]
                ],
                "runAfter": {},
                "type": "Compose"
            },
            "Compose_2": {
                "inputs": "@variables('result')",
                "runAfter": {
                    "Until": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "result",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Until": {
                "actions": {
                    "Append_to_array_variable": {
                        "inputs": {
                            "name": "result",
                            "value": "@outputs('Compose')[0][iterationIndexes('Until')]['val2']"
                        },
                        "runAfter": {},
                        "type": "AppendToArrayVariable"
                    }
                },
                "expression": "@equals('', int('50'))",
                "limit": {
                    "count": 50,
                    "timeout": "PT1H"
                },
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Until"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

Update

Below is something you can do if you require only one foreach loop in your flow.

  • You can use contains while comparing in the condition loop. Below is the flow.

enter image description here

Code view of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "@variables('Result')",
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Append_to_array_variable": {
                                "inputs": {
                                    "name": "Result",
                                    "value": "@items('For_each')['val2']"
                                },
                                "runAfter": {},
                                "type": "AppendToArrayVariable"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "contains": [
                                        "@outputs('Output_from_Liquid_Template_(ValTest)')",
                                        "@json(concat('{','\"valTest\":','\"',items('For_each')['val2'],'\"','}'))"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    }
                },
                "foreach": "@body('Parse_JSON_-_action_(body)')",
                "runAfter": {
                    "Initialize_variable_-_Result": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable_-_Result": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Result",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON_-_action_(body)": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Output_from_Liquid_Template_(ValTest)": {
                "inputs": [
                    {
                        "valTest": "54322"
                    },
                    {
                        "valTest": "13916"
                    },
                    {
                        "valTest": "13856"
                    },
                    {
                        "valTest": "13617"
                    },
                    {
                        "valTest": "00255"
                    },
                    {
                        "valTest": "00254"
                    },
                    {
                        "valTest": "14948"
                    },
                    {
                        "valTest": "14947"
                    }
                ],
                "runAfter": {},
                "type": "Compose"
            },
            "Output_of_action_(body)": {
                "inputs": [
                    {
                        "val2": "00197"
                    },
                    {
                        "val2": "00203"
                    },
                    {
                        "val2": "00205"
                    },
                    {
                        "val2": "13946"
                    },
                    {
                        "val2": "13947"
                    },
                    {
                        "val2": "13948"
                    },
                    {
                        "val2": "00255"
                    },
                    {
                        "val2": "14947"
                    }
                ],
                "runAfter": {
                    "Output_from_Liquid_Template_(ValTest)": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Parse_JSON_-_action_(body)": {
                "inputs": {
                    "content": "@outputs('Output_of_action_(body)')",
                    "schema": {
                        "items": {
                            "properties": {
                                "val2": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "val2"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    }
                },
                "runAfter": {
                    "Output_of_action_(body)": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • Thank you so much for the reply. Is it possible to achieve the same using just 1 for-each loop ? As adding 2 for-each loop would take a lot of run time for the workflow. – timz_123 May 21 '23 at 08:35
  • @timz_123, yes it is possible. Do try checking my updated answer. I have added my code view too. – SwethaKandikonda May 22 '23 at 12:33
  • @timz_123 can you update the question with the blockers and also adding the images of the outputs of the sql and liquid templates in the logic app flow – SwethaKandikonda May 23 '23 at 08:16
  • Thank you for your reply. Please check my question, I have posted my queries under update. I suspect the error is due to json concat but not sure. – timz_123 May 23 '23 at 08:32
  • SwethaKandikonda, could you please update any solution ? – timz_123 May 23 '23 at 17:03
  • @timz_123, Even though the order is not same the updated answer of mine should have to work since it checks if the current value of `val2` is present inside `ValTest`. The purpose of using Append to Array Variable is to just show what are all the values that are same in both the arrays. In the Updated Answer I'm trying to form the Val2 same as that of ValTest By adding "valTest":"" for checking if the Val2 value is present in ValTest. If yes, then the Val2 value will be appended to the array variable – SwethaKandikonda May 24 '23 at 10:19
  • The main issue is that I am unable to run your code as it is. It throws an error at 'condition' action which I have updated in my question. Please have a look at it and help to fix the error. – timz_123 May 24 '23 at 11:54