protected override GenericResult ConcreteExecute(IOrganizationService service) { var contact = PhoneCall.RegardingObjectId; var phoneNumber = PhoneCall.PhoneNumber; //query to create a phone history only if the pair [phonenumber, contact] doesn't exist using (var ctx = new XrmServiceContext(service)) { var exists = (from ph in ctx.CreateQuery <ultra_phonecallhistory>() where ph.ultra_contactid.Id == contact.Id where ph.ultra_phonenumber == phoneNumber select ph).FirstOrDefault() != null; if (!exists) { //Create phone history record var phoneHistory = new ultra_phonecallhistory() { ultra_contactid = contact, ultra_phonenumber = phoneNumber, ultra_lastcalldate = DateTime.Now }; phoneHistory.Id = service.Create(phoneHistory); //Phonecall then assigned to the created phonecall history PhoneCall.ultra_phonecallhistoryid = phoneHistory.ToEntityReference(); } } return(GenericResult.Succeed()); }
public GenericResult Save(ContactDetailsModel model) { using (var ctx = new OrganizationServiceContext(_service)) { var existingContact = (from c in ctx.CreateQuery(Contact) where (string)c[Email] == model.Email select c).FirstOrDefault(); if (existingContact == null) { _service.Create(model.ToEntity()); return(GenericResult.Succeed()); } else { return(GenericResult.FailWith($"Contact with email '{model.Email}' already exists")); } } }