Developer forum

Forum » Integration » Updating product tries to insert the product instead of updating.

Updating product tries to insert the product instead of updating.

Davor Zlotrg
Reply

Hi,

I have an issue when saving the product. Instead of updating it is trying to insert the new one. This is the example code.

var dw = Product.GetProductByNumber(bp.sku, "LANG1");
if (dw == nullreturn;
dw.Save();

 

When dw is null and I call Save it inserts the product just fine but when the product is already in the database it does not update but throws the exception:

System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'DW_PK_EcomProducts'. Cannot insert duplicate key in object 'dbo.EcomProducts'. The duplicate key value is (PROD306, LANG1, ).

 

The version I am using is 8.8.1.6

 

Also there is an issue when inserting 2 or more variants for a product when there are no VariantOptions defined in the database.

VariantOption vo = VariantOption.GetAllVariantOptions().FirstOrDefault(c => c.Name == optionName);
 
if (vo != nullreturn vo;
//create new variant option
var newOption = new VariantOption();
newOption.GroupID = vg.ID;
newOption.LanguageID = "LANG1";
newOption.Name = optionName;
newOption.Save();

 

The workarround I made was, that before adding a variant I try to "clear the cache" for product. The issue is that without clearing the cache VariantNumber.NumberParser(num, vo.ID) returns the empty string.

 

var dwProduct = GetProductNoCache(productId);
var num = string.Format("{0}", vo.ID);
var combo = VariantNumber.NumberParser(num, dwProduct);
var var = new VariantCombination();
var.ProductID = dwProduct.ID;
var.VariantID = combo;
var.Save(dwProduct.ID, combo);

 

private Product GetProductNoCache(string productId)
{
      if (HttpContext.Current != null)
          HttpContext.Current.Items.Clear();
 
      return Product.GetProductByID(productId);
}

 

Of course this is not a solution, there may be other bugs lurking arround, can you figure out what's wrong? I can provide the full solution if you want.


Replies

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Davor,
could you attach your full solution?
Regards, Dmitrij

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Davor,
I've reproduced the first problem with products:

Product p = new Product();
            p.Number = "xyz";
            p.LanguageID = "Lang101";
            p.Save();

gives me error when "Lang101"  is not my default language.

To fix this problem you need to use SaveAndConfirm(dw.ID, dw.VariantID, dw.LanguageID) (instead of just Save()) this will save the product directly in your specific language:

dw.SaveAndConfirm(dw.ID, dw.VariantID, dw.LanguageID);

Clearing cache in HttpContext.Items is fine as Product.GetProductByID is using this caching to decrease amount of calls to database for product retrieving.

Try do not use the VariantNumber.NumberParser instead you can save the variant combination like this:

var var = new VariantCombination();
var.ProductID = dwProduct.ID;
var.VariantID = vo.ID;
var.Save(dwProduct.ID, vo.ID);

Regards, Dmitrij

 

 
Davor Zlotrg
Reply

Hi Dmitriy,

1. Where did you come up with "LANG101"? I have the correct language (LANG1) and it is the default language.

 

2. The method dw.SaveAndConfirm is not available in this version that I am using?

 

3. I already tried to not use VariantNumber.NumberParser and using vo.ID and it didn't work. 

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply

Hi Davor,
1 and 2:
"LANG101"  is my custom language and in my case when I've the default language "LANG1" and try to use Save() which gave me yours expcetion.
The reason when you are trying to save it even in default language and it doens't work is ability that current context language is not a default language,
and it uses Current Context Language(you can chec your context language by eCommerce.Common.Context.LanguageID property)
so it can fail.

This method is available, it is just not browsable by default(but you can browse it by pressing F12 on Product class/this method name), but it allows to save to the direct language without depending on current context/default langauge.

3 Is there some exception? Have you check the database table EcomVariantOptionsProductRelation if it contains the data, so maybe it is some caching issue?

 
Davor Zlotrg
Reply

Aah yes, that explains it, SaveAndConfirm works fine now

About no. 3 - there is no exception, it's just that NumberParser returns an empty string and variant is not saved. Clearing the cache helped, it's just that I am afraid that something else might be causing this. 

 
Dmitriy Benyuk Dynamicweb Employee
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Davor,
Regarding 3, yes you need to clear the Items cache for getting correct/latest product information for Number parser work. This is needed since when you create a new variant group or add a new variant option to variant group the product is already cached(with old varinat group with old variant options) to the Items cache before those events. If you do not want to clear all HttpContext.Items cache you can just remove the one for cached product:

Dim cacheKey As String = productID & "_" & variantID & "_" & languageID
HttpContext
.Current.Items.Remove(cacheKey)

In your case when you don't have a variantid and language id you need to call it:
Dim cacheKey As String = productID & "_" & string.Empty & "_" & string.Empty
HttpContext.Current.Items.Remove(cacheKey)

Regards, Dmitrij

Votes for this answer: 1

 

You must be logged in to post in the forum