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

如何通过迭代数组构建动态Salesforce复合查询并生成DWL代码

Salesforce复合查询的DWL动态实现方案

输入数据

{
  "headerId": "123",
  "Status": "Created",
  "OrderId": "232323",
  "Code": "RE",
  "Line": [{
      "lineItemId": "HP-0000074009",
      "status": null
  },
  {
      "lineItemId": "HP-0000074010",
      "status": "y"
  }
  ]
}

需求说明

迭代输入中的Line数组,完成以下操作:

  1. 对每个Line项发起GET请求,从Salesforce的Line_vod__c对象查询对应记录的Id,查询条件为Name匹配lineItemId且Code_TPI__c匹配输入的Code值。
  2. 利用查询到的Id发起PATCH请求,更新Status_TPI__c字段:status为null时设为"Open",status为"y"时设为"Closed"。

正确的DWL 2.0实现代码

%dw 2.0
output application/json
var apiVersion = "v53.0"
var code = payload.Code
// 生成所有GET查询请求
var getRequests = payload.Line map ((line, index) -> {
    method: "GET",
    url: "/services/data/$(apiVersion)/query/?q=SELECT Id FROM Line_vod__c WHERE Name='$(line.lineItemId)' AND Code_TPI__c='$(code)'",
    referenceId: "LineId_$(index + 1)"
})
// 生成所有PATCH更新请求
var patchRequests = payload.Line map ((line, index) -> {
    method: "PATCH",
    url: "/services/data/$(apiVersion)/sobjects/Line_vod__c/@{LineId_$(index + 1).records[0].Id}",
    referenceId: "UpdateLine_$(index + 1)",
    body: {
        Status_TPI__c: if (line.status == null) "Open" else "Closed"
    }
})
---
{
    allOrNone: true,
    compositeRequest: getRequests ++ patchRequests
}

代码关键点说明

  • 动态生成唯一ReferenceId:每个GET请求的referenceId设为LineId_1、LineId_2这种带索引的唯一值,避免请求引用冲突,确保PATCH能准确关联对应查询结果。
  • 参数复用:把API版本和Code值提取为变量,方便后续维护修改。
  • 条件映射状态:通过if-else判断Line项的status值,动态设置Status_TPI__c的更新内容。
  • 请求合并:用++运算符合并GET请求数组与PATCH请求数组,组成完整的复合请求列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 12:35:28