public JsonResult Get(int id) { var apiResult = TryExecute(() => { var contact = _crmContactRepository.Get(id, "ParentAccount,Person"); var contactVm = new CRMContactModel(contact); return(contactVm); }, "Contacts Fetched sucessfully"); return(Json(apiResult, JsonRequestBehavior.AllowGet)); }
public ActionResult Update(CRMContactModel cRMContact) { ApiResult <CRMContact> apiResult; if (ModelState.IsValid) { if (cRMContact.Id > 0) { apiResult = TryExecute(() => { var person = _personRepository.GetBy(l => l.Id == cRMContact.PersonId); if (person != null) { person.Id = cRMContact.PersonId; person.FirstName = cRMContact.FirstName; person.LastName = cRMContact.LastName; person.Gender = cRMContact.Gender; person.Email = cRMContact.Email; person.Organization = cRMContact.Organization; person.Designation = cRMContact.Designation; person.PhoneNo = cRMContact.PhoneNo; person.SecondaryEmail = cRMContact.SecondaryEmail; person.OfficePhone = cRMContact.OfficePhone; person.Website = cRMContact.Website; person.Skype = cRMContact.Skype; person.Facebook = cRMContact.Facebook; person.Twitter = cRMContact.Twitter; person.GooglePlus = cRMContact.GooglePlus; person.LinkedIn = cRMContact.LinkedIn; person.City = cRMContact.City; person.Country = cRMContact.Country; person.Address = cRMContact.Address; person.CommunicationAddress = cRMContact.CommunicationAddress; person.DateOfBirth = cRMContact.DateOfBirth; _personRepository.Update(person); _unitOfWork.Commit(); } var selectedContact = _crmContactRepository.GetBy(l => l.Id == cRMContact.Id); if (selectedContact != null) { selectedContact.ParentAccountId = cRMContact.ParentAccountId; selectedContact.Expertise = cRMContact.Expertise; selectedContact.Comments = cRMContact.Comments; selectedContact.UpdatedByUserId = WebUser.Id; _crmContactRepository.Update(selectedContact); _unitOfWork.Commit(); } return(selectedContact); }, "Contact updated sucessfully"); } else { apiResult = TryExecute(() => { var newPerson = new Person { FirstName = cRMContact.FirstName, LastName = cRMContact.LastName, Gender = cRMContact.Gender, Email = cRMContact.Email, Organization = cRMContact.Organization, Designation = cRMContact.Designation, PhoneNo = cRMContact.PhoneNo, SecondaryEmail = cRMContact.SecondaryEmail, OfficePhone = cRMContact.OfficePhone, Website = cRMContact.Website, Skype = cRMContact.Skype, Facebook = cRMContact.Facebook, Twitter = cRMContact.Twitter, GooglePlus = cRMContact.GooglePlus, LinkedIn = cRMContact.LinkedIn, City = cRMContact.City, Country = cRMContact.Country, Address = cRMContact.Address, CommunicationAddress = cRMContact.CommunicationAddress, DateOfBirth = cRMContact.DateOfBirth, }; var person = _personRepository.Create(newPerson); var newContact = new CRMContact { PersonId = person.Id, ParentAccountId = cRMContact.ParentAccountId, Expertise = cRMContact.Expertise, Comments = cRMContact.Comments, CreatedByUserId = WebUser.Id }; var contact = _crmContactRepository.Create(newContact); _unitOfWork.Commit(); return(newContact); }, "Contact created sucessfully"); } } else { apiResult = ApiResultFromModelErrors <CRMContact>(); } return(Json(apiResult, JsonRequestBehavior.AllowGet)); }
public static int CRMSaveNewContactService(CRMContactModel theCnt) { int rc = 0; try { CrmServiceClient conn = new CrmServiceClient(DatabaseServices.GetCRMDBConnectionString()); IOrganizationService _orgService; _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy; Entity contact = new Entity("contact"); contact["fullname"] = theCnt.ContactName; contact["firstname"] = theCnt.FirstName; contact["lastname"] = theCnt.LastName; contact["new_specialty"] = new OptionSetValue(Convert.ToInt32(theCnt.SpecialtyID)); contact["new_npi"] = theCnt.NPI; contact["emailaddress1"] = theCnt.EMAIL; Guid objGuid = Guid.Empty; if (Guid.TryParse(theCnt.StateID, out objGuid)) { // string can be converted to GUID contact["new_state"] = new EntityReference("new_state", objGuid); } else { // can not parse state into a guid rc = 100; // bad state guid } // try to convert Account ID as a string into GUID Guid acctGuid = Guid.Empty; if (Guid.TryParse(theCnt.AccountID, out acctGuid)) { // success--not immediate action needed } else { // can not parse rc = 200; // bad Account ID } // only execute if no conversion errors if (rc == 0) { // create the contact record // returns ID of new record Guid contactID = _orgService.Create(contact); // Next create the relationship record with the Account entity Entity relationship = new Entity("new_relationship"); relationship["new_contact"] = new EntityReference("contact", contactID); relationship["statuscode"] = 1; relationship["new_account"] = new EntityReference("account", acctGuid); relationship["new_firstname"] = theCnt.FirstName; relationship["new_lastname"] = theCnt.LastName; relationship["new_name"] = theCnt.ContactName; Guid relationshipID = _orgService.Create(relationship); } } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { Console.WriteLine(ex.Message); rc = 300; // try-catch exception } return(rc); }
public static List <CRMContactModel> GetCRMContactsForRelationshipService(string relationshipID) { List <CRMContactModel> theContacts = new List <CRMContactModel>(); try { CrmServiceClient conn = new CrmServiceClient(DatabaseServices.GetCRMDBConnectionString()); IOrganizationService _orgService; _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy; QueryExpression query = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet("contactid", "fullname"), Criteria = { Filters = { new FilterExpression { FilterOperator = LogicalOperator.And, Conditions = { new ConditionExpression("contactid", ConditionOperator.Equal, relationshipID.Trim()) } } } } }; EntityCollection entityRecords = _orgService.RetrieveMultiple(query); // see if data returned if (entityRecords != null && entityRecords.Entities.Count > 0) { CRMContactModel entityModel; for (int i = 0; i < entityRecords.Entities.Count; i++) { // new blank copy of the C# model entityModel = new CRMContactModel(); if (entityRecords[i].Contains("contactid") && entityRecords[i]["contactid"] != null) { entityModel.Id = entityRecords[i]["contactid"].ToString(); } if (entityRecords[i].Contains("fullname") && entityRecords[i]["fullname"] != null) { entityModel.ContactName = CRMUtilityServices.GetTitleCaseService(entityRecords[i]["fullname"].ToString()); } // add row to collection theContacts.Add(entityModel); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(theContacts); }