Step By Step Creating Custom Workflow in Microsoft CRM – 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;

	condition1.Values.Add("Alan");

	ConditionExpression condition2 = new ConditionExpression();

	condition2.AttributeName = "lastname";

	condition2.Operator = ConditionOperator.Equal;

	condition2.Values.Add("Smith");

	FilterExpression filter1 = new FilterExpression();

	filter1.Conditions.AddRange(condition1, condition2);

	QueryExpression query = new QueryExpression("systemuser");

	query.Criteria.AddFilter(filter1);

	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)

	};

	service.Execute(_Assign);

}
finally we need to modify our Execute method and add below lines
if (Day == "Sunday")

{
	Guid _UserID = GetUserID(service);

	Assignlead(service, context.PrimaryEntityId, _UserID);

}

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.

providedetails

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.

Groupname

17. Select your assembly step to initialize your variables.

firststep

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.

SaveArgument

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

Save and Activity your process, now test your workflow.

HIMBAP | Need any help in Microsoft CRM Contact US !!

12 thoughts on “Step By Step Creating Custom Workflow in Microsoft CRM – Part2

    1. mahenderpal Post author

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

      Reply
    1. mahenderpal Post author

      It seems you have not signed your assembly. please make sure your assembly is signed and class is public.

      Reply
  1. kunalgoel11

    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.

    Reply
  2. Rajnikant

    Error 1 ‘Microsoft.Xrm.Sdk.EntityCollection’ does not contain a definition for ‘FirstOrDefault’ and no extension method ‘FirstOrDefault’ accepting a first argument of type ‘Microsoft.Xrm.Sdk.EntityCollection’ could be found (are you missing a using directive or an assembly reference?) E:Rajnikant’s DataVSFirstCustomWorkflowFirstCustomWorkflowLeadAssignment.cs 73 44 FirstCustomWorkflow

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *