public void Shouldnt_create_a_duplicate_phone_call_history_record() { var ctx = new XrmFakedContext(); var contact = new Contact() { Id = Guid.NewGuid() }; var phoneCall = new PhoneCall() { Id = Guid.NewGuid(), RegardingObjectId = contact.ToEntityReference(), PhoneNumber = "+34666666666" }; var existingPhoneCallHistory = new ultra_phonecallhistory() { Id = Guid.NewGuid(), ultra_contactid = contact.ToEntityReference(), ultra_phonenumber = phoneCall.PhoneNumber }; ctx.Initialize(existingPhoneCallHistory); ctx.ExecutePluginWithTarget <PhoneCallCreatePlugin>(phoneCall); var historyRecords = ctx.CreateQuery <ultra_phonecallhistory>().ToList(); Assert.Equal(1, historyRecords.Count); }
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 void Execute(IServiceProvider serviceProvider) { #region Boilerplate // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); #endregion if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { var target = context.InputParameters["Target"] as Entity; var phoneCall = target.ToEntity <PhoneCall>(); 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(); } } } }
public void Should_not_create_phonecall_history_record_for_the_same_phonenumber_and_contact_pair() { var existingPhoneCallHistoryRecord = new ultra_phonecallhistory() { Id = Guid.NewGuid(), ultra_contactid = _contact.ToEntityReference(), ultra_phonenumber = _phoneCall.PhoneNumber }; _context.Initialize(existingPhoneCallHistoryRecord); _context.ExecutePluginWithTarget <PhoneCallCreatePlugin>(_phoneCall); var phoneCallHistoryList = _context.CreateQuery <ultra_phonecallhistory>().ToList(); Assert.Single(phoneCallHistoryList); }
public void Execute(IServiceProvider serviceProvider) { #region Boilerplate // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); #endregion if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { var phoneCall = (context.InputParameters["Target"] as Entity).ToEntity <PhoneCall>(); using (var ctx = new XrmServiceContext(service)) { var existingPhoneCallHistoryRecord = (from ph in ctx.CreateQuery <ultra_phonecallhistory>() where ph.ultra_contactid.Id == phoneCall.RegardingObjectId.Id where ph.ultra_phonenumber == phoneCall.PhoneNumber select ph).FirstOrDefault(); if (existingPhoneCallHistoryRecord == null) { var phoneCallHistory = new ultra_phonecallhistory() { ultra_contactid = phoneCall.RegardingObjectId, ultra_phonenumber = phoneCall.PhoneNumber }; service.Create(phoneCallHistory); } } } }