web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Array index out of bounds
Power Automate
Answered

Array index out of bounds

(0) ShareShare
ReportReport
Posted on by 84

I have successfully set up a flow with HTML to text but I am unable to pull the first name from a line in an email. 

Second array reads "Please see the PCA details below for Jane Doe."

replace(trim(split(body('Filter_array')[1], ' ')[5]), '.', '') 

The above expression pulls the last name from the array.

Trim(split(body('Filter_array')[1], ' ')[7])

The above expression does not pull Jane from the array though. "cannot be evaluated because array index '7' is outside bounds (0, 6) of array. "

 

Any help is appreciated. 

Categories:
I have the same question (0)
  • grantjenkins Profile Picture
    11,063 Moderator on at

    I'm assuming there is other text within the body of the email. Also, assuming it will always have "Please see the PCA details below for" followed by the name of the person.

     

    Here is the email I used for testing the flow.

    grantjenkins_0-1674879793860.png

     

    Full flow below.

    grantjenkins_1-1674880245029.png

     

    The expression used in the Compose to extract out just the name is below. It splits the data by 'Please see the PCA details below for' and gets the last item from that array. It then splits by new line and gets the first item from that array. It then removes the period and trims the leading/trailing spaces.

    trim(replace(first(split(last(split(outputs('Html_to_text')?['body'], 'Please see the PCA details below for')), decodeUriComponent('%0A'))), '.', ''))

    grantjenkins_2-1674880392950.png

     

    The output after running the flow in this example (using the email above) would be:

    grantjenkins_3-1674880459251.png


    ----------------------------------------------------------------------
    If I've answered your question, please mark the post as Solved.
    If you like my response, please consider giving it a Thumbs Up.

  • DH2023 Profile Picture
    84 on at

    Upon testing the expression provided, I received the following Output. 

     

    DH2023_0-1674885339979.png

     

    The first two lines of the email read as follows:

     

    Personnel Change Authorization (New Hire)

    Please see the PCA details below for Jane Doe.

     

    I would be happy to get either the First Name and Last Name separately so that each could be entered into it's own cell within Excel although I know this would change the expression quite a bit.  

     

    I appreciate your insight on this situation.

  • grantjenkins Profile Picture
    11,063 Moderator on at

    Are you able to copy the text "Please see the PCA details below for" directly from your email and paste it into the expression and see if that works as expected?

     

    Also, for splitting the name into First Name and Last Name - what would you want to happen if the name was made up of more than two parts - say "Mary Jane Smith" for example?

     

  • DH2023 Profile Picture
    84 on at

    I played around with this idea a bit last night but was unsuccessful. I was able to use the following expression to pull the first and last name without and spaces today:

    trim(replace(trim(split(body('Filter_array')[1], 'for')[1]), '.', ''))

     

    With just this first and last name, I should be able to use a formula in excel to separate these and achieve the automation I am looking for. 

     

    For the most part, when our new hires come in, they do not include the middle name but this does happen. 

     

    In this case, I would assume I would need some way to pull the first and last but trim the middle? Do you have any insight on achieving this. 

     

    Again, thank you for the assistance. 

  • grantjenkins Profile Picture
    11,063 Moderator on at

    Looking at your expression, what happens if your email contains the word "for" somewhere else (more than once)?

     

    I've updated my flow, so it first removes any occurrences of   which your text contained in place of normal spaces when I copied your example. That's why it wasn't working for you. Not sure if that's just part of the input you get in your email body but fixed now.

     

    See updated flow that will get the First and Last names, and split them, including handling more than two parts.

    grantjenkins_6-1674918952117.png

     

    Body is a Compose that takes the Body from When a new email arrives. The expression used replaces any occurrences of   with a standard space character.

    replace(triggerOutputs()?['body/body'], ' ', ' ')

    grantjenkins_1-1674918667379.png

     

    Html to text then takes the output from the Body (Compose action above).

    grantjenkins_2-1674918704703.png

     

    Full Name (Compose) extracts out the full name using the following expression (same as before):

    trim(replace(first(split(last(split(outputs('Html_to_text')?['body'], 'Please see the PCA details below for')), decodeUriComponent('%0A'))), '.', ''))

    grantjenkins_3-1674918816971.png

     

    First Name (Compose) extracts out the First Name (first word) from the Full Name.

    first(split(outputs('Full_Name'), ' '))

    grantjenkins_4-1674918873415.png

     

    Last Name (Compose) extracts out the Last Name (remaining words after the first word) from the Full Name.

    join(skip(split(outputs('Full_Name'), ' '), 1), ' ')

    grantjenkins_5-1674918931333.png

     

    When I run the flow, I get the following output.

    grantjenkins_7-1674919017093.png

     

    And running it with a name that contains more than two parts.

    grantjenkins_8-1674919124945.png

     

  • DH2023 Profile Picture
    84 on at

    WOW! That worked like a charm! Thank you. You're a genius. 

     

    Another thing I would be looking to add to this flow would be to pull the supervisors name from the email which would come from a lower line in the email. I have attached an email example here. 

     

    Personnel Change Authorization (New Hire)

    Please see the PCA details below for Jane Doe.

    Details

     

    Company:

    Oxford Global

    Business Unit:

    Oxford

    Department:

    Sales Local

    Work Location:

    Minneapolis, MN

    Discipline:

    ITIT

    Sales Location:

    Minneapolis

    FO Unit:

    Minnesota IT SALES

    Service:

    SA-Staff Aug

    Start Date:

    1/30/2023

    Job Code:

    1111

    Job Title:

    Account Manager

    Standard Hours:

    40

    New Hire Type:

    External

     

    Recruiter:

    Jake Smith

    Supervisor:

    Jack Johnson

    Dept. Head Approval:

    Approved

    Finance Dept. Approval:

    Approved

    Name:

    Jane Doe

    Preferred First Name:

    Jane

    Home Email:

    jane_doe@example.com

    Contact No.:

    (123) 456-7891

     

    Thanks,
    HR Department

     

     

     

    Would modifying this expression to pull both the first and last name of the supervisor in this expression work just the same? Or perhaps using a new compose with one of the previous expression?

  • Verified answer
    grantjenkins Profile Picture
    11,063 Moderator on at

    Below is how I would build the flow, so you have easy access to all the properties from the email.

     

    See full flow below. I'll go into each of the actions. Note that the actions highlighted in yellow are just for testing purposes.

    grantjenkins_0-1674971089321.png

     

    Body is a Compose that takes the Body from When a new email arrives. The expression used replaces any occurrences of   with a standard space character.

    replace(triggerOutputs()?['body/body'], ' ', ' ')

    grantjenkins_1-1674971123250.png

     

    Html to text then takes the output from the Body (Compose action above).

    grantjenkins_2-1674971123415.png

     

    Select splits the data into an array of strings and, trims each one. The expressions used are:

    //From
    split(outputs('Html_to_text')?['body'], decodeUriComponent('%0A'))
    
    //Map
    trim(item())

    grantjenkins_3-1674971212753.png

     

    Filter array takes the output from Select and removes any empty rows. The expression used is:

    item()

    grantjenkins_4-1674971265225.png

     

    XML (Compose) converts the output from Filter array into XML so we can use XPath to extract out the property values. The expression used is:

    xml(json(concat('{"root": { value:', body('Filter_array'), '}}')))

    grantjenkins_5-1674971340852.png

     

    Compose is used to extract out each of the property values from the XML output into a single object. Below is the expressions used. You can just copy/paste this directly into a Compose action.

    {
     "Company": @{xpath(outputs('XML'), 'string( //value[.="Company:"]/following-sibling::*[1]/text())')},
     "Business Unit": @{xpath(outputs('XML'), 'string( //value[.="Business Unit:"]/following-sibling::*[1]/text())')},
     "Department": @{xpath(outputs('XML'), 'string( //value[.="Department:"]/following-sibling::*[1]/text())')},
     "Work Location": @{xpath(outputs('XML'), 'string( //value[.="Work Location:"]/following-sibling::*[1]/text())')},
     "Discipline": @{xpath(outputs('XML'), 'string( //value[.="Discipline:"]/following-sibling::*[1]/text())')},
     "Sales Location": @{xpath(outputs('XML'), 'string( //value[.="Sales Location:"]/following-sibling::*[1]/text())')},
     "FO Unit": @{xpath(outputs('XML'), 'string( //value[.="FO Unit:"]/following-sibling::*[1]/text())')},
     "Service": @{xpath(outputs('XML'), 'string( //value[.="Service:"]/following-sibling::*[1]/text())')},
     "Start Date": @{xpath(outputs('XML'), 'string( //value[.="Start Date:"]/following-sibling::*[1]/text())')},
     "Job Code": @{xpath(outputs('XML'), 'string( //value[.="Job Code:"]/following-sibling::*[1]/text())')},
     "Job Title": @{xpath(outputs('XML'), 'string( //value[.="Job Title:"]/following-sibling::*[1]/text())')},
     "Standard Hours": @{xpath(outputs('XML'), 'string( //value[.="Standard Hours:"]/following-sibling::*[1]/text())')},
     "New Hire Type": @{xpath(outputs('XML'), 'string( //value[.="New Hire Type:"]/following-sibling::*[1]/text())')},
     "Recruiter": @{xpath(outputs('XML'), 'string( //value[.="Recruiter:"]/following-sibling::*[1]/text())')},
     "Supervisor": @{xpath(outputs('XML'), 'string( //value[.="Supervisor:"]/following-sibling::*[1]/text())')},
     "Dept. Head Approval": @{xpath(outputs('XML'), 'string( //value[.="Dept. Head Approval:"]/following-sibling::*[1]/text())')},
     "Finance Dept. Approval": @{xpath(outputs('XML'), 'string( //value[.="Finance Dept. Approval:"]/following-sibling::*[1]/text())')},
     "Name": @{xpath(outputs('XML'), 'string( //value[.="Name:"]/following-sibling::*[1]/text())')},
     "Preferred First Name": @{xpath(outputs('XML'), 'string( //value[.="Preferred First Name:"]/following-sibling::*[1]/text())')},
     "Home Email": @{xpath(outputs('XML'), 'string( //value[.="Home Email:"]/following-sibling::*[1]/text())')},
     "Contact No.": @{xpath(outputs('XML'), 'string( //value[.="Contact No.:"]/following-sibling::*[1]/text())')}
    }

    grantjenkins_6-1674971425819.png

     

    I've then added a Parse JSON that will provide us with easy access to the data via Dynamic content. The schema used is:

    {
     "type": "object",
     "properties": {
     "Company": {
     "type": "string"
     },
     "Business Unit": {
     "type": "string"
     },
     "Department": {
     "type": "string"
     },
     "Work Location": {
     "type": "string"
     },
     "Discipline": {
     "type": "string"
     },
     "Sales Location": {
     "type": "string"
     },
     "FO Unit": {
     "type": "string"
     },
     "Service": {
     "type": "string"
     },
     "Start Date": {
     "type": "string"
     },
     "Job Code": {
     "type": "string"
     },
     "Job Title": {
     "type": "string"
     },
     "Standard Hours": {
     "type": "string"
     },
     "New Hire Type": {
     "type": "string"
     },
     "Recruiter": {
     "type": "string"
     },
     "Supervisor": {
     "type": "string"
     },
     "Dept. Head Approval": {
     "type": "string"
     },
     "Finance Dept. Approval": {
     "type": "string"
     },
     "Name": {
     "type": "string"
     },
     "Preferred First Name": {
     "type": "string"
     },
     "Home Email": {
     "type": "string"
     },
     "Contact No.": {
     "type": "string"
     }
     }
    }

    grantjenkins_7-1674971523108.png

     

    Below is what we would get when running the flow.

    grantjenkins_14-1674972141839.png

     

    If we try to access the data, we can just select the Property Name we want in the Dynamic content.

    grantjenkins_9-1674971663070.png

     

    To extract just the First Name of the person, we can either choose to use the Preferred First Name property or extract it from the Full Name. See expression below:

    first(split(body('Parse_JSON')?['Name'], ' '))

    grantjenkins_10-1674971832908.png

     

    And to get the Last Name (everything from the Full Name after the First Name).

    trim(slice(body('Parse_JSON')?['Name'], indexOf(body('Parse_JSON')?['Name'], ' ')))

    grantjenkins_11-1674971850789.png

     

    After running the flow as it is, we would get the following output.

    grantjenkins_12-1674971900933.png

    grantjenkins_13-1674971921519.png


    ----------------------------------------------------------------------
    If I've answered your question, please mark the post as Solved.
    If you like my response, please consider giving it a Thumbs Up.

  • DH2023 Profile Picture
    84 on at

    This is extremely informational. Thank you so much. 

     

    I have tried to follow the steps provided but seem to be coming up missing the pulled fields in content after the Parse JSON step when testing. Do you have any idea why that may be?

     

    DH2023_0-1674975340022.png

     

    Best regards,

  • grantjenkins Profile Picture
    11,063 Moderator on at

    Can you show the output you are getting in the Filter array?

  • DH2023 Profile Picture
    84 on at

    DH2023_0-1674998299141.png

     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 840

#2
Valantis Profile Picture

Valantis 661

#3
Haque Profile Picture

Haque 589

Last 30 days Overall leaderboard