Example #1
0
        /// <summary>
        /// Post the messages to the service bus queue and deletes the records from the platform entity.
        /// Please note that the method has designed to run on the post create message event.
        /// </summary>
        public void PostMessage()
        {
            Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));

            var rec = TargetBusinessEntity.ToEntity <ExecutionContext>();

            if (rec.Depth > 1)
            {
                Trace(string.Format(CultureInfo.InvariantCulture, "{0} | Depth: {2}  | {1}.", UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name, rec.Depth));
                Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.AbortingMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));
                return;
            }

            if (!rec.Contains(Models.Domain.ExecutionContext.Fields.ContainsParentContext))
            {
                Trace(string.Format(CultureInfo.InvariantCulture, "{0} | Missing argument: {2}  | {1}.", UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name, Models.Domain.ExecutionContext.Fields.ContainsParentContext));
                Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.AbortingMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));
                return;
            }

            if (rec.ContainsParentContext.GetValueOrDefault(false))
            {
                if (rec.ParentIsMainOperation.GetValueOrDefault(false))
                {
                    IList <ExecutionContext> recs = CrmService.GetExecutionContexts(PluginExecutionContext.CorrelationId.ToString(), PluginExecutionContext.OperationId.ToString()).ToList();
                    Trace(string.Format(CultureInfo.InvariantCulture, "{0} | Records: {2}  | {1}.", UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name, recs.Count));
                    ExecutionContext lastRecord = recs.OrderByDescending(x => x.Subject).First();

                    if (!rec.Subject.Equals(lastRecord.Subject))
                    {
                        Trace(string.Format(CultureInfo.InvariantCulture, "{0} | Not the last record: {2}  | {1}.", UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name, rec.Subject));
                        Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.AbortingMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));
                        return;
                    }

                    PostMessages(recs);
                }
            }
            else //ParentContext: NULL
            {
                IList <ExecutionContext> recs = CrmService.GetExecutionContexts(PluginExecutionContext.CorrelationId.ToString(), PluginExecutionContext.OperationId.ToString()).ToList();
                PostMessages(recs);
            }

            Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));
        }
        /// <summary>
        /// Initializes the entity, by updating the target entity in the execution context.
        /// Initializes the account number
        /// </summary>
        public void InitializeEntity()
        {
            Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));

            switch (PluginExecutionContext.MessageName)
            {
            case PlatformMessageHelper.Create:
            case PlatformMessageHelper.Update:
                if (string.IsNullOrWhiteSpace(TargetBusinessEntity.AccountNumber))
                {
                    TargetBusinessEntity.AccountNumber = string.Format(CultureInfo.InvariantCulture,
                                                                       TextFormat, DateTimeNow.ToString(DateTimeFormat, CultureInfo.InvariantCulture));
                }
                PluginExecutionContext.InputParameters[PlatformConstants.TargetText] = TargetBusinessEntity.ToEntity <Entity>();
                break;
            }

            Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, UnderlyingSystemTypeName, MethodBase.GetCurrentMethod().Name));
        }