/// <summary>
        /// Gets the target entity from plugin context input parameters and casts to type T.
        /// </summary>
        /// <typeparam name="T">Strongly-typed Entity.</typeparam>
        /// <param name="context">The context.</param>
        /// <returns>Target entity cast to type T.</returns>
        /// <exception cref="System.ArgumentNullException">context</exception>
        public static T GetTargetEntity <T>(this IPluginExecutionContext context) where T : Entity
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            return(context.GetTargetEntity().ToEntity <T>());
        }
Esempio n. 2
0
        protected override void ExecuteInternal(IPluginExecutionContext context, IOrganizationServiceFactory orgServiceFactory, ITracingService tracingService)
        {
            if (context.PrimaryEntityName == Contact.EntityLogicalName && context.Stage == (int)PluginStage.PreValidation)
            {
                if (context.MessageName == MessageNames.Create || context.MessageName == MessageNames.Update)
                {
                    tracingService.Trace("Retriving the contact from the plugin context");

                    var contact = context.GetTargetEntity <Contact>();

                    if (context.MessageName == MessageNames.Update)
                    {
                        tracingService.Trace("Retriving the contact pre-image");

                        var entityImage = context.GetPreImage <Contact>("PreImage");

                        if (entityImage == null)
                        {
                            tracingService.Trace("Retriving the contact from the organisation service because the pre-image is null.");

                            var orgService = orgServiceFactory.CreateOrganizationService(null);

                            entityImage = orgService.Retrieve <Contact>(Contact.EntityLogicalName, context.PrimaryEntityId);
                        }

                        contact.Merge(entityImage);
                    }

                    tracingService.Trace("Validation the contact:");
                    tracingService.Trace("Mobile Phone: {0}, Email: {1}, Telephone: {2}", contact.MobilePhone, contact.Email, contact.Telephone);

                    if (string.IsNullOrWhiteSpace(contact.MobilePhone) &&
                        string.IsNullOrWhiteSpace(contact.Email) &&
                        string.IsNullOrWhiteSpace(contact.Telephone))
                    {
                        throw new InvalidPluginExecutionException("At least one of the following contact methods must be provided: email, telephone or mobile");
                    }
                }
            }
        }