This is the code for the DepotData class:
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.Helpers;
namespace Custom
{
public class DepotData
{
string customTableClassName = "MySite.CT_Depot";
public void AddDepot(string depotCode, string depotName, string addressLine, string city, string country, string postcode)
{
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
// Creates a new custom table item
CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName);
var customTableData = CustomTableItemProvider.GetItems(customTableClassName).WhereEquals("DepotCode", depotCode);
if (customTableData.Count == 0)
{
// Sets the values for the fields of the custom table
newCustomTableItem.SetValue("DepotCode", depotCode);
newCustomTableItem.SetValue("DepotName", depotName);
newCustomTableItem.SetValue("AddressLine", addressLine);
newCustomTableItem.SetValue("City", city);
newCustomTableItem.SetValue("Country", country);
newCustomTableItem.SetValue("DepotPostcode", postcode);
// Save the new custom table record into the database
newCustomTableItem.Insert();
}
else
{
foreach (CustomTableItem item in customTableData)
{
item.SetValue("DepotName", depotName);
item.SetValue("AddressLine", addressLine);
item.SetValue("City", city);
item.SetValue("Country", country);
item.SetValue("DepotPostcode", postcode);
// Saves the changes to the database
item.Update();
}
}
}
}
}
}
I'm testing it for now. The plan is to update the objects if there are changes every 5 mins and the data will be fetched from the SAP server. The data would be in CSV format.