cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
boedern
Frequent Visitor

Gallery - want to create a duplicate record

Hello,

 

I have a gallery that displays information that is from a collection. A user can indicate if they want to create a similar record from the gallery. I want all of the fields and information to be maintained, but to have the opportunity to change only the ID# as well as an indication to note that this is now a duplicate/similar record. I found a similar thread: https://powerusers.microsoft.com/t5/Building-Power-Apps/Create-a-second-record-entry-from-gallery/td... but something is different and I don't think the original poster was using a collection to power the gallery to start. When I've attempted to re-create, I end up with either multiple collections, identical ID#'s, or completely blank information. Could anyone break down the rules of the code for me and how to apply the same principle to a collection?

 

Much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
GarethPrisk Impactful Individual
Impactful Individual

Re: Gallery - want to create a duplicate record

@WarrenBelzI would use a similar approach. However, I think that @boedern is looking to also increment an ID field to not create an exact duplicate of the record, but instead clone the record as a new unique record.

This requires a relatively basic UpdateIf pattern to what you suggested.

For my example, I'll build a collection

ClearCollect(
    colMain,
    {
        mainID: 1,
        mainName: "Record 1",
        mainDate: Today()
    },
    {
        mainID: 2,
        mainName: "Record 2",
        mainDate: Today()
    }
)

Then I'll add a Gallery and use colMain as the Items property. I mapped the fields, then added another control to trigger the clone. For the OnSelect of that control, I added the following function.

Select(Parent);
// Collect the Item into a temporary collection
ClearCollect(
    colClone,
    ThisItem
);
// Update the necesssary properties of the Clone
UpdateIf(
    colClone,
    mainID = mainID,// Identifer column, but any column will work
    {
        mainName: Concatenate(
            mainName,
            " (Clone)"// Appends the (Clone) text for easy identification
        ),
        mainID: Max(
            colMain,
            mainID
        ) + 1// Increments the Max ID from colMain by 1
    }
);
// Collect the Cloned item into Main
Collect(colMain,colClone);
// Cleanup isn't necessary, since we begin with a ClearCollect

Since my colMain's ID column is a number, I can simply increment it by 1 to get a new incremental number. This is why I was asking initially about your ID column, to understand how we could make/generate a unique ID. If it's not a number, we may need to change that part of the function, but the rest of the cloning operation will work.

View solution in original post

4 REPLIES 4
GarethPrisk Impactful Individual
Impactful Individual

Re: Gallery - want to create a duplicate record

Your collection has an ID column, correct?
What data type is it?
How is it being set now? Can we simply increment?
This is a straight-forward solution, just want to understand your ID field. Also, if would be helpful to see the function used to generate the collection/collect an item into the collection for reference.
Highlighted
WarrenBelz Community Champion
Community Champion

Re: Gallery - want to create a duplicate record

Hello @boedern ,

I can give you a simple process which would (from inside the gallery) add a copy of the current item to the data source. I will call the list TestList

ClearCollect(colTest,ThisItem);
Collect(TestList,colTest)

From outside the gallery (again for the current selected item (gallery name is GalleryTest)

ClearCollect(colTest,GalleryTest.Selected);
Collect(TestList,colTest)

This works as Collect can be used to add new records as long as the field names are the same as those from the collection

You could also Patch back the collection, but this approach is simpler.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

GarethPrisk Impactful Individual
Impactful Individual

Re: Gallery - want to create a duplicate record

@WarrenBelzI would use a similar approach. However, I think that @boedern is looking to also increment an ID field to not create an exact duplicate of the record, but instead clone the record as a new unique record.

This requires a relatively basic UpdateIf pattern to what you suggested.

For my example, I'll build a collection

ClearCollect(
    colMain,
    {
        mainID: 1,
        mainName: "Record 1",
        mainDate: Today()
    },
    {
        mainID: 2,
        mainName: "Record 2",
        mainDate: Today()
    }
)

Then I'll add a Gallery and use colMain as the Items property. I mapped the fields, then added another control to trigger the clone. For the OnSelect of that control, I added the following function.

Select(Parent);
// Collect the Item into a temporary collection
ClearCollect(
    colClone,
    ThisItem
);
// Update the necesssary properties of the Clone
UpdateIf(
    colClone,
    mainID = mainID,// Identifer column, but any column will work
    {
        mainName: Concatenate(
            mainName,
            " (Clone)"// Appends the (Clone) text for easy identification
        ),
        mainID: Max(
            colMain,
            mainID
        ) + 1// Increments the Max ID from colMain by 1
    }
);
// Collect the Cloned item into Main
Collect(colMain,colClone);
// Cleanup isn't necessary, since we begin with a ClearCollect

Since my colMain's ID column is a number, I can simply increment it by 1 to get a new incremental number. This is why I was asking initially about your ID column, to understand how we could make/generate a unique ID. If it's not a number, we may need to change that part of the function, but the rest of the cloning operation will work.

View solution in original post

boedern
Frequent Visitor

Re: Gallery - want to create a duplicate record

Thanks!

 

This has mostly worked with the exception of the ID#. My sample ID number is Alphanumeric with the last 3 digits being generated through the RAND function. Since it is random, I'm a little hesitant to use the +1 just on the off chance it creates a duplicate ID. Also, as the line items are in a way linked (as they are copies) I was hoping to use an A,B,C... indicator on the end of the original ID#.

 

What is currently happening though is that it replaces the ID# with a 1,2,3 rather than adding onto the end as well.

 

Everything else worked perfectly! Thank you.

Helpful resources

Announcements
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Apps Community!

Top Solution Authors
Top Kudoed Authors
Users online (8,225)