Custom APIs
You have been using global actions or entity bound actions to perform some steps inside your environment calling them through the API with Javascript or probably you are calling them with some C# code doing an execute, but you have been always using Actions to perform logic inside your CRM from an external source.
Quite recently, Microsoft added a new post saying: βEh! We have developed Custom APIs, a thing you have been seeing in your ribbon in the make.powerapps.com and now it has some value to youβ
Right, that was not the post, this is the correct one: https://powerapps.microsoft.com/en-us/blog/introducing-custom-api-preview
However, that was not the first time I saw that because Mark Carrington (@MarkMpn) wrote this article: https://markcarrington.dev/2020/10/22/cds-custom-apis/
His stuff is much better than mine, thatβs absolutely sure, so, what about if we create a Custom API step by step and we can see how this is done?
If you go to the Microsoft docs, you can see that there are two ways of creating the Custom API. One is creating it by code, and the second one is going to the Maker Portal. For those who doesnβt know what a Maker Portal is: It is just creating some new records from your Dataverse environment but in a fancy way.
What are we going to do?
So just to let you know, we will first create a new solution, then we will create our API, add it to the solution, register it with Plugin Registrion (I will use XrmToolBox), use it it a Power Automate and finally we will try it.
Boommm! π₯π₯ Done. Seems easy eh? Thatβs because it is quite easy.
Just for your interest, we will create a global βactionβ that will βPOSTβ a search term to https://icanhazdadjoke.com and then it will return a joke that we will write in our entity.
Are you interested right now in the code? Well, then go to: https://github.com/victorsolaya/customapi-dadzjoke
Letβs work on it
First, what are the entities we are going to use?
CUSTOM API
Letβs fill the Custom API with the following items:
Letβs create our request parameter, that is going to be the search item.
REQUEST PARAMETER
Letβs fill the Request Parameter Search with:
RESPONSE PARAMETER
Letβs fill the Response Parameter Joke with:
FYI
Just for your interest, I created a new table (entity) called Joke that I will use for posting purposes π
So everytime we create a record or we update the Search field we will update the joke.
I mean, this is just an example, probably I wonβt ever do this in a production environmentβ¦
Letβs continue
Of course, add those Custom API and Request / Response parameters to the solution, always best practices and ALM ahead of everything!
Then, we are going to create a new plugin, as the one in the github: https://github.com/victorsolaya/customapi-dadzjoke
After you have created all your code, (I am not going to paste it in here, because thatβs why things like Github exist π) register your assembly with Plugin Registration.
Add your Plugin to the Custom API
Now, you need to bound the Plugin you have created into the API you created in the first step.
For that we are going to add, if you remember the Plugin Type:
And thatβs it! You have created the Custom API.
Nowβ¦what?
Of course, letβs use our perfect Custom API!
Power Automate
For testing this, I have created a Power Automate, but you can use a Javascript or whatever you want. I just wanted to show to you how this is shown in a Power Automate and how the Request / Response Parameters are shown in a Flow.
As I said before, we are going to use the Create / Update trigger option, so when the record is update or created we are going to populate the joke field.
Howeverβ¦
π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨ π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨
WHAT ABOUT IF YOU CODE IT YOURSELF THE POWER AUTOMATE AND THEN YOU CHECK THE SOLUTION IN THE NEXT STEP?
π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨ π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨π¨
And this is how the response is shown in the Power Automate (In the previous image was not clear)
Example
So letβs see the final example when we have everything done and we create our joke record:
Just to check once we change the search term the joke is changed I will leave a GIF.
(It takes some time since it is a workflow and I refreshed several timesβ¦π)
Conclusion
Custom API is still in Preview but it seems that is going to replace the Actions we have right now in the Dataverse, so be prepared to change them.
They have a lot of future, since now all the logic can be quite easily done in a Plugin, and as developers it is quite good or thatβs my perception.
As always, if you have any question or whatever, donβt hesitate to contact me at me@victorsolaya.com