Daily Archives: August 29, 2012

Step By Step Creating Custom Workflow in Microsoft CRM 2011- Part2

In previous post we started creating custom workflow to get lead created day and assign all lead created on Sunday to Alan, we have completed code to get lead created day, so let’s now write function to get user id for Alan and assign all lead to Alan if lead created day is Sunday.

13. Create function to get userid like below

private Guid GetUserID(IOrganizationService service) //function to get userid

{  Guid _UserID = Guid.Empty;

ConditionExpression condition1 = new ConditionExpression();

condition1.AttributeName = “firstname”;

condition1.Operator = ConditionOperator.Equal;


ConditionExpression condition2 = new ConditionExpression();

condition2.AttributeName = “lastname”;

condition2.Operator = ConditionOperator.Equal;


FilterExpression filter1 = new FilterExpression();


QueryExpression query = new QueryExpression(“systemuser”);


EntityCollection EntityCol = service.RetrieveMultiple(query);

if (EntityCol.Entities.Count > 0)

{    Entity _User = (Entity)EntityCol.Entities.FirstOrDefault();

_UserID = _User.Id;


return _UserID;


14. Write a function to assign passed lead to Alan, like below

private void Assignlead(IOrganizationService service,Guid LeadID,Guid UserID)


AssignRequest _Assign = new AssignRequest() {

Assignee=new EntityReference(“systemuser”,UserID),

Target =new EntityReference(“lead”, LeadID)




finally we need to modify our Execute method and add below lines


{           Guid _UserID= GetUserID(service);



Our code is complete now, build your assembly and register it using plugin registration tool. While registering we can configure our step and workflow group name like below screen. Save your changes after providing these details.


15. Now create a workflow on lead and set it to run when “Record is created”.

16. Click on Add Step and you should be able to see your custom workflow there.


17. Select your assembly step to initialize your variables.


18. Add an update step to update lead record and click on set properties.

19. Selected your custom field (“Created Day”) and select your assembly name from Look For drop down under form assistance.


20. Assign your output variable to your custom field by clicking on Add and Ok.

Save and Activity your process, now test your workflow.

Enjoy !!!

Note: if you are copying record please make sure to change Quotes marks.


Filed under MS CRM 2011, Step By Step MS CRM 2011