cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
azeesdinu43
Helper II
Helper II

Patch - Collection

I have collection  like below.  I have row number added while adding new item to that collection. 

If I have 5 items in my collections, If I delete 3rd Item, I wanted to reorder the RowNumber again. Since Order will be changed like (1,2,4,5) when deleting in-between any rows. 

Kindly let me know how to update the collection while deleting any row.

 

Collect(myCollection,
{
Id: Text(GUID()),
Column1:"",
Column2:"",
RowNumber: CountRows(myCollection)+1
});
3 ACCEPTED SOLUTIONS

Accepted Solutions
StalinPonnusamy
Community Champion
Community Champion

Hi @azeesdinu43 

 

Reorder all the times

 

Collect(
    myCollection,
    {
        Id: Text(GUID()),
        Column1: "",
        Column2: "",
        RowNumber: 0
    }
);
Clear(ColSeq);
ClearCollect(
    tempCollection,
    myCollection
);
ForAll(
    tempCollection,
    Collect(
        ColSeq,
        {Value: ThisRecord.Id}
    );
    Patch(
        myCollection,
        ThisRecord,
        {RowNumber: CountRows(ColSeq)}
    )
);
Clear(tempCollection);

 

 

View solution in original post

Ethan_R
Continued Contributor
Continued Contributor

Hi @azeesdinu43 ,

 

I understood that you want to replace 4th row in third position.

So use this code in your delete function on the record.

//Item/Record that is to be deleted.

Remove(ItemsCollection, ThisItem);
ClearCollect(
    coltemp,
    Filter(ItemsCollection, RecordID >= Gallery1.Selected.RecordID)
);
ForAll(
    coltemp,
    Patch(
        ItemsCollection,
        ThisRecord,
        {
            RecordID: RecordID - 1
        }
    )
);

Here's the working,

Initially I have 5 items,

Ethan_R_0-1635006016918.png

 

After deleting 3rd Record,

Ethan_R_1-1635006171669.png

 

Hope this helps.

 

View solution in original post

Pstork1
Dual Super User III
Dual Super User III

No, if you delete RowNumber 1 then it will update everything with a rowNumber greater than 1.  Which would be 2,3,4,5.  They would become 1,2,3,4.  It will also work if you delete RowNumber 5 because the Update won't do anything since no RowNumber is greater than 5.  UpdateIf() will go through the whole collection and update all the records that meet the criteria.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

13 REPLIES 13
StalinPonnusamy
Community Champion
Community Champion

Hi @azeesdinu43 

 

To get the next number, We can use

Collect(
    myCollection,
    {
        Id: Text(GUID()),
        Column1: "",
        Column2: "",
        RowNumber: Max(
            myCollection,
            RowNumber
        ) + 1
    }
);

 

Let me know if you like to assign a RowNumber to a new record for the deleted one. In this case, the New record RowNumber should be 3. Is it what you are looking?


Thanks,
Stalin - Learn To Illuminate

Pstork1
Dual Super User III
Dual Super User III

If you want to select a row in a gallery and remove it from the collection use the following code.

Set(currentrow,Gallery1.Selected.RowNumber);
Remove(myCollect,LookUp(myCollect,RowNumber=Gallery1.Selected.RowNumber));
UpdateIf(myCollect,RowNumber > currentrow,{RowNumber: RowNumber -1});

 

- Store the selected RowNumber

- Remove the item from the collection

- Update all the RowNumbers higher than that by subtracting 1 



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Ethan_R
Continued Contributor
Continued Contributor

Hi @azeesdinu43 ,

Instead of updating all records in collection is pointless if you have a lot of records. 

I think you need to start count as unique even after middle record is deleted.

I created something similar

Ethan_R_0-1634999165613.png

 

//Here CountID is similar to your RowNumber.
//But in my collection RecordID is unique

Collect(
    ItemsCollection,
    {
        CountID: CountRows(ItemsCollection)+1,
        RecordID: First(SortByColumns(ItemsCollection,"RecordID", Descending)).RecordID + 1
    }
)

 

Hope this helps you in some way.

Thanks for your reply. I wanted to recount the collection from beginning .
Example , 1,2,3,4,5,6 is my row number, If I deleted third row I wanted to replace 4th row in third position...like 1,2,3,4,5

StalinPonnusamy
Community Champion
Community Champion

Hi @azeesdinu43 

 

Reorder all the times

 

Collect(
    myCollection,
    {
        Id: Text(GUID()),
        Column1: "",
        Column2: "",
        RowNumber: 0
    }
);
Clear(ColSeq);
ClearCollect(
    tempCollection,
    myCollection
);
ForAll(
    tempCollection,
    Collect(
        ColSeq,
        {Value: ThisRecord.Id}
    );
    Patch(
        myCollection,
        ThisRecord,
        {RowNumber: CountRows(ColSeq)}
    )
);
Clear(tempCollection);

 

 

View solution in original post

StalinPonnusamy
Community Champion
Community Champion

Hi @azeesdinu43 

 

For New record, we can do max

 

Collect(
    myCollection,
    {
        Id: Text(GUID()),
        Column1: "",
        Column2: "",
        RowNumber: Max(myCollection,RowNumber) + 1
    }
);

 

 

When you remove the record then add this code  to reorder it

 

//Your Remove Code

Clear(ColSeq);
ClearCollect(
    tempCollection,
    myCollection
);
ForAll(
    tempCollection,
    Collect(
        ColSeq,
        {Value: ThisRecord.Id}
    );
    Patch(
        myCollection,
        ThisRecord,
        {RowNumber: CountRows(ColSeq)}
    )
);
Clear(tempCollection);Clear(ColSeq);

 

 

Ethan_R
Continued Contributor
Continued Contributor

Hi @azeesdinu43 ,

 

I understood that you want to replace 4th row in third position.

So use this code in your delete function on the record.

//Item/Record that is to be deleted.

Remove(ItemsCollection, ThisItem);
ClearCollect(
    coltemp,
    Filter(ItemsCollection, RecordID >= Gallery1.Selected.RecordID)
);
ForAll(
    coltemp,
    Patch(
        ItemsCollection,
        ThisRecord,
        {
            RecordID: RecordID - 1
        }
    )
);

Here's the working,

Initially I have 5 items,

Ethan_R_0-1635006016918.png

 

After deleting 3rd Record,

Ethan_R_1-1635006171669.png

 

Hope this helps.

 

View solution in original post

Pstork1
Dual Super User III
Dual Super User III

You really don't need to do a full ClearCollect() and ForAll() to change the row numbers.  Just use Remove() to delete the one row and then UpdateIf() to subtract one from the row numbers greater than the Row removed.  Its a lot more efficient.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Ethan_R
Continued Contributor
Continued Contributor

Thanks @Pstork1 .

Will keep this in mind.

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,159)