Creating and Debugging Custom Workflow Activities with CRM 2011 and Visual Studio 2012

Leave a comment

By Hector Villafuerte. 

Hector Certifications

When the CRM 2011 out-of-the-box cannot accomplish the implementation of a workflow activity, step or condition, then we need to implement a workflow custom activity using Visual Studio.

For example, every time we update the ticker symbol in an account, we might need to get the annual revenue from a financial web service like Google Finance or Yahoo Finance.

If you don’t have the CRM 2011 SDK installed, download from this link:

http://www.microsoft.com/en-us/download/details.aspx?id=24004

Once you finished the SDK installation, navigate to the installation folder: sdktoolsdevelopertoolkit

Install: crmdevelopertoolsvs12_installer.msi

After install the CRM Developer Tool for VS 2012, you should be able to see the CRM project templates. Select the Dynamics CRM Workflow Library.


Fill out the information required in the “Connect to Dynamics CRM Server” dialog.


Notice the assemblies added to the project automatically are:

Microsoft.Xrm.Sdk.dll and Microsoft.Xrm.Workflow.dll

You also need to add the references to the following assemblies:

System.Activities

System.ServiceModel

Verify if you have all the assemblies needed in the Solution Explorer.

Add a new CRM Package to the solution.

In the new CRM Package Project, add a reference to the WFActivity Project.

 

In the WFActivity Project, add new item WFActivity.cs class.

Populate the Custom Workflow dialog.

 

 

The class WFActivity.cs is created with code, ready to be completed.

Add the following properties to the WFActivity class.


//Define the properties

[Input(“Account”)]

[ReferenceTarget(“account”)]


public
InArgument<EntityReference> Account { get; set; }

 

[Output(“Anual Revenue”)]

[AttributeTarget(“account”, “revenue”)]


public
OutArgument<Decimal> AnualRevenue { get; set; }

 

Add the logic under the comment TODO.

// TODO: Implement your custom Workflow business logic.

 

 


EntityReference account = this.Account.Get(executionContext);

 


Entity accountEntity;

{


//Create a request


RetrieveRequest retrieveRequest = new
RetrieveRequest();

retrieveRequest.ColumnSet =


new
ColumnSet(new
string[] { “tickersymbol” });

retrieveRequest.Target = account;

 


//Execute the request


RetrieveResponse retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);

 


//Retrieve the Loan Application Entity

accountEntity = retrieveResponse.Entity as
Entity;

}

 


string tickerSymbol = string.Empty;


decimal? anualRevenue = null;

 


if(accountEntity.Attributes.ContainsKey(“tickersymbol”))

tickerSymbol = (string)accountEntity[“tickersymbol”];

 


if (tickerSymbol != string.Empty)

{

anualRevenue = GetRevenue(tickerSymbol);

 

AnualRevenue.Set(executionContext, anualRevenue);

service.Update(accountEntity);

}

 


private
decimal? GetRevenue(string tickerSymbol)

{


// TODO: This function should query a financial service like Google or Yahoo to get the anual revenue given the stock symbol


Random random = new
Random();


return (decimal)Math.Round((random.NextDouble() * 100000),3);

}

 

In the WFActivity project, go to project properties and select Signing.

Create a new Strong Name Key.

Deploy the CRM Package project.

 

Create a new workflow

 

 

 

Select Record fields change and click Select.

 

Add a testing step and activate the workflow.

 

Set the Account Input parameter.

Add a second step to Set Account Annual Revenue.

 

 

 

Activate the Workflow

 

Now go to CRM and test the workflow by changing the ticker symbol field in the Account form. Save and Close the form.

Since the workflow is asynchronous the value of Annual Revenue change can take few seconds.

 

 

Debugging a workflow

Copy the .dll and .pdb files from your debug project to server

 

Open the Plugin registration tool located in the CRM SDK directory:

sdkbinpluginregistration.exe

Register the assembly from the debug directory in your Visual Studio WFActivity project.

In Visual Studio, Attach the process CrmAsyncServer.exe.

In CRM Browser create and workflow and add the custom activity WFActivity.

Create a new step and activate the workflow.

Create a new account.

 

Visual Studio will stop the execution in a breakpoint in the Execute event.

 

Leave a Reply