使用Acumatica.RESTClient更新基于属性的自定义字段问题
我正在使用Acumatica RESTClient和Acumatica Default_22.200.001库构建API接口,连接至Default端点。采购订单屏幕存在一个基于属性的用户自定义字段(UDF)。
我能够创建采购订单(PO),但UDF字段未被更新。生成的JSON请求中,Custom字段仅显示属性的Type,却不包含Value。以下是相关代码片段:
public class CustomStringField : Acumatica.RESTClient.ContractBasedApi.Model.CustomField { public string Value { get; set; } }
Acumatica.RESTClient.ContractBasedApi.Model.CustomStringField customStringField = new Acumatica.RESTClient.ContractBasedApi.Model.CustomStringField() { viewName = "Document", fieldName = "AttributeESTPONBR", Type = "CustomStringField", Value = "E123" };
PurchaseOrder order = new PurchaseOrder() { Type = "Normal", Date = poDate, PromisedOn = estDelDate, VendorID = vendor.VendorID, VendorRef = vQuote, Description = notes, Note = splInstr, Custom = new Dictionary<string, Dictionary<string, Acumatica.RESTClient.ContractBasedApi.Model.CustomField>> { { "Document", new Dictionary<string, Acumatica.RESTClient.ContractBasedApi.Model.CustomField> { {"AttributeESTPONBR", customStringField } } } }, Details = details };
生成的请求体如下:
{"Date":{"value":"2024-03-15T00:00:00"},"Description":{"value":"test"},"Details":[{"InventoryID":{"value":"ARCHITECTURAL DOORS"},"OrderQty":{"value":10.0},"UnitCost":{"value":325.0},"UOM":{"value":"EACH"}}],"PromisedOn":{"value":"2024-03-15T00:00:00"},"Type":{"value":"Normal"},"VendorID":{"value":"DOUMOC"},"VendorRef":{"value":"r456"},"custom":{"Document":{"AttributeESTPONBR":{"Type":"CustomStringField"}}},"note":{"value":"test"}}
问题原因及解决方案
1. 序列化格式不匹配
Acumatica REST API要求所有字段值(包括自定义字段)都必须嵌套在value键下,而你自定义的CustomStringField类中的Value属性直接暴露为字符串,没有被包装成API期望的{"value": "E123"}结构,导致序列化时未正确传递值。
2. 正确的实现方式
不需要自定义CustomStringField类,直接使用库中内置的类型或构造符合API要求的结构:
// 构造符合要求的自定义字段 var customField = new Acumatica.RESTClient.ContractBasedApi.Model.CustomField { Type = "CustomStringField", Value = new { value = "E123" } // 用匿名类型包装value }; PurchaseOrder order = new PurchaseOrder() { Type = "Normal", Date = poDate, PromisedOn = estDelDate, VendorID = vendor.VendorID, VendorRef = vQuote, Description = notes, Note = splInstr, Custom = new Dictionary<string, Dictionary<string, Acumatica.RESTClient.ContractBasedApi.Model.CustomField>> { { "Document", new Dictionary<string, Acumatica.RESTClient.ContractBasedApi.Model.CustomField> { {"AttributeESTPONBR", customField } } } }, Details = details };
3. 其他注意事项
- 确认系统中自定义属性的ID完全是
AttributeESTPONBR,名称不匹配会导致字段无法识别。 - 无需扩展Default端点,只要该自定义属性已在采购订单屏幕配置完成,且API用户拥有对应访问权限,即可通过Default端点操作。
内容的提问来源于stack exchange,提问作者Raj

