Scheduling workflows in CRM 4.0

I am going to deviate a little from the series on which i have been working. I will get back to that series but before i do that i just want to share with you some thought and step by step guide for scheduling your workflows in CRM 4.0. I have not been able to find much help on it so i thought I will present here the essentials for achieving this task.

I wanted to perform certain tasks on entities periodically based on an event. Unfortunately CRM 4.0 does not have this functionality for scheduling workflows out of the box. There are various ways to achieve this but i will share with you my approach and the reasons for the selection of this solution. Here is what i wanted to do. Each of my account has a contract with our company. That contract like any other contract expires at a certain date. What we want is to perform a certain set of tasks on each entity whose contract has expired. The tasks were created inside a custom workflow. The solution was reduced to simple scheduling of the written workflow based on contract expiration. As mentioned CRM 4.0 does not support that so we have written our windows service.

However there are other ways of doing this task. Your workflow can remain in waiting stage and wait for an event. The only problem with that approach is that when ever you change something all your workflows will have to be hyderated and dehudrated. It was not an option for us. You can read more on it here :

Anyways so firstly i created a windows service. I will not be covering this in my blog but if you need to write your own windows service its very easy and you can find articles on net. You can mail me if you still need some help.

This is what happens on the “On start” event of my service.

1. Instantiate CrmService

// Set up authentication to be Active Directory mode
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = authType;
token.OrganizationName = orgName;

// Set up the Crm service. Pick up the url from Registry
service = new CrmService();

//Retrieve the port and Server Url from the Registry.
RegistryKey regkey = Registry.LocalMachine.OpenSubKey(“SOFTWARE\\Microsoft\\MSCRM”);
string _serverUrl = regkey.GetValue(“ServerUrl”).ToString();

service.Url = _serverUrl + “/2007/crmservice.asmx”;
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
2. Get workflow id that needs to be executed. We will be using this workflow_id to schedule this workflow on the accounts that have expired contract.

This can be done based on the workflow id but that means you will have to hard code the Id. I am retreiving the workflow based on the name. This is how you can achieve this.

workflow _contractRenewalWorkflow = null;

// Create the ColumnSet that indicates the properties to be retrieved.
ColumnSet _columnsWorkflow = new ColumnSet(new string[] { “name”, “workflowid” });

// Create the ConditionExpression for association Name field.
ConditionExpression _conditionAssociationName = new ConditionExpression();

// Set the condition for the retrieval to be based on the workflow name.
_conditionAssociationName.AttributeName = “name”;
_conditionAssociationName.Operator = ConditionOperator.Equal;
_conditionAssociationName.Values = new string[] { “Association contract renewal” };

// Create the FilterExpression.
FilterExpression _filterRetrieveWorkflow = new FilterExpression();

// Set the properties of the filter.
_filterRetrieveWorkflow.FilterOperator = LogicalOperator.And;

// Create the QueryExpression object.
QueryExpression _queryRetrieveWorkflow = new QueryExpression();

// Set the properties of the QueryExpression object.
_queryRetrieveWorkflow.EntityName = EntityName.workflow.ToString();
_queryRetrieveWorkflow.ColumnSet = _columnsWorkflow;
_queryRetrieveWorkflow.Criteria = _filterRetrieveWorkflow;

// Retrieve the workflow
BusinessEntityCollection workflows = _service.RetrieveMultiple(_queryRetrieveWorkflow);

_contractRenewalWorkflow = (workflow)workflows.BusinessEntities[0];

3. Now i need to fetch all the accounts for which the contract has expired. I will fetch all these accounts and then i will execute workflow on these accounts.

Use retrieve multiple request for this and in case of  custom entities set dynamic entities value.

_requestRetrieveExpiredAssociations = new RetrieveMultipleRequest();RetrieveMultipleResponse _responseRetrieveExpiredAssociations = new RetrieveMultipleResponse();

// set query conditions here based on which you want to retrieve these records.

// Retrieve the entity
_requestRetrieveExpiredAssociations.Query = _queryAssociations;
_requestRetrieveExpiredAssociations.ReturnDynamicEntities = true;
_responseRetrieveExpiredAssociations = (RetrieveMultipleResponse)_service.Execute(_requestRetrieveExpiredAssociations);

4. Loop through each of these accounts and schedule workflow.

// Workflow execute request and response
ExecuteWorkflowRequest _requestExecuteWorkflow = new ExecuteWorkflowRequest();
ExecuteWorkflowResponse _responseExecuteWorkflow = new ExecuteWorkflowResponse();
_requestExecuteWorkflow.WorkflowId = _contractRenewalWorkflow.workflowid.Value;
foreach (DynamicEntity _association in _responseRetrieveExpiredAssociations.BusinessEntityCollection.BusinessEntities)
_requestExecuteWorkflow.EntityId = ((Key)_association.Properties[“cc_associationid”]).Value;
_responseExecuteWorkflow = (ExecuteWorkflowResponse)_service.Execute(_requestExecuteWorkflow);

That should be it. Based on the time interval set in your windows service, your service will execute check for the accounts that have expired contract. Execute the workflow on each of the acocunts with expired contracts. In my implementation my workflow sends an email to the owner of workflow, creates a ‘contract renewal task’, creates an opportunity for sales team. I will continue with my series of post ‘Crm for dummies’ and write about managing different entities using my scenario of soap factory. Please feel free to put your comments or ask questions.


CRM for Dummies – Part 2

In my previous post I discussed the concepts of lead, opportunity and contact for a B2C. As promised today i will be writing about the accounts. You can read my previous post here :

As mentioned in the previous post you can have opportunities associated with contacts and these opportunities are essentially representing things you sell. However some compnies have other companies as their customers. Companies that are in business with other companies would convert their leads to opportunities, accounts and contacts. In this case the opportunities might be related to accounts and the contact details are used to reach out to decision makers in these companies (accounts). Again as mentioned in the previous post its entirely upto the BA to decide when to convert a lead to account and if the lead should convert only to an account or to an account, opportunity and contact. However mostly a lead converts to both an account and contact, as its the contact that you get in touch with to sell your product to accont. To build on the previous example of selling soaps consider that you sell soaps to retailers. So we have a Lead (Subject : sell soap, First name: Tom, Phone: XYZ, Company: KeepClean) that converts to Contact ( Name : Tom, Phone: XYZ), Account(Name: KeepClean) and opportunity (Subject: sell soap to KeepClean). Now sales team works on the opportunity to sell soap to account(‘KeepClean’) (performs tasks like phone calls to contact, follow-ups with contact, mails to contact..). Once they are able to sell the Soap they change the opportunity status to ‘WON’. Now ‘KeepClean’ has actually purchased something so it is a customer. Again as explained an account is not necessarily a customer. However a customer in CRM can be categorized as an account with atleast one Opportunity with status ‘WON’.

One thing that confuses most of the people when doing the analysis for CRM implementation is how to map the existing entities to the entities with in CRM 4.0. Most of the examples that are available on internet show CRM as either a catalog with lots of products/services for various customers or with just a single product. However in real world a company sells certain products that they manufature and mostly analysts want a generic way to categorize their accounts in terms of the products. I think the best solution is to add a field to all the entities in CRM to classify each entity in terms of the product. This is the simplest solution for handling multiple products. That is all for today.

In my next post I will walk you through basic customization for creating a CRM solution that you will be able to use to handle entities in your company. I will also write more posts and take you through the creation of tasks to work towards opportunity closure. Essentilly i have just given a brief introduction about my soap factory. In my next post i will be working on selling some soap:)

CRM for dummies

I have been working on a CRM project as BA/PM for some time now. Initially I didnt know much about the product so I had to read lots of books and obviously i used all the content on internet as well. I think that there are alot of obvious questions for someone starting with CRM that are still not answered in a straight forward manner. Initially I was just responsible for the business analysis, requirements gathering and project management but i also  ended up doing the develoment work. I think because i have been through the mill so i can help you through the CRM project execution life cycle. This blog and others to follow in this series are intended to help with the anlysis, management, development and deployment of the projects related to CRM 4.0. You can ask me all the questions you have and I will try to answer to the best of my abilities.

CRM project should be treated no different from any other project. However with CRM projects if Dev team works closely with the BA/PM then the work required can be reduced significanlty because there are lots of work-arounds to do similar tasks. For instance creating custom entities and setting up its relationship with existing entities like leads, opportunities and contacts is a common task and normally has various implementation with their trade-offs. Similarly the process of lead conversion to other entities and then managing newly created entities through workflows, plugins and CRM integration with other system are some of the tasks that can be simplified with analysis.

In this blog i will introduce the concepts of out of the box entities excluding accounts to help many out there who ask questions regarding these entities. So what is a lead? Well firstly i must say that Microsoft got it wrong. I strongly feel that Lead is not an entity but its a classification of an entity(whatever it maybe) based on its degree of maturity. Let me explain this for you.

Supppose you have a company that sells soap. So all the people out there who are interested in buying soap are leads(potential customers or customers in embroic form). However once you input these people you dont have their all the information and incases where you have maybe its not good enough to be able make them into customers (people who actually buy from you). So you get more information about these people and then you contact them to validate if they actually want to buy your soap. If they are interested you convert the lead to (Contact and Opportunity(maybe)).

An opporunity presents the possibility to sell something. In our case its soap we are selling so we have a Lead (Subject : sell soap, First name: Tom, Phone: XYZ) that converts to Contact ( Name : Tom, Phone: XYZ) and opportunity (Subject: sell soap to Tom). Now sales team works on the opportunity to sell soap to Tom (performs tasks like phone calls, follow-ups, mails..). Once they are able to sell the Soap they change the opportunity status to ‘WON’. Now Tom has actually purchased something so he is a customer.

Some important things that confuse lots of people include, When to convert lead to opportunity and contact. Is Contact a customer? What is the difference between lead and this contact. What is an opportunity and why it is required?

The answer to these quesitons are normally business specific and BA is the best judge. Normally BA needs to interview the Sales and marketing teams to decide on these and there is no thumbs rule to follow. In our implmentation we convert the lead to contact and opportunity when we have a lead with whom we have established contact and it has shown interest. Sales team guages the degree of interest and decides to convert to contact and opportunity. When contact is created the contact is just a customer in embroic form (just like a lead) but with opportunity associated to sell stuff to this contact. Once it actually buys then its a customer. You may argue that one should only convert a lead to contact once it has purchased something. If you want to do that it is not wrong but opportunities are created with contacts in CRM 4.0 so its better if things are sole to contacts and not leads.

In this post i have just given the brief introduction about lead, opporuntity and contact. This is valid for B2C but not for B2B. I will explaing how ‘Accounts’ can be used in a similar fashion when doing business with companies in my next post. Moreover I would also explain what the above given information means for sales team and how they use the system to work on their areas of concerns.

In the end just an analysis tip for the analysts out here. Always remember its not about using the system, its about managing the accounts and contacts and increasing sales. So before the implementation starts make sure you have stats like number of calls per day, number of sales in a region, talk time etc so that you could guage after CRM installation if it has actually helped your company or not.