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

Try our conversational search powered by Generative AI!

Schedule Job updating customer facing content but not visible when editing.

Vote:
 

Hey guys,

I've a small niggly issue here. I've created a scheduled job to add a new value to each of the products we have. The value (integer) is stored in the database e.g. product code - ts_77 is updated with activity level = 3. This is visible on the customer facing product page.

When a member of the team want to update the content on the page the new activity level field is blank (null) which causes and issue when a manual content update occurs where a null value is published.

The code I'm using to update the database is:

public static bool UpdateActivityLevel(string code, int actLevel, string lastSyncBy)
{
if (CatalogRef == null) CatalogRef = CatalogContext.Current;

int holidayCatalogID = GetCatalogId(HOLIDAY_CATALOG_NAME);
if (holidayCatalogID == 0)
throw new Exception(string.Format("No Catalog found with the name {0}", HOLIDAY_CATALOG_NAME));

// Get a CatalogDto object.
CatalogDto catalogDto = CatalogRef.GetCatalogDto(holidayCatalogID, new CatalogResponseGroup(CatalogResponseGroup.ResponseGroup.CatalogFull));

CatalogEntryDto entryDto = null;
entryDto = CatalogRef.GetCatalogEntryDto(code, new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryInfo));

string skuMetaClassName = "Package_Product_Class";
bool retVal = false;

//try updating Package_Product_Class
try
{

MetaDataContext metaContext = CatalogContext.MetaDataContext;
MetaClass metaClass = MetaClass.Load(metaContext, skuMetaClassName);
MetaObject metaObj = MetaObject.Load(metaContext, entryDto.CatalogEntry[0].CatalogEntryId, metaClass.Id);

//only update the Activity level
SetField(metaContext, metaObj, "ActivityLevel", actLevel);
//last updated information
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "LastSync", new object[] { DateTime.UtcNow });
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "LastSyncBy", new object[] { lastSyncBy });

metaObj.AcceptChanges(metaContext);
// Save the entry.
CatalogContext.Current.SaveCatalogEntry(entryDto);

retVal = true;

} catch (Exception e)
{
ErrorLogger.LogError(e, e.Message);
retVal = false;
}

return retVal;
}

Furthermore, the LastSync field is not updating in the editing CMS end which lead me to believe both are related.

Any help / suggestions would be greatly appreciated.

Best,

John

#203307
Apr 16, 2019 12:09
Vote:
 

Hi John,

Did you check the details of the log error? What it is?

And what does the "SetField(metaContext, metaObj, "ActivityLevel", actLevel);" method do?

Best regards,

GiangTran

#203308
Apr 16, 2019 13:15
Vote:
 

Hey Giang,

The error logs are clear.... like I said in the initial comment the value is being saved it's just not coming into editing the document - the activity level field is blank.

the SetField() is just short hand for MetaHelper.SetMetaFieldValue().

#203318
Apr 16, 2019 15:33
Vote:
 

First of all we recommend to use content API instead, it is the way forward.

It sounds like everything works as expected but in Catalog UI you are seeing a different version. Make sure to add Versions gadget and check 

#203323
Apr 16, 2019 16:41
Vote:
 

Hey Quan,

are you referring to using the IContentLoader and CMS API

https://world.episerver.com/documentation/developer-guides/commerce/catalogs/catalog-content/Catalog-content-provider/

Or using the IRelationRepository stuff?

https://world.episerver.com/documentation/developer-guides/commerce/catalogs/catalog-content/Bundles-and-packages/

Best,

John

#203360
Apr 17, 2019 17:55
Vote:
 

From what I can tell you are updating some metafields, so the content API - which also works with catalog content should be used. IRelationRepository should be used when you need to update the relations between catalog entities, and it's also recommended over ICatalogSystem 

#203362
Apr 17, 2019 20:47
Vote:
 

Thanks Quan, what I needed in the end was the IContentRepository to make the update.

Worked a charm.

Thanks guys,

John

#203376
Apr 18, 2019 17:50
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.