0
votes

I am trying to update the Inventory Item ImageUrl if it is found to be null with some conditions. I have added a Usr field called UsrStyleImg to the Item Class screen. This field is for a basic image of an item and it is stored in the database. The functionality I want is if the Inventory Item does not have an image in the ImageUrl then it will default to the UsrStyleImg that is connected with the ItemClassID. ItemClassID is also found on the Stock Item Screen. Here is the code I have in the InventoryItemMaint graph:

protected void InventoryItem_ImageUrl_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e)
    {

      var row = (InventoryItem)e.Row;
      if (row == null) return;
      var item = (INItemClass)PXSelect<INItemClass, Where<INItemClass.itemClassID, Equal<Current<InventoryItem.itemClassID>>>>.Select(Base, row.ItemClassID);
      var image = PXSelect<InventoryItem, Where<InventoryItem.imageUrl, Equal<Current<InventoryItem.imageUrl>>>>.Select(Base, row.ImageUrl);
      if (image != null)
        return;
      else {
        e.NewValue = item.GetExtension<INItemClassExt>().UsrStyleImg;
      }
    }

The code compiles fine but when I test with an Item that has an Item Class attached to it with an image in the INItemClass table called UsrStyleImg it does not populate to the imageUrl found in the Inventory Item table or the Stock Item screen. I have also tried this with FieldSelecting and using the e.ReturnValue with still the same results.

If I need more clarification please let me know.

1

1 Answers

0
votes

Try using a RowSelecting Event

protected virtual void InventoryItem_RowSelecting(PXCache sender, PXRowSelectingEventArgs e)
{
    InventoryItem row = e.Row as InventoryItem;

    //Extra checks to prevent infinite loops
    if (row != null && !string.IsNullOrWhiteSpace(row.InventoryCD) && Base.Item.Cache.GetStatus(row) == PXEntryStatus.Notchanged)
    {
        if (!string.IsNullOrWhiteSpace(row.ItemClassID))
        {
            //You must always use a PXConnectionScope if Selecting during RowSelecting
            using (new PXConnectionScope())
            {
                //If you're going to pass in a value in .Select, use Required instead of Current.
                INItemClass itemClass = PXSelectReadonly<INItemClass, Where<INItemClass.itemClassID, Equal<Required<INItemClass.itemClassID>>>>.Select(Base, row.ItemClassID);

                if (itemClass != null && string.IsNullOrWhiteSpace(row.ImageUrl))
                {
                    INItemClassExt itemClassExt = itemClass.GetExtension<INItemClassExt>();

                    //To prevent unneeded update if it's blank
                    if (!string.IsNullOrWhiteSpace(itemClassExt.UsrStyleImg))
                    {
                        row.ImageUrl = itemClassExt .UsrStyleImg;

                        //Force set the status in the Cache, otherwise it infinite loops
                        Base.Item.Cache.SetStatus(row, PXEntryStatus.Updated);
                        Base.Item.Update(row);
                    }                        
                }
            }
        }
    }
}