private void SetOutlookItemPropertiesFromCrmItem(EntryValue crmItem, Outlook.TaskItem olItem) { try { DateTime dateStart = crmItem.GetValueAsDateTime("date_start"); DateTime dateDue = crmItem.GetValueAsDateTime("date_due"); string timeStart = ExtractTime(dateStart); string timeDue = ExtractTime(dateDue); olItem.Subject = crmItem.GetValueAsString("name"); olItem.StartDate = MaybeChangeDate(dateStart, olItem.StartDate, "olItem.StartDate"); olItem.DueDate = MaybeChangeDate(dateDue, olItem.DueDate, "olItem.DueDate"); string body = crmItem.GetValueAsString("description"); olItem.Body = string.Concat(body, "#<", timeStart, "#", timeDue); olItem.Status = GetStatus(crmItem.GetValueAsString("status")); olItem.Importance = GetImportance(crmItem.GetValueAsString("priority")); EnsureSynchronisationPropertiesForOutlookItem(olItem, crmItem.GetValueAsString("date_modified"), DefaultCrmModule, CrmId.Get(crmItem.id)); } finally { this.SaveItem(olItem); } }
protected override SyncState <Outlook.TaskItem> AddOrUpdateItemFromCrmToOutlook(Outlook.MAPIFolder tasksFolder, string crmType, EntryValue crmItem) { SyncState <Outlook.TaskItem> result = null; Log.Debug($"TaskSyncing.AddOrUpdateItemFromCrmToOutlook\n\tSubject: {crmItem.GetValueAsString("name")}\n\tCurrent user id {RestAPIWrapper.GetUserId()}\n\tAssigned user id: {crmItem.GetValueAsString("assigned_user_id")}"); DateTime dateStart = crmItem.GetValueAsDateTime("date_start"); DateTime dateDue = crmItem.GetValueAsDateTime("date_due"); string timeStart = ExtractTime(dateStart); string timeDue = ExtractTime(dateDue); var syncState = this.GetExistingSyncState(crmItem); if (syncState == null) { /* check for howlaround */ var matches = this.FindMatches(crmItem); if (matches.Count == 0) { /* didn't find it, so add it to Outlook */ result = AddNewItemFromCrmToOutlook(tasksFolder, crmItem, dateStart, dateDue, timeStart, timeDue); } else { this.Log.Warn($"Howlaround detected? Task '{crmItem.GetValueAsString("name")}' offered with id {crmItem.GetValueAsString("id")}, expected {matches[0].CrmEntryId}, {matches.Count} duplicates"); } } else { result = UpdateExistingOutlookItemFromCrm(crmItem, dateStart, dateDue, timeStart, timeDue, syncState); } return(result); }
/// <summary> /// Update a single appointment in the specified Outlook folder with changes from CRM, but /// only if its start date is fewer than five days in the past. /// </summary> /// <param name="folder">The folder to synchronise into.</param> /// <param name="crmType">The CRM type of the candidate item.</param> /// <param name="crmItem">The candidate item from CRM.</param> /// <returns>The synchronisation state of the item updated (if it was updated).</returns> protected override SyncState <Outlook.AppointmentItem> AddOrUpdateItemFromCrmToOutlook( Outlook.MAPIFolder folder, string crmType, EntryValue crmItem) { SyncState <Outlook.AppointmentItem> result = null; DateTime dateStart = crmItem.GetValueAsDateTime("date_start"); if (dateStart >= GetStartDate()) { /* search for the item among the sync states I already know about */ var syncState = this.GetExistingSyncState(crmItem); if (syncState == null) { /* check for howlaround */ var matches = this.FindMatches(crmItem); if (matches.Count == 0) { /* didn't find it, so add it to Outlook */ result = AddNewItemFromCrmToOutlook(folder, crmType, crmItem, dateStart); } else { this.Log.Warn($"Howlaround detected? Appointment '{crmItem.GetValueAsString("name")}' offered with id {crmItem.GetValueAsString("id")}, expected {matches[0].CrmEntryId}, {matches.Count} duplicates"); } } else { /* found it, so update it from the CRM item */ result = UpdateExistingOutlookItemFromCrm(crmType, crmItem, dateStart, syncState); result?.OutlookItem.Save(); } if (crmItem?.relationships?.link_list != null) { foreach (var list in crmItem.relationships.link_list) { foreach (var record in list.records) { var data = record.data.AsDictionary(); try { this.meetingRecipientsCache[data[AddressResolutionData.EmailAddressFieldName].ToString()] = new AddressResolutionData(list.name, data); Log.Debug($"Successfully cached recipient {data[AddressResolutionData.EmailAddressFieldName]} => {list.name}, {data[AddressResolutionData.ModuleIdFieldName]}."); } catch (KeyNotFoundException kex) { Log.Error($"Key not found while caching meeting recipients.", kex); } } } } } return(result); }
private void SetOutlookItemPropertiesFromCrmItem(EntryValue crmItem, Outlook.TaskItem olItem) { try { DateTime dateStart = crmItem.GetValueAsDateTime("date_start"); DateTime dateDue = crmItem.GetValueAsDateTime("date_due"); string timeStart = ExtractTime(dateStart); string timeDue = ExtractTime(dateDue); olItem.Subject = crmItem.GetValueAsString("name"); try { olItem.StartDate = MaybeChangeDate(dateStart, olItem.StartDate, "syncState.StartDate"); } catch (COMException comx) { #if DEBUG Log.Debug($"COM Exception while trying to set start date of task: '{comx.Message}'. Some otherwise-valid tasks don't support this"); #endif } try { olItem.DueDate = MaybeChangeDate(dateDue, olItem.DueDate, "syncState.DueDate"); } catch (COMException comx) { #if DEBUG Log.Debug($"COM Exception while trying to set start date of task: '{comx.Message}'. Do some otherwise-valid tasks not support this?"); #endif } string body = crmItem.GetValueAsString("description"); olItem.Body = string.Concat(body, "#<", timeStart, "#", timeDue); olItem.Status = GetStatus(crmItem.GetValueAsString("status")); olItem.Importance = GetImportance(crmItem.GetValueAsString("priority")); EnsureSynchronisationPropertiesForOutlookItem(olItem, crmItem.GetValueAsString("date_modified"), DefaultCrmModule, CrmId.Get(crmItem.id)); } finally { this.SaveItem(olItem); } }
protected override bool IsMatch(Outlook.AppointmentItem olItem, EntryValue crmItem) { var crmItemStart = crmItem.GetValueAsDateTime("date_start"); var crmItemName = crmItem.GetValueAsString("name"); var olItemStart = olItem.Start; var subject = olItem.Subject; return(subject == crmItemName && olItemStart == crmItemStart); }
/// <summary> /// Update a single appointment in the specified Outlook folder with changes from CRM, but /// only if its start date is fewer than five days in the past. /// </summary> /// <param name="folder">The folder to synchronise into.</param> /// <param name="crmType">The CRM type of the candidate item.</param> /// <param name="crmItem">The candidate item from CRM.</param> /// <returns>The synchronisation state of the item updated (if it was updated).</returns> protected override SyncState <Outlook.AppointmentItem> AddOrUpdateItemFromCrmToOutlook( Outlook.MAPIFolder folder, string crmType, EntryValue crmItem) { SyncState <Outlook.AppointmentItem> result = null; DateTime dateStart = crmItem.GetValueAsDateTime("date_start"); if (dateStart >= GetStartDate()) { /* search for the item among the sync states I already know about */ var syncState = this.GetExistingSyncState(crmItem); if (syncState == null) { /* check for howlaround */ var matches = this.FindMatches(crmItem); if (matches.Count == 0) { /* didn't find it, so add it to Outlook */ result = AddNewItemFromCrmToOutlook(folder, crmType, crmItem, dateStart); } else { this.Log.Warn($"Howlaround detected? Appointment '{crmItem.GetValueAsString("name")}' offered with id {crmItem.GetValueAsString("id")}, expected {matches[0].CrmEntryId}, {matches.Count} duplicates"); } } else { /* found it, so update it from the CRM item */ result = UpdateExistingOutlookItemFromCrm(crmType, crmItem, dateStart, syncState); } result?.OutlookItem.Save(); // TODO TODO TODO TODO: pull and cache the recipients! } return(result); }
/// <summary> /// Get a string representing the values of the distinct fields of this crmItem, /// as a final fallback for identifying an otherwise unidentifiable object. /// </summary> /// <param name="crmItem">An item received from CRM.</param> /// <returns>An identifying string.</returns> /// <see cref="SyncState{ItemType}.IdentifyingFields"/> internal static string GetDistinctFields(EntryValue crmItem) { return($"subject: '{crmItem.GetValueAsString("name")}'; start: '{crmItem.GetValueAsDateTime("date_start")}'"); }
protected override bool IsMatch(Outlook.TaskItem olItem, EntryValue crmItem) { return(olItem.Subject == crmItem.GetValueAsString("name") && olItem.StartDate.ToUniversalTime() == crmItem.GetValueAsDateTime("newValue").ToUniversalTime()); }