Skip to content

Tutorial 8

PhuocLe edited this page Mar 8, 2018 · 7 revisions

Task

  • Automatic create Contact record when Account created with these mapping
    • Contact First Name = Account Name
    • Contact Telephone1 = Account Telephone 1
    • Contact Address Type 1 always Other

Coding

  1. Add 08. C# Plugin Class to Wooow.Kool.Plugin.Account project
    • Message: Create - Stage: PostOperation - Execution: Synchronous
    • PL.DynamicsCrm.DevKit created plugin class: PostAccountCreateSynchronous
  2. Add 21. C# Late Bound Class Contact to Entities folder of Wooow.Kool.Shared project.
    • PL.DynamicsCrm.DevKit created 2 files: Contact.js and Contact.generated.cs
  3. Add PL.DynamicsCrm.DevKit > C# Date.cs to Lib folder of Wooow.Kool.Shared project.
  4. Rebuild solution without errors.
  5. Continue coding
public override void Execute()
{
    if (Plugin.Context.Stage != (int)StageEnum.PostOperation) throw new InvalidPluginExecutionException("Stage does not equals PostOperation");
    if (Plugin.Context.PrimaryEntityName != "account") throw new InvalidPluginExecutionException("PrimaryEntityName does not equals account");
    if (Plugin.Context.MessageName.ToLower() != "Create".ToLower()) throw new InvalidPluginExecutionException("MessageName does not equals Create");
    if (Plugin.Context.Mode != (int)ExecutionModeEnum.Synchronous) throw new InvalidPluginExecutionException("Execution does not equal Synchronous");

    var target = (Entity)Plugin.Context.InputParameters["Target"];
    //var preEntity = (Entity)Plugin.Context.PreEntityImages["PreImage"];
    //var postEntity = (Entity)Plugin.Context.PreEntityImages["PostImage"];

    //YOUR PLUGIN-CODE GO HERE
    var account = new Account(target);
    var contact = new Contact();
    contact.ParentCustomerId = new EntityReference(Account.EntityLogicalName, account.Id);
    contact.FirstName = account.Name;
    contact.Telephone1 = account.Telephone1;
    contact.Address1_AddressTypeCode = Address1_AddressTypeCode.Other;
    Plugin.Service.Create(contact.GetCreateEntity());
}
  1. Run deploy.bat in Wooow.Kool.Plugin.Account to deploy plugin.
  2. Go to Dynamics Crm, create new Account to check plugin worked or not.
  3. Check-in all files to your source control.
  4. You finished this tutorial.

Your Solution Explorer after you finished this tutorial

Finished Tutorial 8

Clone this wiki locally