CRM: 2011 Adding Custom Workflow Activity to Fire

Posted on Posted in MS CRM 2011, MS Dynamics, MS Dynamics CRM

If we have a task/update/query which needs to run for longer time i.e. more than 2 min we can create a custom workflow activity and that will run asynchronously in the background and user of CRM can continue working on other stuff.

Why do we need this?

For example if you have a plugin which runs and adds some records and on the basis of that you  need to run something for longer period then we can add entity id’s to our workflow entity by calling CRM Api and on any insert into Workflow entity this workflow will get fired and will run in the background and will not affect plugin

Proper example….

For example whenever a new match gets created and then assigned to a series, it will create record for each customer who has membership of that series and customer will get a ticket for that series and system will try to insert a new record. So let’s say we had 40000 members then system will generate 40000 extra rows for data table and obviously some other tables/entities will require insert/update too.

We cannot use plugins for this and even trying Plugin asynchronously would not work and Workflow Activity instead might do the job

First need to create a new project in your solution

Templates > Visual C#> Dynamics CRM > Dynamics CRM 2011 Workflow Library and give it a name “WorkflowActivityTest”

workflow 1

Enter server details if you are deploying to a server

workflow 2

Below is how it should look at the start

workflow 3

Now we need to create Strong Name Key for project

Right click on the project> Signing> Tick sign in assembly >give it a name abc

workflow 4

Now we need to add a CRM Package to our solution

Right click on solution and add a new CRM package

workflow 5

Next step is to add activity class to our WorkflowActivityTest project

Then right click on the project and add> Dynamics CRM> Workflow Activity Class> Give it a name “FixtureToSeries”

workflow 6

This will ask for few details with a form popping up like below

workflow 7

Friendly name: Any name (Fixture To Series)

Name: Any name (FixtureToSeries)

Description: Any

WorkflowActivityGroupName: This is Singing name you created or you give it name something like abc and then later you can create it(Go to Project properties> Signing> Choose a strong name key file> New> Give it a name

Make sure we have all missing references and using statements

workflow 8

Now we need to create arguments which will be added later from CRM or any external source i.e. plugin etc.

workflow 9

In our case above, we will need fixture (event), series(category) and type (attaching or detaching ). These can be of any type we need i.e. string etc.

Now add all the code you need to (all logic)

Below is an example how to get data

               string primaryEntityName = context.PrimaryEntityName; //our primary entity name

                Guid primaryEntityId = context.PrimaryEntityId; // entity id

 

                string type = this.Type.Get(executionContext); //is it association or dissociation

 

                EntityReference fixtureEntity = this.Fixture.Get(executionContext); //event/fixture entity

                EntityReference seriesEntity = this.Series.Get(executionContext); //category/series entity

 

                ////fixture Id and Series Id

                Guid fixtureId = fixtureEntity.Id;

                Guid seriesId = seriesEntity.Id;

Now we have passed in values and we can do what we need.

Once you have written all your logic then need to compile and deploy this to your solution

Right click on CrmPackage1 project and then deployworkflow 10

This will deploy your activity into your solution in CRM (make sure you have administrator rights on CRM and it is UMMANAGED solution)

Once we have both entities and our string parameter value we can carry on without work i.e. call method which does long update/insert data.

To link this Workflow Activity to an entity we need to go to CRM to add a new Workflow. We can also do this through Visual Studio too.

When creating a Workflow we need to choose entity we need to link this to. In our case we chose Workflow Queueworkflow 11

Once created we need to add steps to it

First step is to add our custom activity Workflow to it as similar to the one shown below

workflow 12

Then click on properties and add input values

workflow 13

For each input we get option to select a main entity i.e. Workflow Queue and then associated entities

Once done we need to add one last step and that is Stop Workflowworkflow 14

Now activate workflow and we are all set to go.

This workflow will be called whenever any insert is done into Workflow Queue Entity.