Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
I am back with another blog and this time it’s a blog on canvas apps. And quite an interesting requirement as well. It’s about creating accounts in bulk from a Canvas app.
So basically here is the requirement. My customer had a form like the one below through which they want to create customers in bulk. Well it was much prettier but this is what I conjure up the closest to what we had.
The picture is kind of crude but I guess you get the idea. The idea is to create customers in bulk. The data shall be saved to Account entity in Common Data service (CDS).
Commonly when we display CDS data inside a canvas app, we display it using form control.
While form control is great, it does not solve our purpose here. Using form control we won’t be able to save multiple records in one operation. So what is the alternative?
The alternative is to use ForAll function and the Patch function. All new customer record information was collected inside a Collection variable with the formula below.
Collect(AccountItems, {'name' : "Contoso Inc", 'accountnumber': "12345", 'creditlimit': 2000},
{'name' : "Adventure Works", 'accountnumber': "23456", 'creditlimit': 3000},
{'name' : "Contoso Electricals", 'accountnumber': "56789", 'creditlimit': 4000})
I have used the property name similar to schema names for Account entity in CDS so that I can directly map them while creating the record in CDS. So far so good.
So what’s the next thing? I am going to create all these accounts in bulk on click of the “Save” button. Below is the code “OnSelect” of the button.
Quite easy actually. I am iterating through the collection variable and for each record in the collection, I use the Patch function to submit the record to CDS. And once I run the operation, I could see the records are getting created successfully!
But wait. The fun is not over. Now what if I say there is a better way to do this? Yes you heard it right. And thanks to Nataraj Yegnaraman for guiding me here and pointing to this post. You can follow his wonderful blog here.
Now coming to the blog and if you have already browsed the above link, you are aware that using only Patch function instead of ForAll, you can perform efficient bulk operations quite faster. For our case the code will be the following. Simple and fast.
You will also like the below posts
Hope this helps!
Debajit Dutta
(Business Solutions MVP)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.
Hi Debajits,
How and where do you define the variable? Did you use Gallery? Sorry, I am newbie on Power Apps didn’t exactly understand.
Thanks in advance.
Necdet
Hi nescar,
Thanks for reading my blog. I have declared collection in App Onstart event.
Cheers!
Debajit