Skip to main content

Notifications

Power Automate - General Discussion
Answered

Retrieve last record ID from SharePoint list

(0) ShareShare
ReportReport
Posted on by 347

Good morning,

I have a test flow where I want to just retrieve the last record ID within the list. In theory, what I want is to find the last item and then the first item in the list, divide all of that by 4000 to find how many iterations I need to loop, and finally delete every single record from there, but let's take a step back and figure out my first problem.

I have a variable that I'm initliazing and within the array, I want to return the first ID with this code but it's currently not working:

Body('Get_items')[0]?['value']?['ID']


here's my current setup:
get_last_ID.png

 

And here's the error that I'm receiving:
error.png

 

Any ideas on how to fix this?

  • hnguy71 Profile Picture
    hnguy71 347 on at
    Re: Retrieve last record ID from SharePoint list

    Ahhh! The [0] was in the wrong place! Thanks for the help!

  • Verified answer
    v-bacao-msft Profile Picture
    v-bacao-msft on at
    Re: Retrieve last record ID from SharePoint list

    Hi @hnguy71 ,

     

    I noticed that the expression you used seems to be incorrect.

    Please try to get the last item using the expression below.

    body('Get_items')?['value'][0]['ID']

    37.PNG

    Please try again.

     

    Best Regards,

  • hnguy71 Profile Picture
    hnguy71 347 on at
    Re: Retrieve last record ID from SharePoint list

    Hi @ScottShearer ,

    that only gives me back a maximum number of 5000. I was able to accomplish this in a different way. I used an apply_to_each and since I only have 1 item, I'll set the last record variable to just that 1 ID.
    apply_to_each.png

     

    From there I set a variable to see how many iterations I need to purge the entire list:

    add(div(variables('_LastRecord'), 4000), 1)

    iterations.png

     

    And I'll likely create an array to loop through them but I got further than I expected. I don't have a large list to try this out on but haha so far it looks like it's working. Thanks for the help!

  • ScottShearer Profile Picture
    ScottShearer 25,150 on at
    Re: Retrieve last record ID from SharePoint list

    @hnguy71 

    As I understand it, your first goal is to find out how many items are in the list.  Here is an alternate approach:

    ListCount.jpg

     

    The variable holds the total number of list items.

    Remember to go to settings for the Get Items action and set pagination on and set the threshold to a max of 5000 unless you have a P2 license.

     

    If you will be using a do until loop when you delete the items, remember to click "change limits" in the loop and set the count to a number tat makes sense - the default maximum loop limit is 60.

     

     

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,304

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,703

Leaderboard