Tutorial 8 - phuocle/Dynamics-Crm-DevKit GitHub Wiki
- 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
- Finish Tutorial 1: Plugin
- Finish Tutorial 2: Unit Test Plugin
- Finish Tutorial 3: WebResource
- Finish Tutorial 4: Unit Test WebResource
- Finish Tutorial 5: Custom Workflow
- Finish Tutorial 6: WebApiClient
- Finish Tutorial 7: Custom Action
- Goto Dynamics 365 and do the following configure
-
Leadentity, add3Single Line of Textfields:paz_field1,paz_field2,paz_field3 -
Accountentity, add3Single Line of Textfields:paz_field1,paz_field2,paz_field3
- Create plugin project
Paz.LuckeyMonkey.Plugin.Account - Create plugin class
PreAccountCreateSynchronous - The
ExecutePluginfunction 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
- You should
re-add again01. C# Late Bound Classclass:Account,LeadtoEntitiesfolder ofPaz.LuckeyMonkey.Sharedproject -
re-add againmean youadd01. C# Late Bound Classclass, but the process only generate a*.generated.cs
- Create unit test plugin project
Paz.LuckeyMonkey.Plugin.Account.Test - Create unit test plugin class
PreAccountCreateSynchronousTest - 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");
}- Run this unit test and you received an error because the
ProxyTypesDON'Tknow any fields you justcreated - Run
run.batin the projectPaz.LuckeyMonkey.ProxyTypestore-generatedfileGeneratedCode.cs
NOTED when you changed metadata, you should re-generated the ProxyTypes
- Run this unit test again and you
passed - Check-in all files to your source control
- You finished this tutorial
This tutorial, you know howto
-
re-generatedtheProxyTypeswhen a newmetadata added/edit