Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Issues with IPriceDetailService when saving a new product programatically.

Vote:
 

Hi,

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, "");

       
_priceDetailService.Save(newPriceEntry);

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.

Thanks!

-John

#175786
Mar 01, 2017 20:42
Vote:
 

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!

-John

#175787
Mar 01, 2017 22:03
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.