Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

Safely access an array by index without outside bounds of array error

Like (0) ShareShare
ReportReport
Posted on 12 Aug 2019 06:55:38 by 20

Hi,
I have an array variable that's dynamically populated with strings at runtime (see below). The length of the array is unknown.
When accessing the array I'd like to check that the array element exists/ is within the bounds of the array before accessing and using it.

 

How do I safely access an array?

 

Here's an example of what I've tried already:

if(greaterOrEquals(length(variables('Categories')), 4),variables('Categories')[3], null)

The array has the following elements: "Unified Interface", "deprecated", "legacy web client"

 

I would expect the following:

length to return 3

greaterOrEquals statement to result in false

The first return value isn't evaluated and the second return value is used.

 

However this is the error I'm currently receiving:

InvalidTemplate. Unable to process template language expressions in action '~' inputs at line '1' and column '2725': 'The template language expression 'if(greaterOrEquals(length(variables('Categories')), 4),variables('Categories')[3], null)' cannot be evaluated because array index '3' is outside bounds (0, 2) of array. Please see https://aka.ms/logicexpressions for usage details.'.

  • v-alzhan-msft Profile Picture
    on 13 Aug 2019 at 06:18:52
    Re: Safely access an array by index without outside bounds of array error

    Hi @JohnMaxwell ,

     

    Have your problem be solved?  If yes, please go ahead and mark the post as solved by clicking “Accept as Solution” so that this thread will be marked for other users to easily identify!

     

    Best regards,

    Alice       

     

    Community Support Team _ Alice Zhang
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Verified answer
    johnmaxwell Profile Picture
    20 on 13 Aug 2019 at 06:15:05
    Re: Safely access an array by index without outside bounds of array error

    Hi @v-alzhan-msft,

     

    The if expression would check the true value even though the expression has false result, so you couldn't use the if expression for you scenes.

    That's a shame, I take it there's no way to achieve this solely at the logical expression level?

     

    I got around my issue by using Do Until the array length equaled 20 to fill the rest of the array with empty strings "".

    This meant I could access the first 20 array elements (which was all I could use in the Yammer Post message action) via index without worrying about a length check.

     

    Capture.JPG

     

    Capture.JPG

     

     

    Cheers

  • Verified answer
    v-alzhan-msft Profile Picture
    on 13 Aug 2019 at 05:44:20
    Re: Safely access an array by index without outside bounds of array error

    Hi @JohnMaxwell ,

     

    The if expression would check the true value even though the expression has false result, so you couldn't use the if expression for you scenes.

    You could consider using the Condition or Switch on to achieve your needs.

    1.png

     

    Best regards,

    Alice       

     

    Community Support Team _ Alice Zhang
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • abm abm Profile Picture
    32,464 Most Valuable Professional on 12 Aug 2019 at 12:08:31
    Re: Safely access an array by index without outside bounds of array error

    Hi @JohnMaxwell ,

     

    Thanks for your quick reply.

     

    I have tested this and my array has 4 values 

    "Unified Interface", "deprecated", "legacy web client", "Flow"

     

    So the length returned was 4

     

    When I combined with variables('Categories')[3] is  null then it did returned false which was expected.

     

    So my question is are these array values are generated dynamically or always you can expect 4 elements.

     

    I would suggest you to check how these arrays are constructed. 

     

    If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

     

    Thanks

  • johnmaxwell Profile Picture
    20 on 12 Aug 2019 at 11:13:59
    Re: Safely access an array by index without outside bounds of array error

    Hi @abm,

    Thanks for replying but that's what I was expecting and why I'm confused as to why the following if statement is still attempting to access the array despite the greaterOrEquals returning false with the length 3 array.

     

    if(greaterOrEquals(length(variables('Categories')), 4),variables('Categories')[3], null)
    greaterOrEquals(length(variables('Categories')), 4)

     

    If for example the array has the following elements then the if statement would work and the array element "Flow" would be accessed successfully, but I need to handle any length array without it breaking. 

    "Unified Interface", "deprecated", "legacy web client", "Flow"

     

    Cheers

  • abm abm Profile Picture
    32,464 Most Valuable Professional on 12 Aug 2019 at 08:34:00
    Re: Safely access an array by index without outside bounds of array error

    Hi @JohnMaxwell ,

     

    The array index starts from zero.

     

    So in your array Categories are : "Unified Interface", "deprecated", "legacy web client"

     

    The length of the array will return 3. But remember the array index starts with zero.

     

    variables('Categories')[0] returns "Unitifed Interface"

    variables('Categories')[1] returns "deprecated"

    variables('Categories')[2] returns "legacy web client"

     

    If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up.

     

    Thanks

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Building Flows

#1
stampcoin Profile Picture

stampcoin 81

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 70 Super User 2025 Season 1

#3
David_MA Profile Picture

David_MA 48 Super User 2025 Season 1

Overall leaderboard