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 Filled

CUSTOM API

DOCS: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/custom-api#customapi-entity-attributes

Let’s fill the Custom API with the following items:

Custom API Filled

Let’s create our request parameter, that is going to be the search item.

REQUEST PARAMETER

DOCS: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/custom-api#customapirequestparameter-entity-attributes

Let’s fill the Request Parameter Search with:

Custom API Request Parameter

RESPONSE PARAMETER

DOCS: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/custom-api#customapiresponseproperty-entity-attributes

Let’s fill the Response Parameter Joke with:

Custom API Request Parameter

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…

Probably GIF

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.

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:

Plugin Type Custom API

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?

CODE YOURSELF

🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨

Solution Power Automate

And this is how the response is shown in the Power Automate (In the previous image was not clear)

Solution Power Automate Response

Example

So let’s see the final example when we have everything done and we create our joke record:

Joke created

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β€¦πŸ™„)

Joke created

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


Join the newsletter