public static EntityReference GetAccountReferenceByNameFuzzy(this IOrganizationService service, string companyName) { var name = CompanyNameHelper.RemoveCommonCompanySuffixes(companyName); var nameCondition = ConditionExpressionHelper.CreatePublisherNameBeginsWithCondition(name); var filter = new FilterExpression(); filter.Conditions.Add(nameCondition); var query = new QueryExpression(Account.EntityLogicalName); query.Criteria.AddFilter(filter); var accounts = service.RetrieveMultiple(query).Entities; if (accounts.Count() > 1) { throw new CrmAmbiguousMatchException( string.Format("Found multiple fuzzy matches when searching for {0}. Fuzzy match search: {1}", companyName, name)); } var account = accounts.FirstOrDefault(); if (account == null) { throw new CrmEntityNotFoundException("Account not found with name: " + companyName); } return(new EntityReference(Account.EntityLogicalName, account.Id)); }
public string UpdateContactWithVcard(vCard vcard, string filename) { // pre-checks if (string.IsNullOrWhiteSpace(vcard.FamilyName)) { return(string.Format("{0}: not imported - family name not provided", filename)); } if (string.IsNullOrWhiteSpace(vcard.GivenName)) { return(string.Format("{0}: not imported - given name not provided", filename)); } if (string.IsNullOrWhiteSpace(vcard.Organization)) { return(string.Format("{0}: not imported - company not provided", filename)); } if (!vcard.EmailAddresses.Any()) { return(string.Format("{0}: not imported - email address not provided", filename)); } string result; // Search for the company (account) in the CRM var companyNameShortend = CompanyNameHelper.RemoveCommonCompanySuffixes(vcard.Organization); var account = this.orgContext.CreateQuery <Account>().FirstOrDefault(a => a.Name.StartsWith(companyNameShortend)); if (account != null) { // If it exists, update it this.UpdateAccountWithVcard(account, vcard); this.orgContext.UpdateObject(account); result = "Existing account updated"; } else { // If it not yet exists, create it account = this.UpdateAccountWithVcard(null, vcard); account.OwnerId = new EntityReference(this.impersonatedUser.LogicalName, this.impersonatedUser.Id); this.orgContext.AddObject(account); result = "New account created"; } // Save the account, so it receices an ID this.orgContext.SaveChanges(); Contact contact = null; // Try to get the contact by Firstname, Lastname and Company name if (vcard.GivenName != null && vcard.FamilyName != null && vcard.Organization != null) { contact = (from c in this.orgContext.CreateQuery <Contact>() where c.FirstName == vcard.GivenName.Trim() && c.LastName == vcard.FamilyName.Trim() && c.ParentCustomerId == new EntityReference(account.LogicalName, account.Id) select c).FirstOrDefault(); } // Try to search contact by email if (vcard.EmailAddresses.Any() && vcard.EmailAddresses[0].Address != null) { contact = contact ?? this.orgContext.CreateQuery <Contact>().FirstOrDefault(c => c.EMailAddress1 == vcard.EmailAddresses[0].Address.Trim()); } if (contact != null) { // If the contact is found, update it this.UpdateContactWithVcard(contact, vcard); contact.ParentCustomerId = new EntityReference(account.LogicalName, account.Id); this.orgContext.UpdateObject(contact); result += ", Existing contact updated"; } else { // If the contact was not found, insert it contact = this.UpdateContactWithVcard(null, vcard); contact.ParentCustomerId = new EntityReference(account.LogicalName, account.Id); contact.OwnerId = new EntityReference(this.impersonatedUser.LogicalName, this.impersonatedUser.Id); this.orgContext.AddObject(contact); result += ", New contact created"; } // Save the contact this.orgContext.SaveChanges(); return(filename + ": " + result); }