我们正在使用com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultOutboundDeliveryV2Service
更新S / 4上的项目。请在下面找到代码片段:
DefaultOutboundDeliveryV2Service service = new DefaultOutboundDeliveryV2Service();
service.updateOutbDeliveryItem(
OutbDeliveryItem.builder()
.deliveryDocument("some key")
.deliveryDocumentItem("some key")
.actualDeliveryQuantity(BigDecimal.TEN)
.build()
).execute(someDestination);
该代码已成功执行,但是在S / 4上未进行任何更新。在检查日志时,我发现以下内容:
Update strategy is to modify with PATCH, but no fields have changed. The request may be bound to fail in the target system.
这个问题是什么原因造成的?我显然已经对actualDeliveryQuantity
领域进行了更改。为什么更新不起作用?
“没有字段已更改”,因为您已编辑了自己构建的离线实体实例。
为了使您的示例正常工作,无需进行任何调整:
OutbDeliveryItem item = OutbDeliveryItem.builder()
.deliveryDocument("some key")
.deliveryDocumentItem("some key")
.build();
// The following method registers a change of the entity.
item.setActualDeliveryQuantity(BigDecimal.TEN);
// Then execute the actual update operation, which only uses the actually changed fields.
service
.updateOutbDeliveryItem(item)
.execute(someDestination);
如果服务抱怨缺少提供的ETag,请使用“ 按键获取”方法遵循下面进一步描述的示例。或者,您明确告诉请求忽略版本标识符:
service
.updateOutbDeliveryItem(item)
.ignoreVersionIdentifier()
.execute(someDestination);
而已!
或者,为了实施完整的OData更新工作流程,您需要首先使用如下所述的“按键获取”方法来获取实体。
String deliveryDocument;
String deliveryDocumentItem;
HttpDestinationProperties someDestination;
// First get the item by key.
OutbDeliveryItem item = service
.getOutbDeliveryItemByKey(deliveryDocument, deliveryDocumentItem)
.execute(someDestination)
// The following method registers a change of the entity.
itme.actualDeliveryQuantity(BigDecimal.TEN);
// Then execute the actual update operation, which only uses the actually changed fields.
service.updateOutbDeliveryItem(item).execute(someDestination);
这样,将在内部设置ETag。实体版本标识符是大多数S / 4 OData服务启用更新/删除操作所必需的。
注意:您不仅限于密钥获取。您还可以将具有筛选器的“ 获取全部”方法用于解析多个实体,以准备进一步的更新更改。
非常感谢您的回答!由于我仅使用构建器,因此它仅创建一个实体,并且未在字段中注册任何更改。