Using PowerFX with commands for cloning records with N to N relationship

Today we will learn how to create a command bar to clone a record with many-to-many relationships. For those who don’t know, in Model-Driven-Apps, we can create a command bar (previously known as “ribbon button”) that uses PowerFX (low code) query.

First, I created 2 tables for the demonstration (Student and Subject). Then, I created many-to-many relationships between those tables like in the picture below:

Created many to many relationships

Next, you can edit your MDA (Model Driven App) and initiate the command designer (for detailed implementation you can click here):

Open command designer

A new dialog will appear, and you can select which command bar that you want to create. For this demo, I will select “Main form“:

Edit command bar dialog

Next, you can choose which kind of customization that you like. For this demo, I select the Power FX:

Create a component library

Once the Command Designer is loaded, you can create the button that you want. Here, I created a button with the label name “Clone Record”. As we need to enable advanced settings, we need to click “Open component library”:

Open component library

Based on the information that I learned from Flow Joe in this YouTube video, we need to click “Settings” > go to “Upcoming features” > “Experimental” > Enable “Record scope one-to-many and many-to-many relationships“:

Enable "Record scope one-to-many and many-to-many relationships"

Once this is ready, we can focus on the logic to “clone” the record:

If(Confirm("Are you sure to clone this record?"), With(
    { 
        newStudent: Patch(Students, Defaults(Students), { 'Full Name': Self.Selected.Item.'Full Name' & " (Copy)" }), 
        allSubjects: Self.Selected.Item.Subjects 
    }, 
    ForAll(allSubjects, Relate(newStudent.Subjects, ThisRecord)); Navigate(newStudent))
)

As you can see in the above code, we use the Confirm method to give a dialog for the user to select. If the user approves it, then we will use the With function to give variables scope and formula that we want to run. For this purpose, we will be passing:

  • newStudent: To create a new Student record, we can use the Patch function (which can be used to create a new record if we pass the default data source). Because I want to keep it simple, the property that we need to pass is just the “Full Name” and we add the literal string “(Copy)” for it.
  • allSubjects: get all the original record’s Subjects

Last, we need to loop all the allSubjects. For making an association (N-to-N) between the new record and the allSubjects, we need to use the Relate function. The final part is to Navigate to the new record data.

Once you are done, you can click save and publish. Here is the demo:

Happy CRM-ing!

Author: temmyraharjo

Microsoft Dynamics 365 Technical Consultant, KL Power Platform User Community Leader, Student Forever, Test Driven Development, and Human Code enthusiast.

11 thoughts on “Using PowerFX with commands for cloning records with N to N relationship”

  1. Hello…THANK YOU for this post. This many-to-many via a MDA Button is exactly what I have been looking for for several days. NO other website shows how to do this. All of those use Canvas app.

    I have enabled the Experimental feature as suggested. However, I still get a syntax error on this line…

    ForAll(allSubjects, Relate(newStudent.Subjects, ThisRecord));

    The “newStudent.Subjects” is invalid as the first parameter/argument in the Relate() function. And, I don’t even get the intellisense to add “Subjects” to “newStudent.”.

    Do you have the current SOLUTION for this? I am finally within 1 object/variable of a solution. Thank you. Ron

    Like

    1. Hey, glad if my post is helping you!
      In my sample, as Student has 1 to many relationships to the Subject table. That’s why the IntelliSense able to catch that. So you need to check the table design. Hope this helps!

      Like

      1. Temmy, seems you did NOT mention that in your M-to-M example. Why do you have a 1 to many AND m-to-m relationships? The two tables I am working with (I am not the database admin so have to live with what is there) ONLY have M-to-M between the two tables (no 1-to-M like you mentioned here). Can you provide the PowerFX code that would work for MY situation? Again, suspect there are many M-to-M table relationships out there “in the world” that do NOT have the 1-to-M also. Respectfully and thank you…I simply cannot figure out how to do this with M-to-M. 

        Another person suggested this (which does work!!) but it was from a CANVAS app and so I do NOT know how to move it to the MDA Button OnSelect() Function correctly…any thoughts?

        This example uses Teacher table and Course table (teacher could be student) and there is a M-to-M between these tables with NO 1-M relationships between them. The “UpdateContext” only works for Canvas app so need to change to With() for MDA. The BOLD line below has galTeachers which is a gallery and can’t be part of the MDA solution either. Thank you. Ron

        UpdateContext(

            {

                cxtTeacher: Patch(

                    RonTestTeachers,

                    {

                        Name: “Jordan”,

                        RonTestTeacher: GUID()

                    }

                )

            }

        );

        ForAll(

            Filter(

                RonTestCourses,

                RonTestCourse in galTeachers.Selected.RonTestCourses.RonTestCourse

            ) As i,

            Relate(

                LookUp(

                    RonTestTeachers,

                    RonTestTeacher = cxtTeacher.RonTestTeacher

                ).RonTestCourses,

                i

            )

        )

        Like

      2. Oh sorry! Yes2. The relationship is many to many. Do you mind sharing your current Power FX code?
        Is the newStudent already the type of Student? Based on your reference is it newRonTestTeacher? As long as newRonTestTeacher type is Teacher, you are supposed to get the newRonTestTeacher.RonTestCourses.

        Like

  2. Hello…THANK YOU for this post. This many-to-many via a MDA Button is exactly what I have been looking for for several days. NO other website shows how to do this. All of those use Canvas app.

    I have enabled the Experimental feature as suggested. However, I still get a syntax error on this line…

    ForAll(allSubjects, Relate(newStudent.Subjects, ThisRecord));

    The “newStudent.Subjects” is invalid as the first parameter/argument in the Relate() function. And, I don’t even get the intellisense to add “Subjects” to “newStudent.”.

    Do you have the current SOLUTION for this? I am finally within 1 object/variable of a solution. Thank you. Ron

    Like

  3. Temmy…thank you so much.

    Two tables… RonTestTeacher (RonTestTeachers) and RonTestCourse (RonTestCourses). The two tables have M-to-M relationship just like your example. There is NO other relationship that I created (all the others are defaults).

    Neither ForAll() below works. That is because there is NO 1-M relationship that you had in your example. Again, I cannot change the database relationships as I am not the DB Admin. By the way, these 2 tables are just my simple way to try to test the M-to-M situation. The REAL tables (are confidential due to secret clearance) are not shown here BUT they ONLY have the M-to-M relationship (Just like my Teacher and Course). Thank you.

    Ron

    With(

        {

            ctxTeacher: Patch(

                RonTestTeachers,

                {

                    RonTestTeacher: GUID(),

                    Name: Self.Selected.Item.Name & ” Clone April 5″

                }

            ),

            allCourses: Self.Selected.Item.RonTestCourses

        },

        ForAll(allCourses, Relate(RonTestTeachers, RonTestCourses)

        //ForAll(allCourses, Relate(ctxTeacher.RonTestTeacher, ThisRecord))

    );

    Like

    1. Temmy…wonder if it might be easier for you to REMOVE The 1-M Relationship you have in your two tables leaving just the M-to-M relationship between Student and Subject and then see if you can clone the Student and have the new student object related to the same Subjects as the original student? Just a thought. Again, it is pretty unusual to have the 1-to-M AND the M-to-M in typical relational tables. Thank you, Ron

      Like

    2. Yes Ron, previously I’m wrong giving u information about 1-N which is supposed to be N-N.

      Can you try this:
      ForAll(allCourses, Relate(ctxTeacher.RonTestCourses, ThisRecord));

      Like

      1. Temmy…that does not work either. Error message…RonTestCourses isn’t valid; name not recognized. Thank you for the suggestion.

        Like

      2. For the Patch function, because you want to create a new row. Suppose you can use Defaults(RonTestTeachers) right?

        Something like: Patch( Customers, Defaults( Customers ), { Name: “Contoso” } )

        Like

  4. Temmy…I think you are getting off the real issue and I am pretty sure you don’t really know how to do it. Thank you for trying.

    What I need to do is what your example DID (relate M-to-M) but your example had a 1-M relationship that allows your example to work. If you remove your 1-M relationship I bet you cannot figure out how to do the relationship that is M-to-M when you clone your Student (new table row) and then relate this new table row (new student) to the same/existing Subjects without creating any NEW Subject rows. If you do figure it out, and take on this friendly challenge from me, please let me know. Otherwise…best to you 15 hours ahead of me in the State of California in the USA.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.