Search code examples
muledataweavemulesoft

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


Basically if I have "n" objects as input, then that should be used in the query to form "n" conditions.

Input

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

Output

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

Solution

  • WARNING: creating SQL queries from strings can be the cause of an SQL Injection vulnerability. You are responsible on how to prevent that vulnerability

    Using reduce() you can convert each element of the array into a string to concatenate them all:

    %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 ++ "\")" ))