I am building out functionality for a site that reads product data from a service and programatically creates them in Episerver. I can't seem to get the price to save correctly using IPriceDetailService. Not sure what I am missing. Here is how I am saving the price currently:
private static IPriceDetailService _priceDetailService = ServiceLocator.Current.GetInstance();
PriceDetailValue newPriceEntry = new PriceDetailValue();
CatalogKey ck = new CatalogKey(AppContext.Current.ApplicationId, product.ProductID);
var currency = new Currency("USD");
var money = new Money(Convert.ToDecimal(product.SalesPrice), currency);
newPriceEntry.CatalogKey = ck;
newPriceEntry.PriceValueId = 0;
newPriceEntry.MinQuantity = 1;
newPriceEntry.MarketId = MarketId.Default.Value;
newPriceEntry.UnitPrice = money;
newPriceEntry.ValidFrom = DateTime.Now.AddDays(-1);
newPriceEntry.ValidUntil = DateTime.Now.AddYears(20);
newPriceEntry.CustomerPricing = new CustomerPricing(0, "");
Here is the error I get when trying to save:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PriceDetail_CatalogEntry". The conflict occurred in database "epiEcom", table "dbo.CatalogEntry".
Here are my versions of Episerver
Any help would be greatly appreciated.
Figured it out! So, I was trying to add and save pricing to a new variant before it was created/published. I was populating the data, adding pricing then using IContentRepository to save/publish. I moved the pricing into it's own fucntion, which I now call directly after saving/publishing the new variant, and, now everything is working as expected!