private bool TryAddRecipientInModule(string moduleName, CrmId meetingId, Outlook.Recipient recipient) { bool result; CrmId id = SetCrmRelationshipFromOutlook(this, meetingId, recipient, moduleName); if (CrmId.IsValid(id)) { string smtpAddress = recipient.GetSmtpAddress(); this.CacheAddressResolutionData( new AddressResolutionData(moduleName, id, smtpAddress)); CrmId accountId = CrmId.Get(RestAPIWrapper.GetRelationship( ContactSynchroniser.CrmModule, id.ToString(), "accounts")); if (CrmId.IsValid(accountId) && SetCrmRelationshipFromOutlook(this, meetingId, "Accounts", accountId)) { this.CacheAddressResolutionData( new AddressResolutionData("Accounts", accountId, smtpAddress)); } result = true; } else { result = false; } return(result); }
/// <summary> /// Check meeting acceptances for all future meetings. /// </summary> private int CheckMeetingAcceptances() { int result = 0; foreach (MeetingSyncState state in SyncStateManager.Instance.GetSynchronisedItems <MeetingSyncState>().Where(s => s.VerifyItem())) { Outlook.AppointmentItem item = state.OutlookItem; try { if (CrmId.Get(item.UserProperties[OrganiserPropertyName]?.Value) .Equals(RestAPIWrapper.GetUserId()) && item.Start > DateTime.Now) { result += AddOrUpdateMeetingAcceptanceFromOutlookToCRM(item); } } catch (TypeInitializationException tix) { Log.Warn("Failed to create CrmId with value '{item.UserProperties[OrganiserPropertyName]?.Value}'", tix); } catch (COMException comx) { ErrorHandler.Handle($"Item with CRMid {state.CrmEntryId} appears to be invalid (HResult {comx.HResult})", comx); this.HandleItemMissingFromOutlook(state); } } return(result); }
/// <summary> /// Find all CRM records related to this recipient of this meeting, and produce address /// resolution data from them. /// </summary> /// <param name="olItem">An appointment, assumed to be a meeting.</param> /// <param name="recipient">A recipient of that meeting request.</param> /// <returns>A list of address resolution objects.</returns> private List <AddressResolutionData> ResolveRecipient(Outlook.AppointmentItem olItem, Outlook.Recipient recipient) { List <AddressResolutionData> result = new List <AddressResolutionData>(); var smtpAddress = recipient.GetSmtpAddress(); Log.Info($"recepientName= {recipient.Name}, recepient= {smtpAddress}"); if (this.meetingRecipientsCache.ContainsKey(smtpAddress)) { result.AddRange(meetingRecipientsCache[smtpAddress]); } else { CrmId meetingId = olItem.GetCrmId(); Dictionary <string, CrmId> moduleIds = new Dictionary <string, CrmId>(); if (CrmId.IsValid(meetingId)) { foreach (string moduleName in new string[] { "Leads", "Users", ContactSynchroniser.CrmModule }) { CrmId moduleId = this.GetInviteeIdBySmtpAddress(smtpAddress, moduleName); if (CrmId.IsValid(moduleId)) { moduleIds[moduleName] = moduleId; AddressResolutionData data = new AddressResolutionData(moduleName, moduleId, smtpAddress); this.CacheAddressResolutionData(data); result.Add(data); } } if (moduleIds.ContainsKey(ContactSynchroniser.CrmModule)) { CrmId accountId = CrmId.Get(RestAPIWrapper.GetRelationship( ContactSynchroniser.CrmModule, moduleIds[ContactSynchroniser.CrmModule].ToString(), "accounts")); if (CrmId.IsValid(accountId) && SetCrmRelationshipFromOutlook(this, meetingId, "Accounts", accountId)) { var data = new AddressResolutionData("Accounts", accountId, smtpAddress); this.CacheAddressResolutionData(data); result.Add(data); } } } } return(result); }
/// <summary> /// Get the existing sync state for this CRM item, if it exists, else null. /// </summary> /// <param name="crmItem">The item.</param> /// <returns>The appropriate sync state, or null if none.</returns> public SyncState GetExistingSyncState(EntryValue crmItem) { SyncState result; string outlookId = crmItem.GetValueAsString("outlook_id"); CrmId crmId = CrmId.Get(crmItem.id); if (this.byCrmId.ContainsKey(crmId)) { result = this.byCrmId[crmId]; } else if (this.byOutlookId.ContainsKey(outlookId)) { result = this.byOutlookId[outlookId]; } else if (this.byGlobalId.ContainsKey(outlookId)) { result = this.byGlobalId[outlookId]; } else { string simulatedGlobalId = SyncStateManager.SimulateGlobalId(crmId); if (this.byGlobalId.ContainsKey(simulatedGlobalId)) { result = this.byGlobalId[simulatedGlobalId]; } else { string distinctFields = GetDistinctFields(crmItem); if (string.IsNullOrEmpty(distinctFields)) { result = null; } else if (this.byDistinctFields.ContainsKey(distinctFields)) { result = this.byDistinctFields[distinctFields]; } else { result = null; } } } return(result); }
/// <summary> /// Set all those properties of this outlook item whose values are different from the /// equivalent values on this CRM item. Update the synchronisation properties only if some /// other property has actually changed. /// </summary> /// <param name="crmItem">The CRM item from which to take values.</param> /// <param name="olItem">The Outlook item into which to insert values.</param> /// <returns>true if anything was changed.</returns> private void SetOutlookItemPropertiesFromCrmItem(EntryValue crmItem, Outlook.ContactItem olItem) { try { olItem.FirstName = crmItem.GetValueAsString("first_name"); olItem.LastName = crmItem.GetValueAsString("last_name"); olItem.Email1Address = crmItem.GetValueAsString("email1"); olItem.BusinessTelephoneNumber = crmItem.GetValueAsString("phone_work"); olItem.HomeTelephoneNumber = crmItem.GetValueAsString("phone_home"); olItem.MobileTelephoneNumber = crmItem.GetValueAsString("phone_mobile"); olItem.JobTitle = crmItem.GetValueAsString("title"); olItem.Department = crmItem.GetValueAsString("department"); olItem.BusinessAddressCity = crmItem.GetValueAsString("primary_address_city"); olItem.BusinessAddressCountry = crmItem.GetValueAsString("primary_address_country"); olItem.BusinessAddressPostalCode = crmItem.GetValueAsString("primary_address_postalcode"); olItem.BusinessAddressState = crmItem.GetValueAsString("primary_address_state"); olItem.BusinessAddressStreet = crmItem.GetValueAsString("primary_address_street"); olItem.Body = crmItem.GetValueAsString("description"); if (crmItem.GetValue("account_name") != null) { olItem.Account = crmItem.GetValueAsString("account_name"); olItem.CompanyName = crmItem.GetValueAsString("account_name"); } olItem.BusinessFaxNumber = crmItem.GetValueAsString("phone_fax"); olItem.Title = crmItem.GetValueAsString("salutation"); if (olItem.Sensitivity != Outlook.OlSensitivity.olNormal) { Log.Info($"ContactSyncing.SetOutlookItemPropertiesFromCrmItem: setting sensitivity of contact {crmItem.GetValueAsString("first_name")} {crmItem.GetValueAsString("last_name")} ({crmItem.GetValueAsString("email1")}) to normal"); olItem.Sensitivity = Outlook.OlSensitivity.olNormal; } EnsureSynchronisationPropertiesForOutlookItem( olItem, crmItem.GetValueAsString("date_modified"), crmItem.GetValueAsString("sync_contact"), CrmId.Get(crmItem.id)); } finally { this.SaveItem(olItem); } }
/// <summary> /// Construct a JSON packet representing this Outlook item, and despatch it to CRM. /// </summary> /// <param name="olItem">The Outlook item.</param> /// <returns>The CRM id of the object created or modified.</returns> protected override CrmId ConstructAndDespatchCrmItem(Outlook.ContactItem olItem) { return(CrmId.Get(RestAPIWrapper.SetEntry(new ProtoContact(olItem).AsNameValues(), this.DefaultCrmModule))); }
protected override void SetMeetingStatus(Outlook.AppointmentItem olItem, EntryValue crmItem) { olItem.MeetingStatus = CrmId.Get(crmItem.GetValueAsString("assigned_user_id")).Equals(RestAPIWrapper.GetUserId()) ? Outlook.OlMeetingStatus.olMeeting : Outlook.OlMeetingStatus.olMeetingReceived; }
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); } }
/// <summary> /// Get the single CrmId instance for this value. /// </summary> /// <param name="value">The value to seek.</param> /// <returns>A CrmId instance</returns> /// <exception cref="TypeInitializationException"> if `value` does not appear to be a valid CRM id.</exception> public static CrmId Get(object value) { return(value == null ? CrmId.Empty : CrmId.Get(value.ToString())); }
/// <summary> /// Construct a JSON packet representing the Outlook item of this sync state, and despatch /// it to CRM. /// </summary> /// <param name="syncState">The Outlook item.</param> /// <returns>The CRM id of the object created or modified.</returns> protected override CrmId ConstructAndDespatchCrmItem(SyncState <Outlook.TaskItem> syncState) { return(CrmId.Get(RestAPIWrapper.SetEntry(new ProtoTask(syncState.OutlookItem).AsNameValues(), this.DefaultCrmModule))); }
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); } }