Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

CRM 2011 : Custom Workflow to fetch user with Specifed role

$
0
0

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.

                                   



Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>