public void SendViewNotificationEmailWithTable(string recipientType, Guid recipientId, IEnumerable <Entity> recordsToList, LocalisationService localisationService, IDictionary <string, string> aliasTypeMaps)
        {
            if (recordsToList.Any())
            {
                var crmUrl         = GetCrmURL();
                var isToOwner      = Target.GetOptionSetValue(Fields.jmcg_workflowtask_.jmcg_viewnotificationoption) == OptionSets.WorkflowTask.ViewNotificationOption.EmailOwningUsers;
                var fieldsForTable = GetViewLayoutcellFieldNames()
                                     .Except(new[] { XrmService.GetPrimaryKeyField(recordsToList.First().LogicalName) })
                                     .ToList();
                if (isToOwner && fieldsForTable.Contains("ownerid"))
                {
                    fieldsForTable.Remove("ownerid");
                }

                var appId = recipientType == Entities.systemuser
                    ? WorkflowSchedulerService.GetUserAppId(recipientId, Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_fieldforteamappid))
                    : WorkflowSchedulerService.GetQueueAppId(recipientId, Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_fieldforteamappid));
                var email = new HtmlEmailGenerator(XrmService, crmUrl, appId);
                email.AppendParagraph(string.Format("This is an automated notification {0} {1}"
                                                    , isToOwner ? "that you own" : "there are"
                                                    , View.GetStringField(Fields.savedquery_.name)));
                var notes = Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_emailnotes);
                if (!string.IsNullOrWhiteSpace(notes))
                {
                    email.AppendParagraph(notes.Replace("\n", "<br />"));
                }
                email.AppendTable(recordsToList, localisationService, fields: fieldsForTable, aliasTypeMaps: aliasTypeMaps);
                var viewName = View.GetStringField(Fields.savedquery_.name);
                var subject  = viewName + " Notification";
                SendNotificationEmail(recipientType, recipientId, subject, email.GetContent());
            }
        }
Example #2
0
        protected override void Execute()
        {
            var behindSchedule = IsBehindSchedule();

            ActivityThisType.IsBehindSchedule.Set(ExecutionContext, behindSchedule);
            if (behindSchedule)
            {
                //if there is recurring instance, but we are behind schedule
                //then probably went into an error waiting state
                //so lets stop it
                try
                {
                    WorkflowSchedulerService.StopContinuousWorkflowFor(TargetId);
                }
                catch (Exception ex)
                {
                    Trace(string.Format("Error stopping continuous workflows: {0}", ex.XrmDisplayString()));
                }
                //and start a fresh continuous one
                try
                {
                    WorkflowSchedulerService.StartNewContinuousWorkflowFor(TargetId);
                }
                catch (Exception ex)
                {
                    Trace(string.Format("Error starting continuous workflow: {0}", ex.XrmDisplayString()));
                }
            }
            if (behindSchedule && Target.GetBoolean(Fields.jmcg_workflowtask_.jmcg_sendnotificationforschedulefailures))
            {
                var recipientQueue = Target.GetLookupGuid(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto);
                if (!recipientQueue.HasValue)
                {
                    throw new NullReferenceException(string.Format("Error required field {0} is empty on the target {1}", XrmService.GetFieldLabel(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TargetType), XrmService.GetEntityLabel(TargetType)));
                }

                var crmUrl = GetCrmURL();
                var email  = new HtmlEmailGenerator(XrmService, crmUrl, null);
                email.AppendParagraph(string.Format("This is an automated notification that the {0} named '{1}' fell behind schedule. The system has attempted to restart the continuous workflow but any failed instance should be reviewed"
                                                    , XrmService.GetEntityLabel(TargetType)
                                                    , Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_name)));
                if (!string.IsNullOrWhiteSpace(crmUrl))
                {
                    email.AppendParagraph(email.CreateHyperlink(email.CreateUrl(Target), "Open " + XrmService.GetEntityLabel(TargetType)));
                }

                var subject = XrmService.GetEntityLabel(TargetType) + " Schedule Failure: " + Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_name);
                SendNotificationEmail(Entities.queue, recipientQueue.Value, subject, email.GetContent());

                Target.SetField(Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime, DateTime.UtcNow);
                XrmService.Update(Target, new[] { Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime });
            }
        }
        protected override void Execute()
        {
            var failedJobs    = GetfailedJobs();
            var hasNewFailure = failedJobs.Any();

            ActivityThisType.HasNewFailures.Set(ExecutionContext, hasNewFailure);
            if (hasNewFailure && Target.GetBoolean(Fields.jmcg_workflowtask_.jmcg_sendnotificationfortargetfailures))
            {
                var recipientQueue = Target.GetLookupGuid(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto);
                if (!recipientQueue.HasValue)
                {
                    throw new NullReferenceException(string.Format("Error required field {0} is empty on the target {1}", XrmService.GetFieldLabel(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TargetType), XrmService.GetEntityLabel(TargetType)));
                }

                var crmUrl = GetCrmURL();
                var email  = new HtmlEmailGenerator(XrmService, crmUrl, null);
                email.AppendParagraph(string.Format("This is an automated notification that workflows triggered by the {0} named '{1}' have failed. You will need to review the failures to fix any issues"
                                                    , XrmService.GetEntityLabel(TargetType)
                                                    , Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_name)));
                var notes = Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_notesfortargetfailureemail);
                if (!string.IsNullOrWhiteSpace(notes))
                {
                    email.AppendParagraph(notes.Replace("\n", "<br />"));
                }
                if (!string.IsNullOrWhiteSpace(crmUrl))
                {
                    email.AppendParagraph(email.CreateHyperlink(email.CreateUrl(Target), "Open " + XrmService.GetEntityLabel(TargetType)));
                }
                email.AppendTable(failedJobs, LocalisationService, GetFieldsToDisplayInNotificationEmail());

                var subject = XrmService.GetEntityLabel(TargetType) + " Target Failures: " + Target.GetStringField(Fields.jmcg_workflowtask_.jmcg_name);
                SendNotificationEmail(Entities.queue, recipientQueue.Value, subject, email.GetContent());

                Target.SetField(Fields.jmcg_workflowtask_.jmcg_minimumtargetfailuredatetime, DateTime.UtcNow);
                XrmService.Update(Target, new[] { Fields.jmcg_workflowtask_.jmcg_minimumtargetfailuredatetime });
            }
        }
Example #4
0
        public override void ExecuteExtention(ImportSqlRequest request, ImportSqlResponse response,
                                              ServiceRequestController controller)
        {
            Exception tempEx = null;;

            try
            {
                controller.Controller.UpdateProgress(0, 1, "Loading Records For Import");
                var dictionary    = LoadMappingDictionary(request);
                var importService = new SpreadsheetImportService(XrmRecordService);
                var responseItems = importService.DoImport(dictionary, request.MaskEmails, request.MatchRecordsByName, request.UpdateOnly, controller);
                response.LoadSpreadsheetImport(responseItems);
            }
            catch (Exception ex)
            {
                tempEx = ex;
                throw;
            }
            finally
            {
                if (request.SendNotificationAtCompletion &&
                    (!request.OnlySendNotificationIfError ||
                     tempEx != null || response.HasResponseItemError))
                {
                    try
                    {
                        //todo place the html generator somewhere appropriate
                        var htmlGenerator = new HtmlEmailGenerator();
                        //okay lets try create an email with the summary
                        //plus perhaps a log of the errors

                        if (tempEx != null)
                        {
                            htmlGenerator.AppendParagraph(tempEx.XrmDisplayString());
                        }

                        htmlGenerator.WriteObject(response);

                        var subject = $"Sql Import '{request.Name ?? "No Name"}'";
                        if (tempEx != null)
                        {
                            subject = subject + " Fatal Error";
                        }
                        else if (response.HasResponseItemError)
                        {
                            subject = subject + " Completed With Errors";
                        }
                        else
                        {
                            subject = subject + " Completed";
                        }



                        XrmRecordService.SendEmail(request.SendNotificationFromQueue, request.SendNotificationToQueue
                                                   , subject.Left(XrmRecordService.GetMaxLength(Fields.email_.subject, Entities.email))
                                                   , htmlGenerator.GetContent());
                    }
                    catch (Exception ex)
                    {
                        response.AddResponseItem(new ImportSqlResponseItem(new DataImportResponseItem("Error Sending Notification Email", ex)));
                    }
                }
            }
        }