private IList <IReportRow2> getRows(IList <string> columns, IReportFilterNode filterNode) { IList <IReportRow2> reportRows = new List <IReportRow2>(); IRecordContext _context = ((SrmReportTablePackage)this.Parent)._globalContext.AutomationContext.CurrentWorkspace; IIncident incidentRecord = null; int convId = 0, bundleId = 0; String endpoint = null; if (_context != null) { incidentRecord = _context.GetWorkspaceRecord(RightNow.AddIns.Common.WorkspaceRecordType.Incident) as IIncident; convId = ConfigurationSetting.getSrmCustomAttr(incidentRecord, "srm_conversation_id"); bundleId = ConfigurationSetting.getSrmCustomAttr(incidentRecord, "srm_bundle_id"); if (convId == 0 || bundleId == 0) { return(reportRows); } } else { if (filterNode != null && filterNode.FilterNodes != null) { IReportFilterNode convIDFilterNode = filterNode.FilterNodes.ToList <IReportFilterNode>().Find(fn => fn.ReportFilter.Expression == string.Format("{0}${1}.ConversationID", this.Parent.Name, this.Name)); if (convIDFilterNode != null) { convId = Convert.ToInt32(convIDFilterNode.ReportFilter.Value); } if (convId == 0) { return(reportRows); } IReportFilterNode bundleIDFilterNode = filterNode.FilterNodes.ToList <IReportFilterNode>().Find(fn => fn.ReportFilter.Expression == string.Format("{0}${1}.BundleID", this.Parent.Name, this.Name)); if (bundleIDFilterNode != null) { bundleId = Convert.ToInt32(bundleIDFilterNode.ReportFilter.Value); } } } endpoint = String.Format(ConfigurationSetting.convReplyGETEndpoint, convId, bundleId, ConfigurationSetting.max_srm_rows_fetch); // determine whether to close or re-open a conversation String jsonStr = "{\"status\" : \"active\"}"; HttpContent content = new StringContent(jsonStr, Encoding.UTF8, "application/json"); ConfigurationSetting.logWrap.DebugLog(logMessage: Accelerator.SRM.SharedServices.Properties.Resources.GETRequestMessage, logNote: endpoint); var results = RESTHelper.PerformGET(endpoint, ref ((SrmReportTablePackage)this.Parent)._globalContext); if (results != null) { if (!results.Success) { MessageBox.Show(Properties.Resources.GetRowsError, Properties.Resources.Error); ConfigurationSetting.logWrap.ErrorLog(logMessage: "Response GET Conversation message error", logNote: results.JSON); } var jsonData = results.JSON; if (jsonData == null || jsonData == "") { return(reportRows); } JavaScriptSerializer ser = new JavaScriptSerializer(); RootObject replies = ser.Deserialize <RootObject>(jsonData); foreach (Item req in replies.items) { ReportDataRow reportDataRow = new ReportDataRow(this.Columns.Count); object reportDataKey = req.id; foreach (var column in columns) { ReportDataCell reportDataCell = new ReportDataCell(); switch (column) { case "SRM_Data$SrmRepliesListTable.ReplyID": reportDataCell.GenericValue = req.content.id; break; case "SRM_Data$SrmRepliesListTable.liked": reportDataCell.GenericValue = Convert.ToBoolean(req.content.liked); break; case "SRM_Data$SrmRepliesListTable.likesCount": reportDataCell.GenericValue = Convert.ToInt32(req.content.likesCount); break; case "SRM_Data$SrmRepliesListTable.authorName": reportDataCell.GenericValue = req.content.author.name; break; case "SRM_Data$SrmRepliesListTable.authorImage": reportDataCell.GenericValue = req.content.author.authorImage; break; case "SRM_Data$SrmRepliesListTable.attachmentType": reportDataCell.GenericValue = req.content.attachments != null ? req.content.attachments[0].type : null; break; case "SRM_Data$SrmRepliesListTable.attachmentUrl": reportDataCell.GenericValue = req.content.attachments != null ? req.content.attachments[0].url : null; break; case "SRM_Data$SrmRepliesListTable.authorProfileUrl": reportDataCell.GenericValue = req.content.author.authorProfileUrl; break; case "SRM_Data$SrmRepliesListTable.externalId": reportDataCell.GenericValue = req.content.externalId; break; case "SRM_Data$SrmRepliesListTable.type": reportDataCell.GenericValue = req.content.type; break; case "SRM_Data$SrmRepliesListTable.postedAt": DateTime utcTime = DateTime.Parse(req.content.postedAt); DateTime localTime = utcTime.ToLocalTime(); reportDataCell.GenericValue = localTime != null?localTime.ToString() : ""; break; case "SRM_Data$SrmRepliesListTable.status": reportDataCell.GenericValue = req.content.status; break; case "SRM_Data$SrmRepliesListTable.body": reportDataCell.GenericValue = req.content.body; break; case "SRM_Data$SrmRepliesListTable.labels": if (req.content.labels.Count == 0) { reportDataCell.GenericValue = "No Value"; } else { foreach (String label in req.content.labels) { reportDataCell.GenericValue += ", " + label; } } break; } reportDataRow.Cells.Add(reportDataCell); } //Please set the Key, it is necessary to edit report cell reportDataRow.Key = reportDataKey; reportRows.Add(reportDataRow); } } return(reportRows); }
/// <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); }