Skip to content

Tutorial 7

PhuocLe edited this page Aug 27, 2018 · 7 revisions

Task

  • Qualify lead don't create Opportunity, create Account and move to Account page after success qualified

Prerequisites

Coding

  1. Goto Dynamics 365, create a Custom Action then Activated with these information bellow
  • Process Name: Ajax
  • Unique Name: paz_Ajaz
  • Entity: None (global)
  • Arguments
    • Name: function, Type: String, Required: Required, Direction: Input
    • Name: jsonInput, Type: String, Required: Optional, Direction: Input
    • Name: jsonOutput, Type: String, Required: Required, Direction: Output
  1. Add New Project 02. C# Custom Action Project to solution.
    • A popup form Add new Custom Action Project opened
    • Click button >< to create/select a Dynamics 365 connection
    • 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.CustomAction
  2. Rebuild solution to restore NuGet packages
  3. Add New Item 04. C# Custom Action Class to Paz.LuckeyMonkey.CustomAction project
    • A popup form opened
    • Click button >< to create/select a Dynamics 365 connection
    • After connected PL.DynamicsCrm.DevKit loaded all entities and bind to dropdown Message
    • Select paz_Ajax
    • Click OK
    • PL.DynamicsCrm.DevKit created custom action class: PostNonepaz_AjaxSynchronous
  4. Edit ExecuteCustomAction like bellow
        private ParameterCollection ExecuteCustomAction(IPluginExecutionContext context, IOrganizationServiceFactory serviceFactory, IOrganizationService service, ITracingService tracing)
        {
            var outputs = new ParameterCollection();
            //YOUR CUSTOM ACTION BEGIN HERE
            var function = (string)context.InputParameters["function"];
            var jsonInput = (string)context.InputParameters["jsonInput"];
            var @return = string.Empty;
            switch (function)
            {
                case "QualifyLead":
                    var qualifyLead = new QualifyLead();
                    @return = qualifyLead.Do(context, service, tracing, jsonInput);
                    break;
            }
            outputs.Add(new KeyValuePair<string, object>("jsonOutput", @return));
            return outputs;
        }
  1. Class QualifyLead
    public class InputQualifyLead
    {
        public string LeadId { get; set; }
    }
    public class OutputQualifyLead
    {
        public string AccountId { get; set; }
        public bool Status { get; set; }
    }
    public class QualifyLead
    {
        internal string Do(IPluginExecutionContext context, IOrganizationService service, ITracingService tracing, string jsonInput)
        {
            //Parse jsonInput to strong type InputQualifyLead
            var input = SimpleJson.DeserializeObject<InputQualifyLead>(jsonInput);
            //Call request QualifyLead
            var request = new QualifyLeadRequest()
            {
                CreateAccount = true,
                CreateOpportunity = false,
                CreateContact = true,
                LeadId = new EntityReference(Lead.EntityLogicalName, Guid.Parse(input.LeadId)),
                Status = new OptionSetValue((int)StatusCode.Qualified)
            };
            var response = (QualifyLeadResponse)service.Execute(request);
            var account = response.CreatedEntities.FirstOrDefault(r => r.LogicalName == Account.EntityLogicalName);
            //Prepare output and Serialize to jsonOutput
            var output = new OutputQualifyLead
            {
                Status = account != null,
                AccountId = account?.Id.ToString()
            };
            return SimpleJson.SerializeObject(output);
        }
    }  

NOTED You should add 01. C# Late Bound Class class: Account to Entities folder of Paz.LuckeyMonkey.Shared project

  1. Run deploy.bat of project Paz.LuckeyMonkey.CustomAction
  2. Check-in all files to your source control
  3. You finished this tutorial

Summary

This tutorial, you know howto

Clone this wiki locally