You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CustomerID默认填充项目描述?Acumatica定制求助

Acumatica:给项目描述自动添加客户编码的实现问题

我需要在Acumatica中实现两个场景下自动将客户编码(AcctCD)追加到项目描述开头:

  • 场景1:选择客户时
  • 场景2:已选择客户后,选择项目模板时

在扩展PM.ProjectEntry业务逻辑时遇到两个问题:

问题1:CustomerID字段更新事件中查询客户记录报错

报错信息:Argument 1: cannot convert from 'PX.Objects.PM.ProjectEntry_Extension' to 'PX.Data.PXGraph'

问题2:选择项目模板后,项目描述未更新

尝试扩展DefaultFromTemplateProjectSettings、OnDefaultFromTemplateTasksInserted方法及TemplateID字段更新事件均无报错,但描述未修改。


修正后的完整代码

namespace PX.Objects.PM
{
    public class ProjectEntry_Extension : PXGraphExtension<PX.Objects.PM.ProjectEntry>
    {
        #region 场景1:选择客户时更新描述
        protected virtual void _(Events.FieldUpdated<PMProject, PMProject.customerID> e, PXFieldUpdated baseMethod)
        {
            baseMethod(e.Cache, e.Args);

            if (e.Row == null || e.Row.CustomerID == null) return;

            // 使用Base获取原Graph实例,修复查询报错
            Customer customer = PXSelect<Customer, 
                Where<Customer.bAccountID, Equal<Required<Customer.bAccountID>>>>
                .Select(Base, e.Row.CustomerID);
            
            if (customer != null)
            {
                string originalDesc = e.Row.Description ?? string.Empty;
                // 避免重复添加客户编码
                if (!originalDesc.StartsWith(customer.AcctCD))
                {
                    e.Cache.SetValueExt<PMProject.description>(e.Row, $"{customer.AcctCD} {originalDesc}");
                }
            }
        }
        #endregion

        #region 场景2:选择模板时更新描述
        // 订阅DefaultFromTemplateProjectSettings事件,模板复制时修改描述
        public override void Initialize()
        {
            Base.DefaultFromTemplateProjectSettings += DefaultFromTemplateProjectSettings_Handler;
        }

        protected void DefaultFromTemplateProjectSettings_Handler(PMProject prj, PMProject templ)
        {
            if (prj.CustomerID == null) return;

            Customer customer = PXSelect<Customer, 
                Where<Customer.bAccountID, Equal<Required<Customer.bAccountID>>>>
                .Select(Base, prj.CustomerID);
            
            if (customer != null)
            {
                prj.Description = $"{customer.AcctCD}: {templ.Description}";
            }
        }

        // 补充TemplateID字段更新事件,防止模板覆盖后未同步
        protected virtual void _(Events.FieldUpdated<PMProject, PMProject.templateID> e, PXFieldUpdated baseMethod)
        {
            baseMethod(e.Cache, e.Args);

            if (e.Row == null || e.Row.CustomerID == null) return;

            Customer customer = PXSelect<Customer, 
                Where<Customer.bAccountID, Equal<Required<Customer.bAccountID>>>>
                .Select(Base, e.Row.CustomerID);
            
            if (customer != null)
            {
                string currentDesc = e.Row.Description ?? string.Empty;
                if (!currentDesc.StartsWith(customer.AcctCD))
                {
                    e.Cache.SetValueExt<PMProject.description>(e.Row, $"{customer.AcctCD}: {currentDesc}");
                }
            }
        }
        #endregion
    }
}

关键修正说明

  1. 问题1修复:
    将PXSelect构造参数从this改为Base——this是当前GraphExtension实例,而PXSelect需要接收PXGraph类型的参数(即原ProjectEntry图实例)。

  2. 问题2修复:

    • 通过Initialize方法订阅DefaultFromTemplateProjectSettings事件,确保模板复制逻辑执行时触发描述修改,直接赋值prj.Description会被系统正确保存;
    • 移除硬编码的"customer.AcctCD"字符串,替换为从数据库查询到的实际客户编码;
    • 添加空值判断和重复添加校验,避免空引用异常或重复追加客户编码。

内容的提问来源于stack exchange,提问作者John Wiles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 14:45:41