locked
CRM 2011 : Custom Workflow to fetch user with Specifed role RRS feed

  • Question

  • Hi

           

    i am totally new to crm2011.
    i need to write a custom workflow.

    To do the following things -

         1)I want to pass Role Name as "Manager" or something

         2)On the basis of Role Name a user should be returned who is assigned that role.

         3)Send Notification to that User

    From what i have understood is i need to join the below 3 tables -

        System user
        System user roles
        Roleset

    After searching a lot on how to create a workflow i have landed with this -

        class GetUserRole:CodeActivity
        {

            [Output("Current User")]
            [ReferenceTarget("systemuser")]

            public OutArgument<EntityReference> CurrentUser { get; set; }

            string roleName = "Manager";

            protected override void Execute(CodeActivityContext Execution)
            {

                try
                {
                    IWorkflowContext context = Execution.GetExtension<IWorkflowContext>();
                    IOrganizationServiceFactory serviceFactory =
                                     Execution.GetExtension<IOrganizationServiceFactory>();

                    IOrganizationService service =
                          serviceFactory.CreateOrganizationService(context.InitiatingUserId);

                    QueryExpression query = new QueryExpression("systemuser");

                    query.ColumnSet = new ColumnSet(new string[] { "systemuserid"});
                    query.Distinct = true;
                    query.Criteria = new FilterExpression();
                    query.AddLink("systemuserroles", "systemuserid", "systemuserid").
                    AddLink("role", "roleid", "roleid").LinkCriteria.AddCondition("name", ConditionOperator.Equal, roleName);

                    EntityCollection entityCollection = service.RetrieveMultiple(query);
                    if (entityCollection.Entities.Count > 0)
                    {
                        string CurrentUser = entityCollection[0].Attributes["systemuserid"].ToString();
                    }
                }
                catch (Exception ex)
                { }
            }

    But i am getting the Error in it as  "Invalid Argument"

    Please if anybody could guide me with this. i am totally lost.

                                       


    Saturday, November 23, 2013 7:54 PM

Answers

  • Hi, finally i found the solution for my Custom Workflow -

    This will help fetch a user with the mentioned role.

      public class GetUserWorkflow : CodeActivity
        {
            [Output("Current User")]
            [ReferenceTarget("systemuser")]

            public OutArgument<EntityReference> CurrentUser { get; set; }

            string roleName = "Manager";

            protected override void Execute(CodeActivityContext Execution)
            {

                try
                {
                    IWorkflowContext context = Execution.GetExtension<IWorkflowContext>();
                    IOrganizationServiceFactory serviceFactory =
                                     Execution.GetExtension<IOrganizationServiceFactory>();

                    IOrganizationService service =
                          serviceFactory.CreateOrganizationService(context.InitiatingUserId);

                    QueryExpression query = new QueryExpression("systemuser");

                    query.ColumnSet = new ColumnSet(new string[] { "systemuserid"});
                    query.Distinct = true;
                    query.Criteria = new FilterExpression();
                    query.AddLink("systemuserroles", "systemuserid", "systemuserid").
                    AddLink("role", "roleid", "roleid").LinkCriteria.AddCondition("name", ConditionOperator.Equal, roleName);

                    var users = service.RetrieveMultiple(query);

                    if (users.Entities.Count() > 0)
                    {
                        CurrentUser.Set(Execution,((Entity)users[0]).ToEntityReference());
                    }
                }
                catch (Exception ex)
                { }
            }

        }


    Monday, November 25, 2013 8:23 AM

All replies

  • Hello,

    Did you check this Sample, it should help you to implement your requirement.

    you can also refer our custom workflow post, it will help you to write custom workflow.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Sunday, November 24, 2013 2:20 PM
    Moderator
  • Hi Mahender,

    I have a modified my code.Can u check now?
    I am getting the error as "Invalid Argument" once i run the workflow.

    Monday, November 25, 2013 5:54 AM
  • Hi, finally i found the solution for my Custom Workflow -

    This will help fetch a user with the mentioned role.

      public class GetUserWorkflow : CodeActivity
        {
            [Output("Current User")]
            [ReferenceTarget("systemuser")]

            public OutArgument<EntityReference> CurrentUser { get; set; }

            string roleName = "Manager";

            protected override void Execute(CodeActivityContext Execution)
            {

                try
                {
                    IWorkflowContext context = Execution.GetExtension<IWorkflowContext>();
                    IOrganizationServiceFactory serviceFactory =
                                     Execution.GetExtension<IOrganizationServiceFactory>();

                    IOrganizationService service =
                          serviceFactory.CreateOrganizationService(context.InitiatingUserId);

                    QueryExpression query = new QueryExpression("systemuser");

                    query.ColumnSet = new ColumnSet(new string[] { "systemuserid"});
                    query.Distinct = true;
                    query.Criteria = new FilterExpression();
                    query.AddLink("systemuserroles", "systemuserid", "systemuserid").
                    AddLink("role", "roleid", "roleid").LinkCriteria.AddCondition("name", ConditionOperator.Equal, roleName);

                    var users = service.RetrieveMultiple(query);

                    if (users.Entities.Count() > 0)
                    {
                        CurrentUser.Set(Execution,((Entity)users[0]).ToEntityReference());
                    }
                }
                catch (Exception ex)
                { }
            }

        }


    Monday, November 25, 2013 8:23 AM