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.
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.
HIMBAP | Need any help in Microsoft CRM Contact US !!
“Select your assembly step to initialize your variables.” what do you mean by this?
while designing your WF you need to select your custom assembly and need to provide value for input variable if there is any.
While registering assembly it is giving error no plugin selected
It seems you have not signed your assembly. please make sure your assembly is signed and class is public.
Thanks,
Problem was with pulic access of class
Done, I have to create a step with the Assembly to be able to use it later.
Very Nice article ..keep it up dear.
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.
Thanks I have updated post.
Entity _User = (Entity)EntityCol.FirstOrDefault(); this is not deifned in program this is error
Hello, this was a typo, corrected the code, Thank you for your comment.
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