Using a Canvas PowerApp to create records in CDS / Dynamics in complex scenarios

I’ve been spending a lot of time on PowerApps recently and I wanted to share what I have learnt about creating records in CDS. None of this is very revolutionary however I certainly had to read through the documentation a few times to figure this all out so hopefully this saves you some time.

When you want to create a record in CDS via a Canvas App you have two options. The easiest and simplest method is to use an edit form control. In more complex scenarios however you end up needing to use the Patch function. Typically its triggered on change of a field or on select of a button and looks like this:

Patch(CDS_Entity, Defaults(CDS_Entity), {field_logical_name_1: "input", field_logical_name_2: "input"});

Instead of “input” you can obviously refer to an input controls “.text” or “.selected” value. The power of the Patch function being that value can be a locally calculated or anywhere in the app allowing for a ‘multiple page create form’.

So next what happens if you want to create a record in CDS and along with populating some ‘normal’ fields populate an option set. Simply, you give the option set its own set of curly braces. It looks like this:

Patch(CDS_Entity, Defaults(CDS_Entity), {field_logical_name_1: "input", field_logical_name_2: "input"} ,{option_set_logical_name:'Option Set (CDS_Entity)'.value})

With the option set value, if its dynamic I prefer to stitch up the choices to a combo box and then have the selected value of the combo box referenced in the above formula. But hopefully you get the idea!

Okay, now what if we have a much more complicated example. I want to create two records on the same event that relate to each other. Say an account and its primary contact. Effectively I want two ‘forms’ but one submit button. For this we need to use a Set function. Set allows you to declare a global variable. That global variable can be a record. The return value of a Patch function is the record you created (or modified). So we need to create the first record and then capture it. This looks like this:

Set(recContactRecord, Patch(Contacts, Defaults(Contact), {lastname: "Contact Name"}));

Now that we have the record in a variable we can create the next record immediately after with this formula.

Patch(Accounts, Defaults(Account), {accountname:"Account Name", Account_primary_contact: recContactRecord});

I think this is pretty cool! It is possible to instead of referring to a variable actually Patch the other record within the Patch formula but it gets very messy. And that doesn’t help if you would like to make multiple children. I’ve worked on a few scenarios now where we collect locally a set of records, create a parent and then use a ForAll function to create the children in CDS. As a bonus that looks like this:

ForAll(MyLocalCollection, Patch(CDS_Entity, Defaults(CDS_Entity), {parentrecord: recGlobalVariable, field_logical_name: "input"});

Hope this is useful to someone!

Leave a Reply

Your email address will not be published. Required fields are marked *