In my flow in Mule 4 I am trying to query a database for specific data.
For example I want to run a query like this:
SELECT * FROM mulesoft WHERE plant = CCCNNB;
The thing is both plant and CCNNB need to be dynamic. They will come through an API request. I can handle the value to be dynamic, but I get empty results whenever I try to make the field dynamic.
I first create a variable which stores the json from the request:
set-variable value="#[payload]" doc:name="Set Variable" doc:id="8ed26865-d722-4fdb-9407-1f629b45d318" variableName="SORT_KEY"/>
Request looks like this:
{
"FILTER_KEY": "plant",
"FILTER_VALS": "CCNNB"
}
Afterwards in the db connector I configure the following:
<db:select doc:name="Select" doc:id="13a66f51-2a4e-4949-b383-86c43056f7a3" config-ref="Database_Config">
<db:sql><![CDATA[SELECT * FROM mulesoft WHERE :filter_key = :filter_val;]]></db:sql>
<db:input-parameters ><![CDATA[#[{
"filter_val": vars.SORT_KEY.FILTER_VALS,
"filter_key": vars.SORT_KEY.FILTER_KEY
}]]]></db:input-parameters>
Replacing :filter_key with plant works but as soon as I try to make it dynamic I get nothing in the response. It does not fail though, response code is 200 but I get nothing inside it.
How can I make this work?
You can directly use the stored variables in the query itself.
Query Should be an expression in DataWeave.
#["SELECT * FROM $(vars.table) WHERE $(vars.SORT_KEY.FILTER_KEY) = :filter_val"]
<db:select config-ref="Database_Config">
<db:sql><![CDATA[#["SELECT * FROM $(vars.table) WHERE $(vars.SORT_KEY.FILTER_KEY) = :filter_val"]]]></db:sql>
<db:input-parameters ><![CDATA[#[{
"filter_val": vars.SORT_KEY.FILTER_VALS
}]]]>
</db:input-parameters>
</db:select>
There is another way also to read values from payload to build a dynamic query as below
#["SELECT * FROM mulesoft
WHERE " ++ vars.SORT_KEY.FILTER_KEY ++ " = '" ++ vars.SORT_KEY.FILTER_VALS ++ "'"]
Below is the XML that is created for this, as a POC
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:os="http://www.mulesoft.org/schema/mule/os"
xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:xml-module="http://www.mulesoft.org/schema/mule/xml-module"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/os http://www.mulesoft.org/schema/mule/os/current/mule-os.xsd">
<http:listener-config name="HTTP_Listener_config1"
doc:name="HTTP Listener config"
doc:id="6d5de64b-1355-4967-9352-4b324f02c7ad">
<http:listener-connection host="0.0.0.0"
port="8081" />
</http:listener-config>
<db:config name="Database_Config" doc:name="Database Config"
doc:id="d5c4d49c-aef3-4d4a-a7b5-470da3354127">
<db:my-sql-connection host="localhost"
port="3306" user="root" password="admin123" database="Mysql" />
</db:config>
<flow name="testFlow"
doc:id="8cfea1b0-d244-40d9-989c-e136af0d9f80" initialState="started">
<http:listener doc:name="Listener"
doc:id="265e671b-7d2f-4f3a-908c-8065a5f36a07"
config-ref="HTTP_Listener_config1" path="test" />
<set-variable value="#[payload]" doc:name="Set Variable"
doc:id="265a16c5-68d4-4217-8626-c4ab0a3e38e5" variableName="SORT_KEY" />
<db:select doc:name="Select"
doc:id="bdf4a59c-0bcc-46ac-8258-f1f1762c4e7f"
config-ref="Database_Config">
<db:sql><![CDATA[#["SELECT * FROM mulesoft.mulesoft WHERE " ++ vars.SORT_KEY.FILTER_KEY ++ " = '" ++ vars.SORT_KEY.FILTER_VALS ++ "'"]]]></db:sql>
</db:select>
<ee:transform doc:name="Transform Message"
doc:id="72cbe69f-c52e-4df9-ba5b-dd751990bc08">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>
Explanation of the Flow
I am using the payload that is in Question
Seting a variable name "SORT_KEY", value of this varibale is complete payload that we receive.
then creating a dynamic query inside the Db connector
using transform message sending the data as response, that we received from DataBase
So, there are several issues here. One, the creation of the sql statement.
You can do DW inside the DB:SELECT component if you want, as shown by previous answers. Either the
#["SELECT * FROM myTable" ++ vars.myWhere]
OR
#["SELECT * FROM myTable $(vars.myWhere)"]
work.
The problem you will run into is that DataSense doesn't like this. Without the literal text for the column names, DataSense can't figure out what columns to retrieve so it raises an error "Unable to resolve value for the prameter: sql". This leaves an error in your code, which always gives me angst. I wish Mulesoft would resolve this problem.
BTW, if you do dynamic SQL, you should STILL use input parameters for each value to avoid SQL injections.
I have an "Idea" posted here to fix the bogus error: https://help.mulesoft.com/s/ideas#0872T000000XbjkQAC
Related
This question already has an answer here:
Karate: Using data-driven embedded template approach for API testing
(1 answer)
Closed 1 year ago.
I am looking for a way to use data driven in combination with an external request file.
So my feature file looks like this:
Feature: EPOS UNIT test - GetSpendingLimit
Background:
* url 'http://xxx-yyy-zzz'
* def GetSpendingLimit_request = read('classpath:examples/EPOS/request/GetSpendingLimit-dd-request.xml')
* def GetSpendingLimit_data = read('classpath:examples/EPOS/data/GetSpendingLimit.csv')
Scenario Outline: GetSpendingLimit External Request Datadriven
Given request GetSpendingLimit_request
When soap action 'TotalAmount'
Then status 200
# define a variable to check the response
* def total_amount = /Envelope/Body/GetSpendingLimitResponse/spendingLimit/totalAmount
# to print the result to the report
* print '\nTotal Amount is: ', total_amount
Examples:
|read('classpath:examples/EPOS/data/GetSpendingLimit.csv')|
And my request looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:req="http://xxx-yyy-zzz/GetSpendingLimit/Request" xmlns:com="http://xxx-yyy-zzz/Common" xmlns:ser="http://xxx-yyy-zzz/Common/ServiceContext">
<soapenv:Header/>
<soapenv:Body>
<req:GetSpendingLimitRequest>
<ser:productServiceContext>
<ser:conversationId>GetSpendingLimit_1_1</ser:conversationId>
<ser:deviceTypeId>1</ser:deviceTypeId>
<ser:entityId>nl</ser:entityId>
<ser:product>
<ser:id><product-id></ser:id>
</ser:product>
<ser:user>
<ser:id><user-id></ser:id>
</ser:user>
</ser:productServiceContext>
</req:GetSpendingLimitRequest>
</soapenv:Body>
</soapenv:Envelope>
Notice that I have added product-id and user-id as variable which should be replaced by data driven input from the csv. But I get the following error:
[Fatal Error] :10:43: The element type "product-id" must be terminated by the matching end-tag "</product-id>".
11:35:18.404 [main] ERROR com.intuit.karate - src/test/java/examples/EPOS/GetSpendingLimit-external-request-datadriven.feature:5
* def GetSpendingLimit_request = read('classpath:examples/EPOS/request/GetSpendingLimit-dd-request.xml')
js failed:
>>>>
01: read('classpath:examples/EPOS/request/GetSpendingLimit-dd-request.xml')
<<<<
I have tried the request in the feature file and this works fine. But the request in external file fails.
The error is clearly saying the XML is not well-formed. Use some XML tool and fix it, for e.g. something like this: https://www.freeformatter.com/xml-formatter.html
For example this should be:
<ser:id><product-id/></ser:id>
I'm starting a bulk API job and passing a spec file. Here is the job.txt file I'm using to create the job:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>upsert</operation>
<object>Contact</object>
<externalIdFieldName>Unique_Email__c</externalIdFieldName>
<contentType>CSV</contentType>
</jobInfo>
Then, I'm posting a spec file as follows:
Salesforce Field,Csv Header,Value,Hint
Unique_Email__c,email1,,
Name,Full Name,,
And I get the error "Transformation specification is missing a mapping for the external id field" but that's very strange because the Unique_Email__c is actually there. What's going on?
I have a Mule application that is writing a file in an outbound endpoint, with the config below:
<file:outbound-endpoint path="${Outbound}" outputPattern="outputFile_#[function:datestamp:yyyyMMddHHmmss].csv" doc:name="Output File"/>
Following this point in the flow I need to display a log message along the lines of "Successfully created file {filename}".
The issue I'm having is that I can't find a way of displaying the name of the file I have just created. I could put: Successfully created file outputFile_#[function:datestamp:yyyyMMddHHmmss].csv, but there is a chance that the datestamp may differ by one second.
Is there a way in Mule that I can display the name of the file I've just written?
UPDATE
Following the response from #til_b, I've achieved this using the following:
<set-variable value="outputFile_#[function:datestamp:yyyyMMddHHmmss].csv" variableName="Filename" doc:name="Variable"/>
<file:outbound-endpoint path="${Outbound}" outputPattern="#[variable:Filename]" doc:name="Output File"/>
<logger level="INFO" message="Successfully created file #[variable:Filename]" doc:name="Logger" />
I dont know about mule, but when i encounter such a problem while programming i store the generated filename in a variable, and then use that variable to actually create the file and display the message.
In Pseudocode:
var filename = #yyyymmddhhMMss.csv
create_file(filename)
log_message(filename + ' successfully created')
When creating a new bulk job through the API, I am getting an InvalidEntity as an exception code which isn't in SFDC's docs (pdf)
I'm making the following request
https://na13-api.salesforce.com/services/async/24.0/job
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<operation>query</operation>
<object>Campaign</object>
<concurrencyMode>Parallel</concurrencyMode>
<contentType>CSV</contentType>
</jobInfo>
And I get this response:
<?xml version="1.0" encoding="UTF-8"?><error
xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<exceptionCode>InvalidEntity</exceptionCode>
<exceptionMessage>Entity 'Campaign' is not supported by the Bulk API.</exceptionMessage>
</error>
I've excluded the headers but I do have accept & content encoding specified as gzip and the content is gzipped when it is sent and received.
The requests work correctly for
Lead
Contact
Opportunity
Task
Event
OpportunityContactRole
Account
CampaignMember
Note
Profile
RecordType
User
I only get the exception shown above for:
Campaign
UserLicense
OpportunityStage
OpportunityHistory
LeadHistory
I'm only using the bulk api to download csv's out of salesforce and I've only tried the above entities because I don't need any others.
It has been a very long time, but I think you can get this exception if one of your foreign key relationships is incorrect.
I am using cxf:jaxws-client in Mule 3 and the response I get from my web service call is of type ReleasingInputStream. I have tried adding the http-response-to-message-transformer, but that generates an error - does anyone know how I can retrieve the response as an object as opposed to a ReleasingInputStream?
Many thanks.
To solve the issue put the <cxf-client> inside the <outbound-endpoint> section (NOT BEFORE IT), by modifying the following code
<cxf:jaxws-client
clientClass="com.xyz.services.WSServices"
port="WSServicesSoap"
wsdlLocation="classpath:wsdl-file.wsdl"
operation="GimmeDataOperation" />
<outbound-endpoint exchange-pattern="request-response" address="http://localhost:8083/OutboundService" />
which produces a ReleasingInputStream output to
<outbound-endpoint exchange-pattern="request-response" address="http://localhost:8083/OutboundService" >
<cxf:jaxws-client
clientClass="com.xyz.services.WSServices"
port="WSServicesSoap"
wsdlLocation="classpath:wsdl-file.wsdl"
operation="GimmeDataOperation" />
</outbound-endpoint>
that returns the expected object.
I was just having this same problem. I solved it by adding an ObjectToString transformer to the response side of the outbound endpoint like this:
<mule>
<object-to-string-transformer name="ObjectToString"/>
<flow>
...
...
...
<cxf:jaxws-client clientClass="com.my.ClientClass"
port="MyPort"
wsdlLocation="classpath:MyWsdl.wsdl"
operation="MyOperation" />
<outbound-endpoint address="http://some.address/path/to/service"
exchange-pattern="request-response"
responseTransformer-refs="ObjectToString" />
...
...
...
</flow>
</mule>
The whole point of jaxws-client is to receive the unmarshaled Java object, so getting the WS response as a String or ReleasingInputStream should not even be an option.
To make the <cxf:jaxws-client> "work" as one would expect the WS client to work - put the INSIDE of the <outbound-endpoint> you will be getting a correct Java object as a payload.