Acumatica:通过DAC扩展实现Inventory Item ImageUrl默认值配置
实现Inventory Item空ImageUrl自动填充关联ItemClass的自定义图片字段
我来帮你搞定这个Acumatica里的字段自动填充需求!根据你的描述,你已经在Item Class界面添加了自定义字段UsrStyleImg存储基础图片,现在要让Stock Item(Inventory Item)的ImageUrl字段为空时,自动关联取对应ItemClass的这个自定义字段值,下面是具体的实现步骤和代码:
1. 确认基础配置
首先确保你已经完成了这些前置工作:
- 在Item Class DAC(
ItemClass)中成功创建了自定义字段UsrStyleImg,并且该字段已经正确保存到数据库,界面上能正常输入和存储图片路径/URL - Stock Item(
InventoryItem)的ItemClassID字段能正常关联到对应的ItemClass记录
2. 编写事件处理代码
我们可以通过Acumatica的图形扩展(Graph Extension)来实现这个逻辑,主要用到两个事件确保覆盖不同场景:
代码示例
using PX.Data; using PX.Objects.IN; using PX.Objects.CS; public class InventoryMaint_Extension : PXGraphExtension<InventoryMaint> { // 场景1:字段默认值加载时填充(新建Item或打开现有Item时触发) protected void _(Events.FieldDefaulting<InventoryItem.imageUrl> e) { InventoryItem currentItem = e.Row as InventoryItem; // 如果当前Item为空,或者ImageUrl已经有值,直接返回 if (currentItem == null || !string.IsNullOrEmpty(e.NewValue?.ToString())) return; // 查询当前Item关联的ItemClass记录 ItemClass linkedItemClass = PXSelect<ItemClass, Where<ItemClass.itemClassID, Equal<Current<InventoryItem.itemClassID>>>> .Select(Base); // 如果找到对应的ItemClass且UsrStyleImg不为空,填充到ImageUrl if (linkedItemClass != null && !string.IsNullOrEmpty(linkedItemClass.UsrStyleImg)) { e.NewValue = linkedItemClass.UsrStyleImg; } } // 场景2:保存前检查填充(防止用户手动清空ImageUrl后保存) protected void _(Events.RowPersisting<InventoryItem> e) { InventoryItem currentItem = e.Row as InventoryItem; if (currentItem == null || !string.IsNullOrEmpty(currentItem.ImageUrl)) return; // 根据ItemClassID查询对应的ItemClass记录 ItemClass linkedItemClass = PXSelect<ItemClass, Where<ItemClass.itemClassID, Equal<Required<InventoryItem.itemClassID>>>> .Select(Base, currentItem.ItemClassID); if (linkedItemClass != null && !string.IsNullOrEmpty(linkedItemClass.UsrStyleImg)) { currentItem.ImageUrl = linkedItemClass.UsrStyleImg; // 标记字段为已修改,确保保存时更新到数据库 Base.Caches[typeof(InventoryItem)].Update(currentItem); } } }
3. 代码说明
- FieldDefaulting事件:在字段首次加载、重置默认值时触发,确保新建Stock Item或者打开已有Item时,只要
ImageUrl为空就自动填充关联的图片地址 - RowPersisting事件:在保存数据前触发,覆盖用户手动清空
ImageUrl后保存的场景,保证最终存入数据库的记录符合需求 - 两处都做了空值检查,避免出现空引用异常,确保代码稳定性
4. 额外注意事项
- 确保当前操作的用户有访问ItemClass的
UsrStyleImg字段的权限,否则可能无法读取该字段的值导致填充失败 - 如果你的
UsrStyleImg存储的是本地路径,要确认Stock Item的ImageUrl字段支持这种路径格式,或者根据需求做路径转换
内容的提问来源于stack exchange,提问作者Dane
相关产品推荐
相关产品推荐

