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

11 responses to “Step By Step Creating Custom Workflow in Microsoft CRM 2011- Part2

  1. jonathan yong

    “Select your assembly step to initialize your variables.” what do you mean by this?

    • mahenderpal

      while designing your WF you need to select your custom assembly and need to provide value for input variable if there is any.

  2. Rahul

    While registering assembly it is giving error no plugin selected

  3. Very Nice article ..keep it up dear.

  4. Great post mahender. Minor updation (if you like)
    Instead of using (Entity)EntityCol.Entities[0]; we should use (Entity)EntityCol.Entities.FirstOrDefault(); so that we never rely of the first element, which may result in outOfBound exception or might be the case if record doesnot exists. So for that case, if there’s no row to be returned, FirstOfDefault() will return a NULL value without raising any exception.

  5. Rajnikant

    Entity _User = (Entity)EntityCol.FirstOrDefault(); this is not deifned in program this is error

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s