private void AddMainContact(CrmLeadContact contact) { var contacts = _db.CrmLeadContacts.Where(x => x.ContactID > 100 && x.LeadID == contact.LeadID); if (contacts.Count() == 1) { var lead = _db.CrmLeads.Find(contact.LeadID); UpdateCrmLeadContactID(lead, contact.ContactID); } LogMethods.Log.Debug("AddMainContact:Debug:" + "Done"); }
/// <summary> /// Edit an existing contact /// </summary> /// <param name="contact"></param> public void EditLeadContact(CrmLeadContact contact) { try { _db.Entry(contact).State = EntityState.Modified; _db.SaveChanges(); AddMainContact(contact); LogMethods.Log.Debug("EditLeadContact:Debug:" + "Done"); } catch (DbEntityValidationException dbEx) { LogMethods.Log.Error("EditLeadContact:Error:" + dbEx.Message); } }
/// <summary> /// Add new contact to corresponding lead /// </summary> /// <param name="contact"></param> public void CreateLeadContact(CrmLeadContact contact) { _db.CrmLeadContacts.Add(contact); try { _db.SaveChanges(); AddMainContact(contact); LogMethods.Log.Debug("CreateLeadContact:Debug:" + "Done"); } catch (DbEntityValidationException dbEx) { LogMethods.Log.Error("CreateLeadContact:Error:" + dbEx.Message); } }
/// <summary> /// Delete an exising contact to corresponding lead /// </summary> /// <param name="contact"></param> /// <param name="username">user name, such as type whoami under command line \\F01\anh.tran</param> public void DeleteLeadContact(CrmLeadContact contact, string username) { var leadID = contact.LeadID; //Validation if (contact.CustomerContactID > 0) { LogMethods.Log.Error("DeleteLeadContact:Error:" + "The Lead has been convert to Customer, can not be deleted."); return; } var emp = new CrmEmployee(username); var lead = _db.CrmLeads.Find(contact.LeadID); if (emp.UserEmployeeID != lead.OriginalOwner) { LogMethods.Log.Error("DeleteLeadContact:Error:" + "Only owner can delete contact."); return; } //to be deleted contact is Main Contact if (lead.ContactID == contact.ContactID) { lead.ContactID = 0; _db.Entry(lead).Property(x => x.ContactID).IsModified = true; _db.SaveChanges(); } _db.Entry(contact).State = EntityState.Deleted; _db.SaveChanges(); var contacts = _db.CrmLeadContacts.Where(x => x.LeadID == contact.LeadID); if (contacts.Count() > 0) { var firstContact = contacts.First(); lead.ContactID = firstContact.ContactID; _db.Entry(lead).State = EntityState.Modified; _db.SaveChanges(); } LogMethods.Log.Debug("DeleteLeadContact:Debug:" + "Done"); }