Skip to content

Tutorial 5

PhuocLe edited this page Aug 26, 2018 · 10 revisions

Task

  • When lead created, send an email (with an email template) notification to all team members of the current user belong to teams

Prerequisites

Coding

Break down tasks

  • A custom workflow get all team members of the current user belong to teams
  • A custom workflow send email with email template for all team members of current record

RetrieveUsers

  1. Add New Project 04. C# Workflow Project to solution.
    • A popup form Add new Workflow Project opened
    • Click button >< to create/select a Dynamics 365 connection
    • After connected PL.DynamicsCrm.DevKit loaded all entities and bind to dropdown Project Name
    • Checkbox Others should be checked
    • Keep the text box Project Name empty
    • Select 9.0.2.4 in the Crm Version PL.DynamicsCrm.DevKit get all Microsoft.CrmSdk.CoreAssemblies version from NuGet
    • Select 4.5.2 in the .Net version
    • Click OK
    • PL.DynamicsCrm.DevKit created workflow project name: Paz.LuckeyMonkey.Workflow
  2. Rebuild solution to restore NuGet packages
  3. Add 01. C# Late Bound Class SystemUser to Entities folder of Paz.LuckeyMonkey.Shared project. If you don't know how, please check again the Tutorial 1: Plugin
  4. Add New Item 03. C# Workflow Class to Paz.LuckeyMonkey.Workflow project
    • A popup form opened
    • Enter RetrieveUsers to texbox Class Name
    • Click OK
    • PL.DynamicsCrm.DevKit created workflow class: RetrieveUsers
  5. Edit the RetrieveUsers.cs like bellow
    public class RetrieveUsers : CodeActivity
    {
        [Output("UserIds")]
        [RequiredArgument]
        public OutArgument<string> UserIds { get; set; }

        protected override void Execute(CodeActivityContext executionContext)
        {
            var workflowContext = executionContext.GetExtension<IWorkflowContext>();
            var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            var service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
            var tracing = executionContext.GetExtension<ITracingService>();
            ExecuteWorkflow(executionContext, workflowContext, serviceFactory, service, tracing);
        }

        private void ExecuteWorkflow(CodeActivityContext executionContext, IWorkflowContext workflowContext, IOrganizationServiceFactory serviceFactory, IOrganizationService service, ITracingService tracing)
        {
            Debugger.Trace(tracing, "Begin Execute Workflow: RetrieveUsers");
            //YOUR CUSTOM-WORKFLOW-CODE GO HERE
            var fetchXml = $@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
  <entity name='systemuser'>
    <attribute name='systemuserid'/>
    <link-entity name='teammembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'>
      <link-entity name='team' from='teamid' to='teamid' alias='aa'>
        <link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>
          <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='ab'>
            <filter type='and'>
              <condition attribute='systemuserid' operator='eq-userid'/>
            </filter>
          </link-entity>
        </link-entity>
      </link-entity>
    </link-entity>
  </entity>
</fetch>
";
            var users = service.RetrieveAll<SystemUser>(fetchXml);
            var userIds = string.Join(";",
                users
                .Where(u => u.Id != workflowContext.InitiatingUserId)
                .Select(u => u.Id).Distinct().ToList());
            UserIds.Set(executionContext, userIds);

            Debugger.Trace(tracing, "End Execute Workflow: RetrieveUsers");
        }
    }

NOTED Remember to use View-FetchXML to copy/paste code 6. Open file PL.DynamicsCrm.DevKit.Cli.json by Notepad and edit these information in section: workflows.profile = "DEBUG" * workflows.solution = "LuckeyMonkey" * workflows.includefiles = "Paz.LuckeyMonkey.*.dll" 7. Run deploy.bat of project Paz.LuckeyMonkey.Workflow 8.

Clone this wiki locally