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

Community site session details

Session Id :
Power Apps - Microsoft Dataverse
Unanswered

Model driven App Clone records, child records and grandchild records.

(0) ShareShare
ReportReport
Posted on by
Hi All, 
 
I'm finding it very difficult to clone all the records associated with a parent record, the code just simply clones the parent record but not the child records. any help or advise would be much appreciated. 
 
This code is added to a command bar button.
 
ForAll(
  Self.Selected.AllItems As selContract,
  With(
    {
      clonedContract:
        Patch(
          'GIND UK Contracts',
          Defaults('GIND UK Contracts'),
          {
            'Notes (cr666_notes)': selContract.'Notes (cr666_notes)' & " COPY - "
            // add other Contract fields to copy here if needed
          }
        )
    },
    With(
      {
        ChildContractTask:
          Filter(
            'GIND UK Contract Tasks',
            'Contract'.'Contract Number' = selContract.'Contract Number'
          )
      },
      ForAll(
        ChildContractTask As ct,
        With(
          {
            CreatedChildContractTask:
              Patch(
                'GIND UK Contract Tasks',
                Defaults('GIND UK Contract Tasks'),
                {
                  'Comments ': ct.'Comments ' & " (COPY)",
                  'Contract ': clonedContract
                  // add other Task fields to copy here
                }
              )
          },
          With(
            {
              GrandChildLabour:
                Filter(
                  'GIND UK Task Labour',
                  'Contract Task'.'Contract Task ID' = ct.'Contract Task ID'
                )
            },
            ForAll(
              GrandChildLabour As gl,
              Patch(
                'GIND UK Task Labour',
                Defaults('GIND UK Task Labour'),
                {
                  Comments: gl.Comments & " (COPY)",
                  'Contract Task': CreatedChildContractTask
                  // add other Labour fields to copy here
                }
              )
            )
          )
        )
      )
    )
  )
);
Notify("Contract and child records copied", NotificationType.Success)
 
I have the same question (0)
  • sannavajjala87 Profile Picture
    47 on at
    Model driven App Clone records, child records and grandchild records.
    Hi,
     
    You are very close. 
     
    The reason only the parent record gets cloned is that your Filters for the child and grand-child tables probably return no rows.
     
    You’re filtering on 'Contract'.'Contract Number' and 'Contract Task'.'Contract Task ID', which are fields on the lookup record. It’s more reliable to filter directly by the lookup column itself (i.e., compare records, not inner fields).
     
    Try this simplified version (same structure, but notice the Filters and lookup assignments):
     
    ForAll(
        Self.Selected.AllItems As selContract,
        With(
            {
                clonedContract:
                    Patch(
                        'GIND UK Contracts',
                        Defaults('GIND UK Contracts'),
                        {
                            'Notes (cr666_notes)':
                                selContract.'Notes (cr666_notes)' & " COPY - "
                            // other Contract fields...
                        }
                    )
            },
            // CHILD: Contract Tasks
            ForAll(
                Filter(
                    'GIND UK Contract Tasks',
                    'Contract' = selContract // <-- use lookup, not Contract Number
                ) As ct,
                With(
                    {
                        CreatedChildContractTask:
                            Patch(
                                'GIND UK Contract Tasks',
                                Defaults('GIND UK Contract Tasks'),
                                {
                                    'Comments ': ct.'Comments ' & " (COPY)",
                                    'Contract ': clonedContract // <-- link to new parent
                                    // other Task fields...
                                }
                            )
                    },
                    // GRANDCHILD: Task Labour
                    ForAll(
                        Filter(
                            'GIND UK Task Labour',
                            'Contract Task' = ct // <-- use lookup, not ID field
                        ) As gl,
                        Patch(
                            'GIND UK Task Labour',
                            Defaults('GIND UK Task Labour'),
                            {
                                Comments: gl.Comments & " (COPY)",
                                'Contract Task': CreatedChildContractTask
                                // other Labour fields...
                            }
                        )
                    )
                )
            )
        )
    );
     
    Notify("Contract and child records copied", NotificationType.Success)
     
    Key changes:
     
    Filter child tasks with 'Contract' = selContract.
     
    Filter labour records with 'Contract Task' = ct.
     
    Use the result of Patch (clonedContract, CreatedChildContractTask) when setting the lookup columns on the new records.
     
     
    If you drop a Label with CountRows(ChildContractTask) / CountRows(GrandChildLabour) while testing, you should see those now return > 0 and the children/grandchildren get created.

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

Coming soon: forum hierarchy changes

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

Chiara Carbone – Community Spotlight

We are honored to recognize Chiara Carbone as our Community Spotlight for November…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 624 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 384 Super User 2025 Season 2

#3
developerAJ Profile Picture

developerAJ 246

Last 30 days Overall leaderboard

Featured topics