cancel
Showing results for 
Search instead for 
Did you mean: 
Amik

How to copy a record in a Form as a new record without using Patch

I recently came across the following scenario posted on the forums and I moved my response to this blog.

 

Scenario:

 

When a user selects a record from a Gallery control, an Edit Form is automatically populated with the selected record. The user wants to be able to copy the existing record, make a change to one or more fields, and then submit the Form as a new record.

 

Steps

 

1. Insert a Edit Form control into your Screen. For this example, I will refer to this Form as "Form1" in our formulas.

2. Set the DataSource property to your data source.

3. Insert your required DataCards into your Edit Form. For this example, we are adding a Single Line Text field, a Single-Select Choice field, and a Date field.

4. Insert a Gallery control into the same screen or in a different screen.

 

5. Set the Items property of the Gallery control to the same data source used in the Edit Form control.

 

6. [Optional] - To ensure we can immediately see the copied record in our Gallery, use the below in the Items property:

 

 

Sort(
    'Your List',
    Modified
    SortOrder.Descending

)

//For Dataverse, use 'Modified On'

 

 

7. On the OnSelect property of the Gallery, enter

 

 

Set(gbl_this_item, ThisItem);
UpdateContext({ctx_copy: false});
EditForm(Form1)

 

 

Note we are using a Global Variable in case your Gallery is on a different screen. You can of course replace this with a Context Variable. E.g.:

 

 

UpdateContext({ctx_this_item: false});
UpdateContext({ctx_copy: false});
EditForm(Form1)

 

 

7. On the Item property of the Edit Form, enter

 

 

gbl_this_item

 

 

8. Insert a Button or a Save Icon into the Screen and on the OnSelect property, enter:

 

 

SubmitForm(Form1)

 

 

9. Insert a Copy Icon into your Screen and on the OnSelect property, enter:

 

 

UpdateContext({ctx_copy: true});
NewForm(Form1);
Notify(
    "Existing record copied.",
    NotificationType.Success
);

 

 

10. Insert a Cancel Icon into your Screen and on the OnSelect property, enter:

 

 

If(
    ctx_copy,
    Notify(
        "Copy cancelled.",
        NotificationType.Warning
    )
);
UpdateContext({ctx_copy: false});
ViewForm(Form1)

 

 

11. On the OnSucess property of the Form, enter:

 

 

UpdateContext({ctx_copy: false});
Notify(
    "Form Saved",
    NotificationType.Success
)

 

 

12. Now the most important part, unlock all the DataCards in your Edit Form.

 

13. For the Default property for each DataCard, enter the following pattern of syntax. Keep in mind that you will need to enter the name of the relevant field bound to that DataCard. For example, for the "Title" DataCard, we have used: 

 

 

If(
    ctx_copy,
    gbl_this_item.'Title Field',
    ThisItem.'Title Field'
)

 

 

14. For the Date field, we have used:

 

 

If(
    ctx_copy,
    gbl_this_item.'Date Column',
    ThisItem.'Date Column'
)

 

 

15. Repeat the same process for the other Data Cards.

 

Final result:

 

Example.gif

 

------------------------------------------------------------------------------------------------------------------------------------------------


If you liked this blog, please give it a Thumbs Up.

Imran-Ami Khan