Inheritance: DLaBExtendedPluginContextBase
        protected override void ExecuteInternal(ExtendedPluginContext context)
        {
            var preImage = context.GetPreEntity <Contact>();
            var target   = context.CoalesceTargetWithPreEntity <Contact>();

            if (preImage.EMailAddress1 == target.EMailAddress1)
            {
                context.Trace("Email Address 1 didn't change.  Exiting.");
                return;
            }

            if (target.AccountId == null)
            {
                context.Trace("No Account for Contact.  Exiting.");
                return;
            }

            InitializeAccount(context, target);
            if (Account.PrimaryContactId == null)  // Account could have been changed by a new thread.
            {
                SetContactToPrimary(context);
            }
            else if (Account.PrimaryContactId?.Id != context.PrimaryEntityId) // Account could have changed, which could result in this being true, when it shouldn't.
            {
                context.Trace("Contact is not primary contact of the account.  Exiting.");
                return;
            }

            UpdateAccountEmail(context, target);
        }
        private void ExecuteCrmAddresses(ExtendedPluginContext context)
        {
            // Account is used here, but since all Entities have the same exact field names, this works just fine
            var target = context.GetTarget <Entity>();

            switch (target.LogicalName)
            {
            case Contact.EntityLogicalName:
                RemoveFormatting(target, Contact.Fields.Address3_Telephone1);
                RemoveFormatting(target, Contact.Fields.Address3_Telephone2);
                RemoveFormatting(target, Contact.Fields.Address3_Telephone3);
                RemoveFormatting(target, Contact.Fields.MobilePhone);
                break;

            case Lead.EntityLogicalName:
            case SystemUser.EntityLogicalName:
                RemoveFormatting(target, Contact.Fields.MobilePhone);
                break;
            }

            RemoveFormatting(target, Account.Fields.Address1_Telephone1);
            RemoveFormatting(target, Account.Fields.Address1_Telephone2);
            RemoveFormatting(target, Account.Fields.Address1_Telephone3);
            RemoveFormatting(target, Account.Fields.Address2_Telephone1);
            RemoveFormatting(target, Account.Fields.Address2_Telephone2);
            RemoveFormatting(target, Account.Fields.Address2_Telephone3);
        }
 private void UpdateAccountEmail(ExtendedPluginContext context, Contact target)
 {
     context.OrganizationService.Update(new Account
     {
         Id            = Account.Id, // Account could have been changed by a new thread.
         EMailAddress1 = target.EMailAddress1
     });
 }
 private void SetContactToPrimary(ExtendedPluginContext context)
 {
     context.OrganizationService.Update(new Account
     {
         Id = Account.Id, // Account could have been changed by a new thread.
         PrimaryContactId = context.PrimaryEntity
     });
 }
        private void ExecuteCrmPhoneNumber(ExtendedPluginContext context)
        {
            // Account is used here, but since all Entities have the same exact field names, this works just fine
            var target = context.GetTarget <Entity>();

            RemoveFormatting(target, Account.Fields.Telephone1);
            RemoveFormatting(target, Account.Fields.Telephone2);
            RemoveFormatting(target, Account.Fields.Telephone3);

            ExecuteCrmAddresses(context);
        }
Exemplo n.º 6
0
        protected override void ExecuteInternal(ExtendedPluginContext context)
        {
            // Get the Target
            var contact = context.GetTarget <Contact>();

            if (string.IsNullOrWhiteSpace(contact.Address1_Line1))
            {
                context.Trace(AddressNotUpdatedMessage);
                return;
            }

            using (var crm = new CrmContext(context.OrganizationService))
            {
                var accounts = crm.AccountSet.Where(a => a.PrimaryContactId.Id == contact.Id);
                foreach (var account in accounts)
                {
                    UpdateAccountAddress(context.OrganizationService, account.Id, contact);
                }
            }
        }
        private void InitializeAccount(ExtendedPluginContext context, Contact target)
        {
            var account = context.OrganizationService.GetEntity <Account>(target.AccountId.Id, a => new { a.AccountId, a.PrimaryContactId });

            Account = account; // This line starts the race condition
        }
 protected override void ExecuteInternal(ExtendedPluginContext context)
 {
     throw new InvalidOperationException("Should Never Get Called!");
 }