/// <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);
        }
        /// <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);
        }
Example #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);
        }
        /// <summary>
        /// Attempt to save me given these contact Ids and this HTML body, taking note of these previous failures.
        /// </summary>
        /// <param name="contactIds">CRM ids of contacts to which I should be related.</param>
        /// <param name="htmlBody">The HTML body with which I should be saved.</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 TrySave(List <string> contactIds, string htmlBody, Exception[] fails)
        {
            var restServer  = SuiteCRMUserSession.RestServer;
            var emailResult = restServer.GetCrmResponse <RESTObjects.SetEntryResult>("set_entry",
                                                                                     ConstructPacket(htmlBody));
            ArchiveResult result = ArchiveResult.Success(emailResult.id, fails);

            SaveContacts(contactIds, emailResult);

            SaveAttachments(emailResult);

            return(result);
        }
        /// <summary>
        /// Attempt to save me given these related records and this HTML body, taking note of these previous failures.
        /// </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 TrySave(IEnumerable <CrmEntity> relatedRecords, Exception[] fails)
        {
            CrmRestServer  restServer  = SuiteCRMUserSession.RestServer;
            SetEntryResult emailResult = restServer.GetCrmResponse <RESTObjects.SetEntryResult>("set_entry",
                                                                                                ConstructPacket(this.HTMLBody));
            ArchiveResult result = ArchiveResult.Success(emailResult.id, fails);

            if (result.IsSuccess)
            {
                LinkRelatedRecords(relatedRecords, emailResult);
                SaveAttachments(emailResult);
            }

            return(result);
        }