Search code examples
jsonmule-componentmule-el

Mule ESB : Traverse a json payload and make where clause condition for Salesforce


Following is the input in terms of json payload and what is required is to form a where clause statement which I can use for salesforce query.

Note: The number of query fields can vary from 1 to n.

Input payload

{
"object_type": "contact",
"query_fields": [
{
    "field_name": "CreatedById", "field_value": "005g0000003qelYAAQ"},
{
    "field_name": "BillingState", "field_value": "KA"}
]
}

Sample output:

#[json:query_fields[0]/field_name] = '#[json:query_fields[0]/field_value]' AND #[json:query_fields[0]/field_name] = '#[json:query_fields[0]/field_value]'

Solution

  • I figured out the solution to this problem and thought of sharing the same with us all.

    Step 1: Transformed the above mentioned input payload into array:

        <dw:set-payload><![CDATA[%dw 1.0
    %output application/java
    ---
    payload map {
        field_name : $.field_name,
        field_value : $.field_value
    }]]></dw:set-payload>
    

    Step 2: Used an expression component to traverse the array and take the value of it so that I can use the values to form a where clause for the Salesforce query further in my flow.

    <![CDATA[String wrcls ="";
    int m=1;
    int n=0;
    for (String counter : flowVars.queryfields)
    {
    wrcls += payload[m].field_name[n] + " = " + "'" + payload[m].field_value[n] + "'"  +  " AND " 
    m = m + 1;
    n = n + 1;
    
    }
    
    payload = wrcls;
    
    payload = payload.replaceAll(" AND $", "");]]>