Power Community

Power Community

Copy a record and save as new using an Edit Form and Gallery

Scenario

We want to copy a record selected from a Gallery and display the attributes of that copied record on an Edit Form. We also want to save the copied record as a new record in our data source. Note I have already written a version of this solution via the blog article below, but without the use of a Gallery:

https://powerusers.microsoft.com/t5/Power-Apps-Community-Blog/How-to-copy-a-record-in-a-Form-as-a-ne… 

Initial Set Up

This solution has been verified using SharePoint and Dataverse.

1. Insert an EditForm control into your Screen.

2. Set the DataSource property to your data source, and insert the DataCards you need.

3. For this example, we are going to identify the record we want to copy by using a Gallery control. Therefore, insert a Gallery control onto your Screen and set the Items property to the same data source as our Edit Form. Configure the Gallery control as you see fit. 

4. Insert a Button or Icon control into your Gallery. For example a “Copy” Icon.

5. Insert a Label control into the Gallery and for the time being, enter “Copy” into the Text property of the Label.

6. Insert another Button control onto your Screen. Label the Button to “New Form” and on the OnSelect property, enter:

NewForm(Form1)

7. Insert another Button control onto your Screen. Label the Button to “Save” and on the OnSelect property, enter:

SubmitForm(Form1)

Note that “Form1” is name of the EditForm control in this example.

Here is an example of what our set up looks like:

Amik_0-1718755996967.png

Here, my EditForm and Gallery includes two Single Line Text columns called “Title” and “Record Priority”, a Multi-line Text Column called “Record Priority”, and a People column called “Record Owner”.

We want to take a copy of all DataCards except for the “Title” field, because for this scenario we want our users to be able to edit this field prior to clicking the Submit Button.

Setting up the Copy operation

1. We are going to leverage Variables to determine whether or not we want to copy a record. On the OnSelect property of the Copy Button embedded into our Gallery control, enter:

Set(
    gbl_record,
    ThisItem
);
Set(
    gbl_copy,
    true
);
Notify(
    "Record Copied",
    NotificationType.Success
);
NewForm(Form1)

Explanation

“gbl_copy” is a Global Variable which will return Boolean true or Boolean false. We want this Variable to be true.

We are also going to declare another Global Variable called “gbl_record” to capture the attributes of the selected record.

Importantly, we are also going to set the Form to New Mode. This is important because we want to ensure we save a new record, rather than overwrite the existing selected record.

Note, we can also use a Local Context Variable for this exercise but for the purpose of simplicity, a Global Variable is being used in case you’re EditForm and Gallery component are on different screens.

2. Set the Item property of the EditForm property to our Variable:

gbl_record

3. On the OnSelect property of the Gallery control, enter:

If(
    gbl_copy,
    Set(
        gbl_copy,
        false
    );
    Notify(
        "Copy cancelled",
        NotificationType.Warning
    )
);
Set(
    gbl_record,
    ThisItem
);
EditForm(FormForCopy)

Explanation

Whenever an item in the Gallery control is selected, we are also setting the Form mode to Edit and the gbl_copy variable to false.

This will ensure the selected record is displayed in the Edit Form, and we can still edit the record using our EditForm as normal without any copy action taking place.

This will also accommodate a situation in which user might select the Copy Button initially, but then decide to select a different record. If this occurs, we will cancel the copy operation and inform the user using the Notify function.

4. On the OnSuccess property of the EditForm control, enter:

Notify(
    "Saved",
    NotificationType.Success
);
Set(
    gbl_record,
    Self.LastSubmit
);
Set(
    gbl_copy,
    false
)

5. For the Default property of each DataCard in your EditForm, enter the following formula, but change the field name based on DataCard. 

Here for example, we going to apply the below to the Default property of the DataCard for “Record Priority”:

If(
    gbl_copy,
    gbl_record.'Record Priority',
    ThisItem.'Record Priority'
)

On the Default property of another DataCard, for example the “Record Owner” DataCard, enter:

If(
    gbl_copy,
    gbl_record.'Record Owner',
    ThisItem.'Record Owner'
)

Optional UI changes

It would be useful to give the user some meaningful visual feedback that an item has been copied.

1. On the Visible property of the “Copy” Button, enter:

ThisItem.IsSelected

2. On the Visible property of the “Copy” Label, enter:

ThisItem.IsSelected

3. Change the formula in the Text property of the Label to:

If(
    ThisItem.IsSelected && gbl_copy,
    "Item Copied",
    "Copy"
)

4. Set the TemplateFill property of the Gallery to:

If(gbl_copy && ThisItem.IsSelected, Color.LightGray, RGBA(0,0,0,0))

5. Although the EditForm is displaying the copied version of a record, the original item we copied is still displaying as selected in the Gallery. This will be confusing for the user. To accommodate for this, set the Default property of the Gallery to the below to make the Gallery “appear” unselected:

{}

Final output

Recording2024-06-19014250.gif

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

Maplytics Optimizes Routes and Boosts Productivity for T-GREX

T-GREX, a company founded by brothers Roberto and Matteo, brings together their 30 years of experience in the precision...

More Articles Like This

- Advertisement -spot_img