コード例 #1
0
        /// <summary>
        /// Method to save the SRM record
        /// </summary>
        /// <param name="attempts"></param>
        /// <returns></returns>
        private bool SaveRecord(int attempts = 0)
        {
            Stopwatch clickLogStopwatch = new Stopwatch();

            clickLogStopwatch.Start();
            IThread   thread;
            Stopwatch stopwatch = new Stopwatch();

            try
            {
                IIncident incident = _recordContext.GetWorkspaceRecord(RightNow.AddIns.Common.WorkspaceRecordType.Incident) as IIncident;

                String endpoint = null;
                int    convId = 0, bundleId = 0, externalRefId = 0;
                convId        = ConfigurationSetting.getSrmCustomAttr(incident, "srm_conversation_id");
                bundleId      = ConfigurationSetting.getSrmCustomAttr(incident, "srm_bundle_id");
                externalRefId = ConfigurationSetting.getSrmCustomAttr(incident, "srm_external_reference_id");

                //get custom field for retrieving social channel constraints
                string socialConversationReplyMode = ConfigurationSetting.getSrmStringCustomAttr(incident, "srm_conversation_reply_mode");

                if (_incidentStatusWhenLoaded != incident.Status.StatusID)
                {
                    endpoint = String.Format(ConfigurationSetting.convPUTEndpoint, convId, externalRefId, bundleId);

                    // determine whether to close or re-open a conversation
                    String jsonStr = "{\"status\" : \"active\"}";
                    if (incident.Status.StatusID == ConfigurationSetting.closedConversationStatusId)
                    {
                        jsonStr = "{\"status\" : \"completed\"}"; // close the SRM conversation
                    }

                    ConfigurationSetting.logWrap.DebugLog(logMessage: Accelerator.SRM.SharedServices.Properties.Resources.PUTRequestMessage, logNote: String.Format("{0} {1}", endpoint, jsonStr));
                    var results = RESTHelper.PerformPUT(endpoint, jsonStr, ref _GlobalContext);

                    if (!results.Success)
                    {
                        MessageBox.Show(results.Message);
                    }
                }

                int threadsCreated = incident.Threads.Count - _threadCountWhenLoaded;

                //Get current conversation channel constraints - including char limit and social handle
                int  charLimit           = 0;
                bool includeSocialHandle = false;
                if (threadsCreated > 0)
                {
                    Constraint constraint = GetCurrentIncidentConversationConstraints(_socialChannelType, socialConversationReplyMode);
                    if (constraint != null)
                    {
                        charLimit           = constraint.char_limit < 0 ? 0 : constraint.char_limit;
                        includeSocialHandle = constraint.include_social_handle == true ? true : false;
                    }
                    else
                    {
                        ConfigurationSetting.logWrap.DebugLog(logMessage: String.Format(Properties.Resources.ConstraintsNotDefinedError, _socialChannelType));
                    }
                }

                //Check char limit, if one of the threads exceed the max limit, cancel saving event
                if (charLimit > 0)
                {
                    bool isSucceed = CharLimitChecking(incident, charLimit, _socialChannelType);
                    if (!isSucceed)
                    {
                        return(false);
                    }
                }
                else
                {
                    ConfigurationSetting.logWrap.DebugLog(logMessage: Properties.Resources.CharacterLimitError);
                }

                //Set social handel
                string socialHandle = "";
                if (includeSocialHandle && !String.IsNullOrEmpty(_socialChannelUserName))
                {
                    socialHandle = "@" + _socialChannelUserName + " ";
                }

                for (int i = 1; i < threadsCreated + 1; i++)
                {
                    thread = incident.Threads[incident.Threads.Count - i];

                    string contentThread = thread.Note;
                    if (thread.ContentType == RightNow.AddIns.Common.ThreadContentType.HTML)
                    {
                        string note     = thread.Note;
                        string note_raw = Regex.Replace(note, "<.*?>", string.Empty);
                        if (note_raw.Length > 0)
                        {
                            note_raw = note_raw.TrimStart('\n').TrimEnd('\n');
                        }
                        contentThread = WebUtility.HtmlDecode(note_raw);
                    }
                    else
                    {
                        contentThread = contentThread.Remove(contentThread.Length - 1, 1);
                    }

                    bool isSrmEngage = false;
                    foreach (int chanId in ConfigurationSetting.SrmEngageChannelId)
                    {
                        if (thread.ChanID == chanId)
                        {
                            isSrmEngage = true;
                            break;
                        }
                    }

                    if (isSrmEngage)
                    {
                        endpoint = String.Format(ConfigurationSetting.convReplyPOSTEndpoint, convId, bundleId);

                        String jsonStr = "{\"body\" : \"";
                        jsonStr += socialHandle + contentThread + "\",\"externalType\": \"rightnow\"}";
                        ConfigurationSetting.logWrap.DebugLog(logMessage: Accelerator.SRM.SharedServices.Properties.Resources.POSTRequestMessage, logNote: String.Format("{0} {1}", endpoint, jsonStr));
                        var results = RESTHelper.PerformPOST(endpoint, jsonStr, ref _GlobalContext);

                        if (!results.Success)
                        {
                            MessageBox.Show(results.Message);
                        }
                        if (incident.Status.StatusID != ConfigurationSetting.closedConversationStatusId)
                        {
                            incident.Status.StatusID = (int)ConfigurationSetting.openConversationStatusId;
                        }
                    }
                }

                updateSocialChannelAccount(incident);
                clickLogStopwatch.Stop();
                ConfigurationSetting.logWrap.ClickLog(0, 0, "SrmCreateReplyAddIn _recordContext_Saving() total time:", null, (int)clickLogStopwatch.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                MessageBox.Show(Properties.Resources.CommunicationError, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                ConfigurationSetting.logWrap.ErrorLog(logMessage: e.Message, logNote: e.InnerException != null ? e.InnerException.Message : null);
                return(false);
            }

            return(true);
        }