Skip to content

Tutorial 8

PhuocLe edited this page Aug 27, 2018 · 7 revisions

Task

  • Lead have 3 custom fields: LEAD_FIELD1, LEAD_FIELD2, LEAD_FIELD3 mapping correspond with 3 custom fields: ACC_FIELD1, ACC_FIELD2, ACC_FIELD3 in Account. When lead qualified, make sure 3 custom fields should copy from lead to account

Prerequisites

Coding

  1. Goto Dynamics 365 and do the following configure
  • Lead entity, add 3 Single Line of Text fields: paz_field1, paz_field2, paz_field3
  • Account entity, add 3 Single Line of Text fields: paz_field1, paz_field2, paz_field3
  1. Create plugin project Paz.LuckeyMonkey.Plugin.Account
  2. Create plugin class PreAccountCreateSynchronous
  3. The ExecutePlugin function like bellow
        private void ExecutePlugin(IPluginExecutionContext context, IOrganizationServiceFactory serviceFactory, IOrganizationService service, ITracingService tracing)
        {
            //var target = (???)context.InputParameters["Target"];
            //var preEntity = (Entity)context.PreEntityImages["PreImage"];
            //var postEntity = (Entity)context.PreEntityImages["PostImage"];
            //YOUR PLUGIN-CODE GO HERE
            WhenALeadQualify(context, serviceFactory, service, tracing);
        }

        private void WhenALeadQualify(IPluginExecutionContext context, IOrganizationServiceFactory serviceFactory, IOrganizationService service, ITracingService tracing)
        {
            var target = (Entity)context.InputParameters["Target"];
            var account = new Shared.Entities.Account(target);//Bind account entity to Account Strong Type Late Bound by PL.DynamicsCrm.DevKit
            if (account.OriginatingLeadId != null) //check this field to make sure Account created by Qualify Lead
            {
                //Reading mapping Lead fields
                var fetchData = new
                {
                    leadid = account.OriginatingLeadId.Id
                };
                var fetchXml = $@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='lead'>
    <attribute name='leadid'/>
    <attribute name='paz_field3'/>
    <attribute name='paz_field2'/>
    <attribute name='paz_field1'/>
    <filter type='and'>
      <condition attribute='leadid' operator='eq' value='{fetchData.leadid}'/>
    </filter>
  </entity>
</fetch>
";
                var rows = service.RetrieveMultiple(new FetchExpression(fetchXml));
                if (rows.Entities.Count == 0) return;//Lead not found
                var lead = new Shared.Entities.Lead(rows.Entities[0]);//Bind lead entity to Lead Strong Type Late Bound by PL.DynamicsCrm.DevKit
                account.paz_Field1 = lead.paz_Field1;
                account.paz_Field2 = lead.paz_Field2;
                account.paz_Field3 = lead.paz_Field3;
            }
        }

NOTED

  1. Create unit test plugin project Paz.LuckeyMonkey.Plugin.Account.Test
  2. Create unit test plugin class PreAccountCreateSynchronousTest
  3. Edit unit test like bellow
        public void PreAccountCreate_Test_WhenALeadQualify()
        {
            //prepare data
            //1. Lead
            var LEAD_ID = Guid.Parse("{00000000-0000-0000-0000-000000000001}");
            var lead = new Entity("lead");
            lead["leadid"] = LEAD_ID;
            lead["paz_field1"] = "abc";
            lead["paz_field2"] = null;
            lead["paz_field3"] = "def";

            //2. target
            var target = new Entity("account");
            target["originatingleadid"] = new EntityReference("lead", LEAD_ID);

            //setup
            Context.ProxyTypesAssembly = Assembly.GetAssembly(typeof(ProxyTypesAssembly));
            Context.Data.Clear();
            Context.Data.Add("lead", new Dictionary<Guid, Entity> {
                { LEAD_ID, lead }
            });
            PluginContext.InputParameters["Target"] = target;
            //run
            Context.ExecutePluginWithConfigurations<PreAccountCreateSynchronous>(PluginContext, null, null);
            //result
            var result = PluginContext.InputParameters["Target"] as Entity;
            var account = new Shared.Entities.Account(result);
            Assert.AreEqual(account.paz_Field1, "abc");
            Assert.AreEqual(account.paz_Field2, null);
            Assert.AreEqual(account.paz_Field3, "def");
        }
  1. Run this unit test and you received an error because the ProxyTypes DON'T know any fields you just created
  2. Run run.bat in the project Paz.LuckeyMonkey.ProxyTypes to re-generated file GeneratedCode.cs

NOTED when you changed metadata, you should re-generated the ProxyTypes

  1. Run this unit test again and you passed
  2. Check-in all files to your source control
  3. You finished this tutorial

Summary

This tutorial, you know howto

  • re-generated the ProxyTypes when a new metadata added/edit
Clone this wiki locally