Finally, I found how to call Custom Action from Xrm.WebApi.execute function. The funny thing is, the method has already been in place for a long time. I just realized how to call it after I have the curiosity to get the EnvironmentVariable effectively in Javascript (for calling Custom Web API in Plugin Development, you can refer to this blog post) and found Carl De Souza blog post and check furthermore in the official documentation. 

So for this blog post demonstration, we will call 2 Custom Actions (GetEnvironmentVariable and GetTeamEmailRequest) that I already prepared (you can download the unmanaged solutions that you can find here). The idea of "effective" is still the same as the previous blog post about the effective way of calling Custom Web API series: we (as the developer) need to have a collection of classes that represent the Custom API that we create. So we don't need the hassle to remember all the requests by ourself and we can pass the collections to your teams to be reused by them.

The Code

So here is the sample code for the 2 Custom API that I created:

 var Insurgo = Insurgo || {}; Insurgo.CustomApi = Insurgo.CustomApi || {}; var ACTION_TYPE = 0; (function () {     this.GetTeamsEmailRequest = function (teamName, isEmail) {         this.TeamName = teamName;         this.IsEmail = isEmail;     };     this.GetEnvironmentVariable = function (envName) {         this.EnvironmentVariableName = envName;     }; }).apply(Insurgo.CustomApi); Insurgo.CustomApi.GetTeamsEmailRequest.prototype.getMetadata = function () {     return {         boundParameter: null,         parameterTypes: {             TeamName: {                 typeName: "Edm.String",                 structuralProperty: 1             },             IsEmail: {                 typeName: "Edm.Boolean",                 structuralProperty: 1             }         },         operationType: ACTION_TYPE,         operationName: 'ins_getteamsemail'     } }; Insurgo.CustomApi.GetEnvironmentVariable.prototype.getMetadata = function () {     return {         boundParameter: null,         parameterTypes: {             EnvironmentVariableName: {                 typeName: "Edm.String",                 structuralProperty: 1             }         },         operationType: ACTION_TYPE,         operationName: 'ins_getenvironmentvariable'     } }; 

From the above code, the things that you must understand are:

The object parameterTypes' name equal with the function's variable name

The object name of the parameterTypes must same as the variable name of the method and must be the same as the Custom API Parameter name. You can check all this information from metadata URL like the below picture:

Our Custom API Metadata

The typeName, you can copy-paste from what you found in metadata.

The operationType definition are:

  • Action : 0
  • Functions: 1
  • CRUD: 2

*If you set the operationType wrongly, then your function will be not triggered.

How to Execute?

When we want to execute the requests, we just need to execute them using the below code:

 var req = new Insurgo.CustomApi.GetTeamsEmailRequest('Testing Team', true); Xrm.WebApi.execute(req).then(success => success.json()).then(success => console.log(success)); var req1 = new Insurgo.CustomApi.GetEnvironmentVariable('msdyn_IncidentShouldValidatePrimaryContact'); Xrm.WebApi.execute(req1).then(success => success.json()).then(success => console.log(success)); 

Here is the result from the console:

The result from console

What do you think?

* You can check the the source code (Solution, Custom API, Plugin, and the Javascript) in here.


This free site is ad-supported. Learn more