-3

I have a getrecord component which I am getting all Case record from salesforce. My requirement I need to grab all case.ID in one array so that I can pass that array to second query. How I can build a array of IDs.

I tried the below code but didnt worked:

vars.idList.add[payload]

enter image description here

enter image description here

----------------- XML Code -----------------------

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:os="http://www.mulesoft.org/schema/mule/os"
    xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/os http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="e3ca913f-748b-419a-aa1f-38203698eac9" >
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <salesforce:sfdc-config name="MuleSFAccount" doc:name="Salesforce Config" doc:id="eb518df4-3a84-4f1d-be74-e631ff23ae11" >
        <salesforce:basic-connection username="XXXXX@myorg.com" password="XXXXXXXXXXX" securityToken="zKGdzbzxtZ7F36L2D2AzcONhL" url="https://xyzllc6-dev-ed.my.salesforce.com/services/Soap/u/54.0"/>
    </salesforce:sfdc-config>
    <flow name="SFQuery" doc:id="672e35c6-cae6-4519-9fbd-9251b7b9a028" >
        <http:listener doc:name="Listener" doc:id="3b2c2d05-40e2-4e99-83cf-4a57afb1022d" config-ref="HTTP_Listener_config" path="/myquery"/>
        <salesforce:query doc:name="getCaseRecord" doc:id="4db8b2d9-4298-410c-8201-ba5437d50197" config-ref="MuleSFAccount">
            <salesforce:salesforce-query ><![CDATA[SELECT ID FROM CASE]]></salesforce:salesforce-query>
            <salesforce:headers />
        </salesforce:query>
        <ee:transform doc:name="dispCaseRec" doc:id="9f1104ac-cdaa-4858-8525-33c3d60e18c3" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <set-variable value="#[[]]" doc:name="Set Variable" doc:id="b29750af-75fc-4931-a0d4-3f4cfaeb1182" variableName="idList"/>
        <foreach doc:name="For Each" doc:id="215aa0fa-f136-4331-9205-6cf9ff353e70" >
            <ee:transform doc:name="Transform Message" doc:id="48e2e2ed-0fa7-4ceb-80e1-11f9833c4617" >
                <ee:message >
                    <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
vars.idList.add[payload]]]></ee:set-payload>
                </ee:message>
            </ee:transform>
        </foreach>
        <salesforce:query doc:name="getContentLink" doc:id="bdc79d59-59c1-4586-bcd0-a880ff5a21b5" config-ref="MuleSFAccount" target="varCont">
            <salesforce:salesforce-query ><![CDATA[SELECT ContentDocumentId,Id,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId = 'idVal']]></salesforce:salesforce-query>
            <salesforce:parameters ><![CDATA[#[output application/java
---
{
    "idVal" : "idList"
}]]]></salesforce:parameters>
        </salesforce:query>
        <ee:transform doc:name="Transform Message" doc:id="40d10daa-00c2-407d-83cb-9e123f965a79" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <foreach doc:name="For Each" doc:id="5bc9f699-b83a-4f7b-a598-7bb936d0100f" >
            <ee:transform doc:name="Transform Message" doc:id="ca3c8579-2efa-46ff-9896-4613f706cdbc" >
                <ee:message >
                    <ee:set-payload ><![CDATA[%dw 2.0
output application/JSON
---
payload.Id joinby ',']]></ee:set-payload>
                </ee:message>
            </ee:transform>
        </foreach>
    </flow>
</mule>
Tamil
  • 115
  • 4
  • Isn't the output of Query already an Array? – Harshank Bansal Jul 25 '22 at 04:54
  • Also use XML code snippet too along with images as it is hard to understand where the problem is just with images. What is the dw in `dispCaseRec` transformer? What is the `set-variable`? – Harshank Bansal Jul 25 '22 at 04:58
  • @HarshankBansal my apologies. This is the first time I am posting in Mulesoft forum. Didn't know that need to post xml code. I have updated my post. – Tamil Jul 25 '22 at 12:19
  • I am unable to understand the problem here. But if you only want the `Id` you just need to do `payload.Id` after the query in your `set-variable`. You will get array of ID string. No need for the `foreach` loop or `dispCaseRec` transformer. – Harshank Bansal Jul 25 '22 at 12:29
  • Hi @HarshankBansal, yes I tried. in getContentLink Query I tried '#[payload.Id]', payload.id nothing worked. I need array list in this format ('5003R000002MjnfQAC','5003R000002MjnfQBC','5003R000002MjnfQCC'). I hardcode value like this in getContentLink component and it worked. set-variable and foreach I am trying to build an array in the format which I mentioned above. – Tamil Jul 25 '22 at 13:21
  • @Tamil this is Stackoverflow.com, not a discussion forum. You should get familiar with the way this site operates. I recommend to start by reading the help on how to ask questions: https://stackoverflow.com/help/asking. About your question, you are making it more complex than it needs to be. For a DataWeave question you should at the least provide the input, the expected output, actual output and the scripts you attempted. All in textual form. Avoid screenshots except when it is a graphical error. Your complete flow may not be needed if you provide enough context, and it may be confusing. – aled Jul 25 '22 at 13:23
  • Okay, so what your are looking for is completely different then what you are asking. What you are looking for is a way to get a **comma separated string** from an array of IDs so that you can use it in SF query. What you are asking is how to get an array of IDs. Can you update your question likewise ? – Harshank Bansal Jul 25 '22 at 13:37

2 Answers2

1

You are trying to use Java syntax in DataWeave which is a completely different language. DataWeave doesn't allow method execution in that way. But you should not try to execute methods at all. DataWeave is a functional language and you should learn how to use correctly. In this particular case you have an array of objects. For DataWeave objects are sets of key-values.

Use the multi valued selector will return all values of the desired key in an array.

Example:

Input:

[
    {
        "id": 1,
        "x": "aaa"

    },
    {
        "id": 2,
        "x": "bbb"
    }
]

Script:

payload.*id

Output:

[
  1,
  2
]
aled
  • 21,330
  • 3
  • 27
  • 34
  • You don't need the * in this case just payload.id as payload is an Array it will apply it to all the elements in the array that are objects – machaval Jul 25 '22 at 17:12
  • Hi @machaval I tried I am getting following error :"org.mule.extension.salesforce.internal.error.exception.service.SalesforceException: invalid parameter value" – Tamil Jul 25 '22 at 21:06
  • This is an error on the salesforce side. Meaning that the value you are trying to assing to a parameter is not the valid, is not a problem on the DW inside – machaval Jul 25 '22 at 21:44
  • No, the payload is coming as {"ID" :"payload.Id"} which query where clause is not understand. I need value to be passed as ('5003R000002MjnfQAC','5003R000002MjnfQBC','5003R000002MjnfQCC'). If I hard code this value I am getting output. – Tamil Jul 25 '22 at 21:59
  • Either the input is not as expected or you are doing something different in the script. Please share both – aled Jul 26 '22 at 00:56
0

You don't need the transformer and the foreach loop. You can just create SOQL statement after getting the result of your first Query. You can get the Array of IDs after first query with payload.Id or payload.*Id. But you can not just pass it in your SOQL statement. You need to convert this to a string that is comma separated and enclose each element in single quotes. '...','...'
This can be done in many ways.I have used this expression payload.*Id map "'$$'" joinBy ','. The map function "'$$'" may seem confusing but it just means "'" ++ $ ++ "'"

<salesforce:query doc:name="getCaseRecord" doc:id="4db8b2d9-4298-410c-8201-ba5437d50197" config-ref="MuleSFAccount">
    <salesforce:salesforce-query><![CDATA[SELECT ID FROM CASE]]></salesforce:salesforce-query>
    <salesforce:headers/>
</salesforce:query>
<salesforce:query doc:name="getContentLink" doc:id="bdc79d59-59c1-4586-bcd0-a880ff5a21b5" config-ref="MuleSFAccount" target="varCont">
    <salesforce:salesforce-query><![CDATA[SELECT ContentDocumentId,Id,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN (:idVal)]]></salesforce:salesforce-query>
    <salesforce:parameters><![CDATA[#[output application/json
---
{
    "idVal" : payload.*Id map "'$$'" joinBy ','
}]]]></salesforce:parameters>
</salesforce:query>
Harshank Bansal
  • 2,798
  • 2
  • 7
  • 22