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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Copy last gallery item
Power Apps
Unanswered

Copy last gallery item

(0) ShareShare
ReportReport
Posted on by 820

Hi

I am trying to Copy/Patch last gallery item(data source is dataverse)

But i see it is working not as i have expected

It works for the first time like if i patch value Last(Gallery.Allitems).Name wheer rl;ets say Name value is "Name"

When new line appears in my gallery i want to change "Name" to something else lets say "Name2" and then again i want to press button copy and it should copy to new line Name with value "Name2" but the function Last(Gallery.Allitems).Name copies Name value "Name" not "Name2".

Can i do somehow that within the gallery control or i need to search for the last record inside datasource?

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jja 

    Your gallery will always represent all the items in the Items property table provided.  So, if your gallery is based on a dynamic formula (direct to datasource), then it should update with the addition of the new row, and thus, the Last of AllItems will usually be that record.  However, that is completely dependent on the formula you have in the Items property.  If you are, for example, sorting the table, then Last might not be what you expect.

     

    Check you Items property for any clues, and if nothing found, please provide the Items property you currently have for more analysis.

     

    I hope this is helpful for you.

  • julijazas Profile Picture
    820 on at

    @RandyHayes 

    IN the Items property i have the folowing. In short description user can checkbox and it shows him last 5 records. I am testing with this checkbox checked

    If(Checkbox2.Checked,LastN(SortByColumns(Filter(Components,'Projekto nuoroda'.'Projekto pavadinimas'=FilterProjektas.Selected.'Projekto pavadinimas'),"createdon",Ascending),5),SortByColumns(Filter(Components,'Projekto nuoroda'.'Projekto pavadinimas'=FilterProjektas.Selected.'Projekto pavadinimas'),"new_componentname",Ascending))

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jja 

    You might want to streamline that a little bit...

    With({_items: 
     SortByColumns(
     Filter(Components,
     'Projekto nuoroda'.'Projekto pavadinimas'=FilterProjektas.Selected.'Projekto pavadinimas'
     ),
     "createdon", Ascending)
     )
     },
    
     If(Checkbox2.Value, LastN(_items, 5), _items)
    )

     

    This will avoid you having duplicate formulas in one formula.

     

    In general though, this should work for what you are trying.  It would still show the last record added and even with the LastN function, it would provide what you need.

  • julijazas Profile Picture
    820 on at

    @RandyHayes 

    After tweaking the code i have same behaviour and it is hard to understand what is happening.

    I put some LAST formula on top of app to debug to see what the app thinks what is trully last and i see when first time i create a new record the LAST GUID is understood correctly, The last let's say Name is coppied correctly but now i wantg to change the Name to Name2. I change it and the LAST formula of the name field on top of app still shows Name because the gallery is still not saved. Theoretically before patching new row i save what is changed and only after saving i perform copy/patch LAST but it will COPY anyway the  NAME and not NAME2.

     

    Seems like after the records are saved inside the dataverse either gallery is not refreshed or resorted or there is too less time between the save action and copy action.

     

     

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jja 

    It will all "refresh" dynamically as long as you are based on the datasource you are updating.

     

    But to be clear - the steps are:

    1) You copy/duplicate

    2) The new item is shown in the list/gallery

    3) You edit the item and then save it

    4) You perform the copy/duplicate again and the new record appears but with the old values

     

    Is that what you are seeing, or please clarify the above steps.

  • julijazas Profile Picture
    820 on at

    @RandyHayes 

    Steps are correct and to clarify more save and copy happens within 1 button. This button first checks if there are any changes in a gallery if yes then it performs save changes and after patch copy LAST

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jja 

    What is your formula on that button?

  • julijazas Profile Picture
    820 on at

    Here it is(part of code with some columns that are patching)

    I know you will not like the collection but i still did not manage to owerwrite it as we figured out why i failed working with gallery only(due to gallery being inside containers)

    //check if changes are recorded to collection

    If(
    CountRows(colTaskUpdates)>0,

    Patch(
    Components,

    ForAll(

    colTaskUpdates,


    Patch(
    LookUp(Components,new_componentsId=ThisRecord.Components),
    {

    //new_Projectname:LookUp(Projects,'Projekto pavadinimas'=new_projectname),
    new_gaminiotipas:new_gaminiotipas,
    new_componentname:new_componentname,
    new_qty:Value(new_Qty)


    }
    )
    )
    );

    Clear(colTaskUpdates);
    //Refresh(Components);

     


    //----Copy LAST line----
    Patch(
    Components,
    Defaults(Components),
    {
    'Projekto nuoroda': LookUp(Projects,'Projekto pavadinimas' = Last(galleryGrid.AllItems).'Projekto nuoroda'.'Projekto pavadinimas'),
    'Gaminio tipas': Last(galleryGrid.AllItems).'Gaminio tipas',

    Medžiaga:LookUp(Materials,Pavadinimas=Last(galleryGrid.AllItems).Medžiaga.Pavadinimas),
    Kiekis : Last(galleryGrid.AllItems).Kiekis


    }
    ),

     

    //COPY last record

    Patch(
    Components,
    Defaults(Components),
    {

    'Projekto nuoroda': LookUp(Projects,'Projekto pavadinimas' = Last(galleryGrid.AllItems).'Projekto nuoroda'.'Projekto pavadinimas'),
    'Gaminio tipas': Last(galleryGrid.AllItems).'Gaminio tipas',
    Kiekis : Last(galleryGrid.AllItems).Kiekis,
    Medžiaga:LookUp(Materials,Pavadinimas=Last(galleryGrid.AllItems).Medžiaga.Pavadinimas)

    }
    )
    )
    //Refresh(Components);

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @jja 

    Yep...there's the issue!  Your Gallery will NOT have re-evaluated its formula yet.  So, the Last Item is still the last item that was at the time the formula was run.

     

    What is the difference in your Formula between Copy last line and Copy last record?  These are separated by a comma which is a syntax error...so I am just guessing you have cut the actual formula up and did not notice the error.

  • julijazas Profile Picture
    820 on at

    @RandyHayes 

    Probably i cut the formula and the wrong part remained but all original formula works. Copy last line and copy last record are identical. 
    Logic is like:

    IF any changes exist in gallery THEN 

    Save changes;

    clear collection;

    COPY last gallery Item,

    ELSE(value if false)
    COPY last gallery Item

     

    So what you are telling after creating a record inside the LAST formula is still not revaluated? 
    Do i need to search for last record inside dataverse instead of gallery?

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard