Execute Workflow using Web API in Dynamics 365


Sharing sample code to call workflow using Web API from JavaScript.
First, we will create a normal workflow which works on demand and a create task with predefined subject and description and activate the workflow.Workflow

Copy the workflow Id and save it to use in JavaScript function.Call below JavaScript function from ribbon button from Lead entity.

function CallWorkflow() {
var workflowId = “71A6BC35-16D8-4447-8ADE-F040CDAE9524″;
var clientURL = Xrm.Page.context.getClientUrl();
var leadId = Xrm.Page.data.entity.getId().replace(‘{‘, ”).replace(‘}’, ”);
var data = {
“EntityId”: leadId
};
var req = new XMLHttpRequest();
req.open(“POST”, clientURL + “/api/data/v8.2/workflows(“+workflowId+”)/Microsoft.Dynamics.CRM.ExecuteWorkflow”, 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) {
var data = JSON.parse(this.response);
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send(JSON.stringify(data));
}
For more details, please refer below URL
http://www.inogic.com/blog/2016/11/execute-workflow-using-web-api-in-dynamics-365-2/
hope this helps.

 

Execute Action using Web API in Dynamics 365


Sharing simple scenario to call custom action from JavaScript (from ribbon button).
First of all, create an Action for Sales Order entity and a step to change status to Active(New) as shown below.
Action

Call below function form ribbon button to change the status of Sales Order to New.

function ChangeAgreementStatus() {
var Id = Xrm.Page.data.entity.getId().replace(‘{‘, ”).replace(‘}’, ”);
var clientURL = Xrm.Page.context.getClientUrl();

// pass the id as inpurt parameter
var data = {
“agreementid”: Id
};

var req = new XMLHttpRequest();

// specify name of the entity, record id and name of the action in the Wen API Url
req.open(“POST”, clientURL + “/api/data/v8.2/salesorders(” + Id + “)/Microsoft.Dynamics.CRM.new_ActivateAgreement”, 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) {
var data = JSON.parse(this.response);
alert(data);
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};

// send the request with the data for the input parameter
req.send(window.JSON.stringify(data));

//Refresh form
Xrm.Page.data.refresh();
}

For details, please refer below URL.

https://community.dynamics.com/crm/b/nishantranaweblog/archive/2017/05/27/sample-code-to-call-action-using-web-api-in-crm

hope this help you

Debug WCF Restful service (POST/GET) – Part 2


There are many way to debug WCF/Cloud service hosted in Azure and you can refer the below tutorial but I prefer to debug the service old and easy way by deploying the service in local IIS then publish to Azure.

https://docs.microsoft.com/en-us/azure/vs-azure-tools-debug-cloud-services-virtual-machines

https://docs.microsoft.com/en-us/azure/vs-azure-tools-debugging-cloud-services-overview

In Part 1, I explain how to create and deploy WCF Restful service in Azure.

https://arvindcsit.blog/crm-and-azure-integration/

I will use the same existing code and will add another POST method to explain debugging.

I am going to create one POST method which takes User Name and Password as request body and return predefined token.

So I added below code in Part 1 source code.

Contract: ICRMService.cs

Under ServiceContract

[OperationContract]
[WebInvoke(Method = “POST”,
UriTemplate = “/getauth”,
ResponseFormat = WebMessageFormat.Json)]
CRMTokenResponse GetCRMToken(CRMTokenRequest tokenReqeust);

DataContract
[DataContract]
public class CRMTokenResponse
{
public CRMTokenResponse()
{
this.Token = “”;
this.Error = “”;
}
[DataMember(IsRequired = false)]
public string Token { get; set; }
[DataMember(IsRequired = false)]
public string Error { get; set; }
}
[DataContract]
public class CRMTokenRequest
{
[DataMember]
public string Username { get; set; }
[DataMember]
public string Password { get; set; }
}
CRMService.svc.cs
public CRMTokenResponse GetCRMToken(CRMTokenRequest tokenRequest)
{
CRMTokenResponse resp = new CRMTokenResponse();
if (tokenRequest != null)
{
string userName = WebConfigurationManager.AppSettings[“CRM.Username”];
string password = WebConfigurationManager.AppSettings[“CRM.Password”];
string tokenId = WebConfigurationManager.AppSettings[“CRM.Token”];
if (tokenRequest.Username == null || tokenRequest.Password == null)
{
resp.Error = “Enter UserName and Password”;
return resp;
}
if (tokenRequest.Username.Equals(userName) && tokenRequest.Password.Equals(password))
{
resp.Token = tokenId;
}
else
{
resp.Error = “Invalid Username & Password”;
}
}
else
{
resp.Error = “Request is Null”;
}
return resp;
} 

Web.config

Add these three lines under <appSettings></appSettings> tag.
<add key=”CRM.Username” value=”Arvind” />
<add key=”CRM.Password” value=”Singh” />
<add key=”CRM.Token” value=”8178746c24b9f5544d259f2a38d7c1a785bc70a2″ />

We are done with code changes, make sure you build the project in Debug mode and deploy in local IIS.

Browse the service and make sure the service is running without any error. I will use PostMan to hit and debug the service.

Set the breakpoint and attached to W3WP process. Select the Content Type and Request Body as below.

Then Click “Send” and cursor will stop at breakpoint as shown below.

you can see the token value while debugging as below

Finally, you get output in output window of PostMan as below.

Similarly, you can debug it for GET method. Once your service is working fine, you can publish your service to Azure using steps mentioned in PART 1 (https://arvindcsit.blog/crm-and-azure-integration/)

Hope this helps you.

 

 

 

 

%d bloggers like this: