Create a Custom API using bulk request
This is the continuation of a series on combining Custom Pages and Custom APIs, but also using the Creator Kit and a bit of TypeScript in the process.
In the previous blogpost we have learned how to create a Custom Page using the Creator Kit templates and its ready to use components.
With this first step the UI is already covered, but let’s remember that we need to create multiple Payment Allocation records in Dataverse and also update the selected Quote products with the split %.
So now we are faced with the question “How can I execute this logic to meet the business scenario?”
Well here we have multiple options, we can run this logic within the Custom Page using Power FX applying a patch function to create the records and update the others.
Or we can call a cloud flow sending the corresponding information to it, so the cloud flow can create and update the records.
But let me give you another important point to consider for this type of requirement. How many users are going to use this functionality and it would be nice to have an average of how many Payment Allocation records are going to be created per day. In the end, it is more than likely that several users will be using this
feature at the same time.
So it is important to understand that if we are talking about many users, maybe running that kind of logic within the Custom Page is not the best idea. For example, let me give you the numbers for my business case:
I have 200 users on this project, and all of them are using this feature, the average number of users working at the same time is 80 and the average number of quotes created per day is 50. So even assuming low numbers like each quote only has two products, that would be 100 quote products and assuming each quote product
only has two Payment Allocation records, then we need to create 200 records and update another 100 records as well.
That said, I think we should delegate this logic and its number of records to be created and updated to something else. And don’t get me wrong, I still believe Power FX or a cloud flow could handle these numbers, but I also believe there is a better option in terms of performance to handle these numbers much faster.
That option is the Custom API, at the end we can send the corresponding information to the Custom API and we can even create and update all the records in a single call to Dataverse, and if something goes wrong we can do a rollback and not create or update anything, how cool is that?
using the Web API.
Is worth to mention that when we want to create a custom API, we’ll need three components:
- The plugin
- The custom API and parameters records (we can create these using make.powerapps.com)
- A way to call the custom API, as the options I mentioned before.
So without further introduction, let’s start creating these components. We will start with the code so let’s prepare or solution and projects in Visual Studio.
Create a Core and a Main project
I would to start explaining that I like to separate the server side logic in different projects, the Main project contains the Custom API or even other plugins separated in a different folder of the same project.
And the Core project contains all the handlers classes that will be in charge of the heavy work like validate something or performing CRUD operations.
So, open your last version of Visual Studio, and create a Class library .NET Framework project:
In my case I like to give a more general name to the solution and a more specific name to each project, but in the end you can create it as you feel more comfortable, this first project will be the Core one:
Now let’s create the Main project by adding a new project to the solution:
Select the same Class library .NET Framework project, but this time give a different name to the project:
Now under the Main project let’s add a new folder to store there all the Custom APIs, in this case is just one but maybe in your project you’ll to create more than one:
Then name of the folder is “Custom API”, and under that folder finally let’s create our Custom API:
Select a C# Class and give the name “CreatePaymentAllocations.cs”:
Now we can start developing
Create the Plugin
After creating the CreatePaymentAllocations.cs, you have to add the following code:
public class CreatePaymentAllocations : IPlugin
public void Execute(IServiceProvider serviceProvider)
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
var input = (string)context.InputParameters[“p365i_payments”];
RootPayments root = Core.Helpers.Common.JsonDeSerialize<RootPayments>(input);
var paymentAllocationHandler = new Core.Handlers.PaymentAllocationHandler(tracingService, service);
Now your code should have multiple errors but don’t worry because we are just getting started:
Next step is to solve this errors step by step.
First position your mouse over ITracingService object, select show potential fixes and then select “Install package Microsoft.CrmSdk.CoreAssemblies” and then select “find and install latest version”: