powerGateServer always merges the data sent by Update-ERPObject with the current data from the erp system

Description

powerGateServer merges the data sent by the Update-ERPObject cmdlet with the data from the erp system. This behaves the same when you specify PUT or MERGE as the preferred update method.

Solution

To get around this issue you have to compare the entity that was passed into the Update function with the entity that is currently stored in the ERP system. As we don't want to write a line of code for each possible property we will iterate through all the properties, using reflection.

public override void Update(Material entity)
{
// Creates an empty material object that will be used to store the actual update values
    var updateMaterial = new Material();
// Gets the current erp entity. This probably differs in most plugins.
    var erpMaterial = ErpManager.GetMaterialyByNumber(entity.Number);
// Gets all the propertyInfos for the Material type
    var materialPropertyInfos = entity.GetType().GetProperties();

    foreach( var propertyInfo in materialPropertyInfos)
    {
// Gets the value for the current property of the passed in object
        var erpValue = propertyInfo.GetValue(erpMaterial);
        var updateValue = propertyInfo.GetValue(entity);

        if(!Object.Equals(erpValue, updateValue))
        {
            propertyInfo.SetValue(updateMaterial, updateValue);
        }
    }
// updates the erp system with the passed in data. This prbobably differs in most plugins.
    ErpManager.UpdateMaterial(updateMaterial);
}