/// <summary>
        /// Save my email to CRM, and link it to these contact ids.
        /// </summary>
        /// <remarks>
        /// In the original code there were two entirely different ways of archiving emails; one did the trick of
        /// trying first with the HTML body, and if that failed trying again with it empty. The other did not.
        /// I have no idea whether there is a benefit of this two-attempt strategy.
        /// </remarks>
        /// <param name="crmContactIds">The contact ids to link with.</param>
        public ArchiveResult Save(List <string> crmContactIds)
        {
            ArchiveResult result;

            try
            {
                result = TrySave(crmContactIds, this.HTMLBody, null);
            }
            catch (Exception firstFail)
            {
                log.Warn($"ArchiveableEmail.Save: failed to save '{this.Subject}' with HTML body", firstFail);

                try
                {
                    result = TrySave(crmContactIds, string.Empty, new[] { firstFail });
                }
                catch (Exception secondFail)
                {
                    log.Error($"ArchiveableEmail.Save: failed to save '{this.Subject}' at all", secondFail);
                    result = ArchiveResult.Failure(new[] { firstFail, secondFail });
                }
            }

            return(result);
        }
        /// <summary>
        /// Delete my existing record and create a new one.
        /// </summary>
        /// <param name="relatedRecords">CRM module names/ids of records to which I should be related.</param>
        /// <param name="fails">Any previous failures in attempting to save me.</param>
        /// <returns>An archive result object describing the outcome of this attempt.</returns>
        private ArchiveResult TryUpdate(IEnumerable <CrmEntity> relatedRecords, Exception[] fails)
        {
            ArchiveResult result;

            try
            {
                // delete
                NameValue[] deletePacket = new NameValue[2];
                deletePacket[0] = RestAPIWrapper.SetNameValuePair("id", this.CrmEntryId);
                deletePacket[1] = RestAPIWrapper.SetNameValuePair("deleted", "1");
                RestAPIWrapper.SetEntry(deletePacket, "Emails");
                // recreate
                result = this.TrySave(relatedRecords, fails);
            }
            catch (Exception any)
            {
                List <Exception> newFails = new List <Exception>();
                newFails.Add(any);
                if (fails != null && fails.Any())
                {
                    newFails.AddRange(fails);
                }
                result = ArchiveResult.Failure(newFails.ToArray());
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Save my email to CRM, and link it to these contact ids.
        /// </summary>
        /// <remarks>
        /// In the original code there were two entirely different ways of archiving emails; one did the trick of
        /// trying first with the HTML body, and if that failed trying again with it empty. The other did not.
        /// I have no idea whether there is a benefit of this two-attempt strategy.
        /// </remarks>
        /// <param name="relatedRecords">CRM module names/ids of records to which I should be related.</param>
        public ArchiveResult Save(IEnumerable <CrmEntity> relatedRecords)
        {
            ArchiveResult result;

            if (relatedRecords.Count() == 0)
            {
                result = ArchiveResult.Failure(
                    new[] { new Exception("Found no related entities in CRM to link with") });
            }
            else
            {
                try
                {
                    result = TrySave(relatedRecords, this.HTMLBody, null);
                }
                catch (Exception firstFail)
                {
                    log.Warn($"ArchiveableEmail.Save: failed to save '{this.Subject}' with HTML body", firstFail);

                    try
                    {
                        result = TrySave(relatedRecords, string.Empty, new[] { firstFail });
                    }
                    catch (Exception secondFail)
                    {
                        log.Error($"ArchiveableEmail.Save: failed to save '{this.Subject}' at all", secondFail);
                        result = ArchiveResult.Failure(new[] { firstFail, secondFail });
                    }
                }
            }

            return(result);
        }