Dynamics 365 Different ways to test Custom Actions
1. Introduction:
Testing is an important part of software development. Have you ever implement an Action and feel confused in execute your action? If yes, this article is what you finding because I will share some methods to execute Custom Action in a specific context which you could apply also to Workflow in Dynamics 365.
2. Execute action methods:
a. JavaScript:
This is one of the most basic methods to execute an Action. The idea is you will write simple code and run it directly in the console of browser. You could find the example code below and change some parameters such as: actionName, version of Dynamics. In the example code, I put 2 debuggers that will be paused when execute response is successful or fail in order to check the result and debug.
This method could be applied for all versions of Dynamics.
function CallGlobalCustomAction() {
//get the current organization name
var serverURL = Xrm.Page.context.getClientUrl();
//query to send the request to the global Action
var actionName = "new_MyCustomAction"; // Global Action Unique Name
//Pass the input parameters of action
var data = {};
//Create the HttpRequestObject to send WEB API Request
var req = new XMLHttpRequest();
//Post the WEB API Request
req.open("POST", serverURL + "/api/data/v8.0/" + actionName, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 200 || this.status == 204) {
debugger;
// TODO
alert("Action Executed Successfully...");
}
else {
debugger;
var error = JSON.parse(this.response).error;
alert("Error in Action: " + error.message);
}
}
};
//Execute request passing the input parameter of the action
req.send(window.JSON.stringify(data));
b. Postman:
This method has different usage between the Online and On-premises versions. The idea is you will send an request to Dynamics Server to execute action. However, the authentication of 2 version is not the same.
- On-Premises version: Select Post request and input your Custom Action Url
- In tab Authorization of Postman, select NTLM Authentication method for type then input your username, password, and Domain
- In tab Body, select raw then add input parameter for action in JSON format then press Send
- Online version: You need to send 2 requests: First request to get an access token and the second request to execute the action.
- First request: This request is send to Tenant to get access token and each token will be only valid for around 10 minutes. You need input information of your environment: client id, client secret, scope. Please refer to this article to get more information.
- Second request: same as On-premise version above except Select Authorization type as Bearer Token and paste access token which you get from first request.
This access token could be used to execute other stuff in Azure like Azure Function, Webjobs so if you work with action as well as Azure Function, this is the best method for testing with your context because you not only execute Action in Dynamics 365 but also Azure function in the same tool.
c. Custom Action Tester tool:
This is the easiest way to execute your action because it does not require knowledge about HTTP request as well as coding skills, especially you are working with the plugin of action. Custom Action Tester is an available tool in XrmToolbox. Here are the UI:
What you need is:
- Connect to your environment
- Select Solution
- Select Action name
- Input parameters
- Press Execute Custom Action
3. Conclusion:
Everyone has different situation so you need to select suitable strategy. In the table below, I compare the advatage and disadvantage among methods in this article. What you need is select the best one for your work.
|
Pros |
Cons |
Should use in |
JavaScript |
- Exactly with what we will code in client side (use it to test then copy to code and deploy J)
|
Complex, require code base |
- Simple exercise invoke action |
Postman |
- Simulate real response
- Could invoke other service in Azure
|
Complex in online version |
- On-premises (easy to login)
- Work with other service in Azure such as Azure function
|
Custom action tester tool |
- Easy to use, not require code base
|
Work not well in complex response such as output type entity collection |
- Test/debug plugin of action |
Add new comment