我有一个对象数组作为输入,使用其中的字段,我必须为 salesforce 形成一个动态查询

I have an array of objects as an input, using the fields in it, I have to form a dynamic query for salesforce

基本上,如果我有“n”个对象作为输入,那么应该在查询中使用它来形成“n”个条件。

输入

[
{
Id: "Id1",
System: "System1"
},
{
Id: "Id2",
System: "System2"
}
]

输出

Select Name FROM Account where (Id = "Id1" and System="System1") OR (Id = "Id2" and System="System2")

警告:从字符串创建 SQL 查询可能会导致 SQL Injection 漏洞。您负责如何防止该漏洞

使用 reduce() 您可以将数组的每个元素转换为字符串以将它们全部连接起来:

%dw 2.0
output application/json
---
"Select Name FROM Account where " ++ (payload reduce ((item, acc="") -> acc ++ (if(sizeOf(acc)>0) " OR " else "") ++ "(Id = \"" ++ item.Id ++"\" and System=\"" ++ item.System ++ "\")" ))