I have Input as below
[{Name=ABC, ID=123},{Name=XYZ, ID=345}]
I would iterate over this collection in a for-each loop and add one more element Age to each object.
My expected output would be like
[{Name=ABC, ID=123, Age=23},{Name=XYZ, ID=345, Age=24}]
Any help would be highly appreciated. Thanks in advance.
HTH..
%dw 2.0
output application/json
var inp = [
{
"Name":"ABC",
"ID":"123"
},
{
"Name":"XYZ",
"ID":"345"
}
]
---
inp map {
($),
age: (23 + ($$)) // or your logic to derive age
}
It was not possible with DW as because payload inside for-each was modified. So I tried using Expression component. Below is my configuration XML.
<flow name="add-one-more-element-to-the-original-payload">
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="30" timeUnit="SECONDS"/>
<logger message="Pooling Started" level="INFO" doc:name="Log"/>
</poll>
<dw:transform-message doc:name="Transform Payload">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
%var collection = [{"Name":"ABC","ID":123},{"Name":"XYZ","ID":345}]
---
collection]]></dw:set-payload>
</dw:transform-message>
<set-variable variableName="outputList" value="#[new java.util.ArrayList()]"
doc:name="Set Variable"/>
<foreach doc:name="For Each" collection="#[payload]">
<dw:transform-message doc:name="CurrentPayload">
<dw:set-variable variableName="currentPayload"><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-variable>
</dw:transform-message>
<flow-ref name="get-age-subflow" doc:name="Get Age"/>
<expression-component doc:name="Expression"><!
[CDATA[flowVars.currentPayload.Age=payload.Age;]]></expression-component>
<expression-transformer expression="#
[flowVars.outputList.add(flowVars.currentPayload)]" doc:name="Expression"/>
</foreach>
<logger message="#[flowVars.outputList]" level="INFO" doc:name="Logger"/>
</flow>
<sub-flow name="get-age-subflow">
<dw:transform-message doc:name="Transform Age">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Age:24
}]]></dw:set-payload>
</dw:transform-message>
</sub-flow>
Facing below runtime error after upgrading from Mule Runtime 4.2.0 to 4.2.1.
Below detailed my codebase, and quick error message, and detailed error logs for review. Please suggest, if anyone faced the same problem?
I referred to other similar question and answer in StackOverflow Mule ESB : Cannot copy message with a stream payload, this doesn't solve mine below error.
Runitime Error on Mule 4.2.1:
Cannot copy message with a stream payload. Payload can be transformed by using an <object-to-byte-array-transformer> in order to be able to copy the message. Payload type is "org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider"
Detailed Error Log
INFO 2019-08-05 11:29:15,388 [[MuleRuntime].cpuLight.11: [jda-demand-adapter].Get_BY_Status_n_Update_Part_Flow.CPU_LITE #7ed56bee] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: BY status: <?xml version='1.0' encoding='UTF-8'?>
<DeliveryStatus>
<DeliveryID>46658</DeliveryID>
<Username>user_di_test</Username>
<StartDelivery>2019-08-05T05:24:34.886502+00:00</StartDelivery>
<LastProcessed>2019-08-05T05:46:16.508861+00:00</LastProcessed>
<Status>LOADED</Status>
<ParameterInfo>
<Category>Events</Category>
<Version>1.14.17</Version>
<File>Events.gz</File>
</ParameterInfo>
<Statistics>
<DeliveredRecords>323</DeliveredRecords>
<PendingRecords>323</PendingRecords>
<ProcessedRecords>0</ProcessedRecords>
<ErroneousRecords>0</ErroneousRecords>
</Statistics>
</DeliveryStatus>
INFO 2019-08-05 11:29:15,397 [[MuleRuntime].cpuIntensive.09: [jda-demand-adapter].Get_BY_Status_n_Update_Part_Flow.CPU_INTENSIVE #6b417a92] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: payload : {
"DeliveryStatus": {
"DeliveryID": "46658",
"Username": "user_di_test",
"StartDelivery": "2019-08-05T05:24:34.886502+00:00",
"LastProcessed": "2019-08-05T05:46:16.508861+00:00",
"Status": "LOADED",
"ParameterInfo": {
"Category": "Events",
"Version": "1.14.17",
"File": "Events.gz"
},
"Statistics": {
"DeliveredRecords": "323",
"PendingRecords": "323",
"ProcessedRecords": "0",
"ErroneousRecords": "0"
}
}
}
INFO 2019-08-05 11:29:15,410 [[MuleRuntime].cpuIntensive.09: [jda-demand-adapter].Get_BY_Status_n_Update_Part_Flow.CPU_INTENSIVE #6b417a92] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Delivery Status summary: "46658","LOADED", {
"DeliveredRecords": "323",
"PendingRecords": "323",
"ProcessedRecords": "0",
"ErroneousRecords": "0"
}
INFO 2019-08-05 11:29:17,030 [[MuleRuntime].io.110: [jda-demand-adapter].Update_Part_Status_Recordcount_Flow.BLOCKING #502d3ddf] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Final response for Job without Errors, jobDetails: null
INFO 2019-08-05 11:29:17,032 [[MuleRuntime].io.110: [jda-demand-adapter].Update_Part_Status_Recordcount_Flow.BLOCKING #502d3ddf] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Inside Aggregation complete - For loop payload:[org.mule.runtime.api.metadata.TypedValue#fc4ba33d]
INFO 2019-08-05 11:29:17,052 [[MuleRuntime].io.110: [jda-demand-adapter].Update_Part_Status_Recordcount_Flow.BLOCKING #502d3ddf] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: After Reporting Transformation: {
"Summary": {
"DataPostingStatus": "",
"DataPostingDetailedStatus": "",
"JobCreatedDate": "",
"JobRestartDate": "",
"JobLastModifiedTime": "",
"JobExecutionDuration": "",
"JobExecutionDuration_in_Secs": "",
"RecordsProcessed": "",
"DeliveredRecords": "323",
"PendingRecords": "323",
"ProcessedRecords": "0",
"ErrorRecords": "0",
"Categories": [
"Events"
]
},
"CategorySummary": [
{
"Category": "Events",
"DeliveredRecords": "323",
"PendingRecords": "323",
"ProcessedRecords": "0",
"ErrorRecords": "0",
"DeliveryId": [
"46658"
]
}
],
"Errors": {
},
"Details": [
{
"DeliveryStatus": {
"DeliveryID": "46658",
"Username": "user_di_test",
"StartDelivery": "2019-08-05T05:24:34.886502+00:00",
"LastProcessed": "2019-08-05T05:46:16.508861+00:00",
"Status": "LOADED",
"ParameterInfo": {
"Category": "Events",
"Version": "1.14.17",
"File": "Events.gz"
},
"Statistics": {
"DeliveredRecords": "323",
"PendingRecords": "323",
"ProcessedRecords": "0",
"ErroneousRecords": "0"
}
}
}
]
}
ERROR 2019-08-05 11:29:17,076 [[MuleRuntime].cpuLight.11: [jda-demand-adapter].Ingestion_Status_Main_Flow.CPU_LITE #7132a277] [event: 20e0d4f1-b746-11e9-afb2-0a0027000005] org.mule.runtime.core.internal.exception.OnErrorContinueHandler:
********************************************************************************
Message : Cannot copy message with a stream payload. Payload can be transformed by using an <object-to-byte-array-transformer> in order to be able to copy the message. Payload type is "org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider".
Element : Get_Job_Status_Details_Flow/processors/1 # jda-demand-adapter:com/jda/cd/common/flow/ingestion-status.xml:257 (Scatter-Gather)
Element XML : <scatter-gather doc:name="Scatter-Gather">
<route>
<logger level="INFO" doc:name="Logger" message="Calculating the Job Satus ..."></logger>
<flow-ref name="GetDetailedJobStatus_SubFlow" doc:name="GetDetailedJobStatus_SubFlow"></flow-ref>
<set-payload value="#[{
"detailedStatus": vars.detailedStatus
}]" mimeType="application/java" doc:name="Set Payload" doc:id="5ea3c7bf-b1b1-4231-9af3-c8e39f241866"></set-payload>
</route>
<route>
<flow-ref name="Get_Job_Details_Flow_Enricher_0" doc:name="Get_Job_Details_Flow_Enricher_0"></flow-ref>
<set-payload value="#[{
"jobStatus": vars.jobStatus
}]" mimeType="application/java" doc:name="Set Payload" doc:id="e0a78803-a93c-4523-9551-4cf9218f059b"></set-payload>
<logger level="INFO" doc:name="Logger Job Details" doc:id="8b31e869-5630-4e01-902d-3e259ba2734e" message="Scatter-Gather Get_Job_Details_Flow_Enricher_0 payload:#[payload], jobStatus:#[vars.jobStatus]"></logger>
<choice doc:name="CalcJobStatus_IfNeeded-Choice">
<when expression="#[vars.jobStatus == 'IN_PROGRESS']">
<async doc:name="Async">
<flow-ref name="CalcBatchStatus_Flow" doc:name="CalcBatchStatus_Flow"></flow-ref>
</async>
</when>
<otherwise>
<logger message="job status is final" level="INFO" doc:name="Logger"></logger>
</otherwise>
</choice>
</route>
<route>
<flow-ref name="Get_Records_By_Status_SubFlow_Enricher_1" doc:name="Get_Records_By_Status_SubFlow_Enricher_1"></flow-ref>
<set-payload value="#[{
"recordsByStatus": vars.recordsByStatus
}]" mimeType="application/java" doc:name="Set Payload"></set-payload>
</route>
<route>
<logger message="#["jobExecution Duration : "]" level="INFO" doc:name="Logger"></logger>
<flow-ref name="Get_Job_Status_Details_Flow_Enricher_2" doc:name="Get_Job_Status_Details_Flow_Enricher_2"></flow-ref>
<logger message="payload last_mod_date #[vars] #[vars.lastModDate]" level="INFO" doc:name="Logger"></logger>
<flow-ref name="Get_Job_Status_Details_Flow_Enricher_6" target="jobCreatedDate" targetValue="#[payload[0].CREATED_DATE]" doc:name="Get_Job_Status_Details_Flow_Enricher_6"></flow-ref>
<set-variable value="#[now()]" doc:name="Current time" doc:id="547a11d2-b9dc-479e-a0c4-36e5b367bcee" variableName="currentTime"></set-variable>
<set-variable value="#[%dw 2.0 import * from dw::util::Timer output application/java --- ((toMilliseconds(vars.lastModDate) - toMilliseconds(vars.jobCreatedDate))/1000) as Number as String {format: ".##"}]" doc:name="Set Variable jobExecutionDurationSeconds" doc:id="499b7eda-f565-4a7c-94bd-9aeaf7306941" variableName="jobExecutionDurationSeconds"></set-variable>
<set-variable value="#[%dw 2.0 import * from dw::util::Timer output application/java --- ((toMilliseconds(vars.currentTime) - toMilliseconds(vars.lastModDate))/1000) as Number as String {format: ".##"}]" doc:name="TimeSinceLastMod" doc:id="0f30996f-1c89-4d45-90e9-915f3d2440bb" variableName="timeSinceLastMod"></set-variable>
<set-variable value="#[%dw 2.0 output application/json --- (((vars.jobExecutionDurationSeconds / 3600) as Number) as String {format: ".##"}) ++ ":" ++ ((((vars.jobExecutionDurationSeconds mod 3600) as Number) / 60 as Number) as String)as String {format: ".##"} ++ ":" ++((((vars.jobExecutionDurationSeconds mod 60) as Number))as String) as String {format: ".##"}]" doc:name="Set Variable jobExecutionDuration" doc:id="eaa22fce-6085-4290-8a31-46fc0bdd90c0" variableName="jobExecutionDuration"></set-variable>
<set-variable value="#[%dw 2.0 output application/json --- (((vars.timeSinceLastMod / 3600) as Number) as String) as String {format: ".##"} ++ ":" ++ ((((vars.timeSinceLastMod mod 3600) as Number) / 60 as Number) as String) as String {format: ".##"} ++ ":" ++((((vars.timeSinceLastMod mod 60) as Number))as String) as String {format: ".##"}]" doc:name="Set Variable TimeSinceLastModString" doc:id="15942ccf-d219-40f1-a783-3e4466bdfbbf" variableName="timeSinceLastModString"></set-variable>
<logger message="job_create date #[vars]" level="INFO" doc:name="Logger"></logger>
<set-payload value="#[%dw 2.0 output application/json --- {"jobExecutionDuration": vars.jobExecutionDuration, "jobExecutionDurationSeconds": vars.jobExecutionDurationSeconds, "jobCreatedDate":vars.jobCreatedDate,"lastModDate": vars.lastModDate,"currentTime": vars.currentTime,"timeSinceLastMod": vars.timeSinceLastMod}]" doc:name="Set Payload" doc:id="a97e5020-9d64-411e-87bf-ab325f29cd7f" mimeType="application/json"></set-payload>
</route>
</scatter-gather>
Error type : MULE:UNKNOWN
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.runtime.api.exception.MuleRuntimeException: Cannot copy message with a stream payload. Payload can be transformed by using an <object-to-byte-array-transformer> in order to be able to copy the message. Payload type is "org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider"
********************************************************************************
Below Codebase works fine in Mule 4.2.0, after upgrading Mule runtime to 4.2.1 and trying to use Anypoint 7.3.4, getting above runtime error. I am not getting the clue to fix it.
Below same code works on Mule 4.2.0:
<flow name="Ingestion_Status_Main_Flow" doc:description="Ingestion_Status_Main_Flow will read BY_delivery_IDs and try to read the status of the delivery from BY. If BY service is not available, then it provides the status of the status tables. In addition to the BY response details, it shows the job's current status, Number of records as per the status. ">
<http:listener path="${secure::ingestion.status.resource}/{jobId}" allowedMethods="GET" doc:name="/status" config-ref="HTTP_Listener_Config">
<http:response statusCode="#[migration::HttpListener::httpListenerResponseSuccessStatusCode(vars)]"/>
<http:error-response statusCode="#[vars.statusCode default migration::HttpListener::httpListenerResponseErrorStatusCode(vars)]"/>
</http:listener>
<logger message='#[attributes.uriParams.jobId]' level="INFO" doc:name="Job ID from input" />
<set-variable variableName="jobId" value="#[attributes.uriParams.jobId]" doc:name="jobId" />
<set-variable value="#[attributes.queryParams.notify]" doc:name="notifyFlag" doc:id="c2c8a869-4941-4f72-9cd3-3c9ea7441e65" variableName="notify"/>
<flow-ref name="Validate_Input_JobID_Sub_Flow" doc:name="Validate_Input_JobID_Sub_Flow" />
<set-variable variableName="correlationId" value="#[vars.jobId]" doc:name="Session Variable"/>
<flow-ref name="Fetch_Parts_for_Job_SubFlow" doc:name="Fetch_Parts_for_Job_SubFlow" />
<set-variable value="#[sizeOf(payload)]" doc:name="sizeOfPayload" doc:id="0ab10eae-0b50-4b37-bd89-0ac614f548db" variableName="sizeOfPayload" />
<logger message='Delivery ID for Job: #[payload.BY_DELIVERY_ID]' level="INFO" doc:name="Deliver IDs" />
<choice doc:name="Choice">
<when expression="#[sizeOf(payload) > 0]">
<foreach doc:name="For Each" doc:id="742d1496-9be5-409a-a42c-e0da8a1a696d" >
<logger level="INFO" doc:name="Logger For Each" doc:id="e9aeff5c-e00c-4fd4-ada3-09b22f4161cd" message="inside for each #[payload] "/>
<flow-ref doc:name="Get_BY_Status_n_Update_Part_Flow" doc:id="6e00a6b0-8d83-4df6-b22a-ce6666fed36a" name="Get_BY_Status_n_Update_Part_Flow"/>
<aggregators:group-based-aggregator doc:name="groupByDeliveryId" doc:id="b4b76fac-3f35-4812-9c1d-81b23ddd1eb9" name="groupByDeliveryId" groupSize="#[vars.sizeOfPayload]" >
<aggregators:incremental-aggregation >
<logger level="INFO" doc:name="Logger" doc:id="f3e2bdd1-d660-4074-92b0-4c04ea421706" message="Incremental size #[sizeOf(payload)]" />
</aggregators:incremental-aggregation>
<aggregators:aggregation-complete >
<logger message="Final response for Job without Errors, jobDetails: #[vars.detailedStatus]" level="INFO" doc:name="Response without Errors included" />
<logger level="INFO" doc:name="Logger" doc:id="eb7c7626-7091-481a-939c-926f938d84ae" message="Inside Aggregation complete - For loop payload:#[payload]" />
<ee:transform doc:name="Reporting Transformation" doc:id="421fb819-c58f-4f46-8d6d-e5dfa43df133">
<ee:message>
<ee:set-payload resource="ingestion\ingestion-status-resp.dwl" />
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="f6acf89c-5b84-4cbc-9cab-bf4b9e48da4f" message="After Reporting Transformation: #[payload]"/>
<set-variable variableName="aggregatedPayload" value="#[payload]" doc:name="Set Variable" doc:id="eded53f3-18ac-4a84-8fcf-916e89d7a185"/>
</aggregators:aggregation-complete>
</aggregators:group-based-aggregator>
</foreach>
</when>
<otherwise>
<logger message="No Delivery IDs found." level="INFO" doc:name="Logger" />
<set-payload value="#[{
"Summary": {
"DataPostingStatus": '',
"DataPostingDetailedStatus": '',
"JobCreatedDate": '',
"JobRestartDate": '',
"JobLastModifiedTime": '',
"JobExecutionDuration": '',
"JobExecutionDuration_in_Secs": '',
"RecordsProcessed": ''
}
}]" mimeType="application/json" doc:name="Set Payload" doc:id="066c7871-dd4b-4bde-8663-e0558a5847df"/>
</otherwise>
</choice>
<set-payload value="#[vars.aggregatedPayload]" doc:name="Set Payload" doc:id="337a0771-458c-4511-920c-ca38fcb8c6d9" />
<logger message='getting db details #[payload]' level="INFO" doc:name="Logger"/>
<flow-ref name="Ingestion_Status_Main_Flow_Enricher_22" doc:name="Ingestion_Status_Main_Flow_Enricher_22"/>
<set-payload value="#[vars.aggregatedPayload]" doc:name="Set Payload" doc:id="7ef033ba-17d6-4597-9b4c-96fb73212e63" />
<ee:transform doc:name="Transform Message" doc:id="794cfad2-e368-4b44-9c7e-746e0e692338" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
Summary: {
DataPostingStatus: vars.jobStatus,
DataPostingDetailedStatus: vars.detailedStatus,
JobCreatedDate: vars.jobCreatedDate,
JobRestartDate: "",
JobLastModifiedTime: vars.lastModDate,
JobExecutionDuration: vars.jobExecutionDuration,
JobExecutionDuration_in_Secs: vars.jobExecutionDurationSeconds,
RecordsProcessed: "",
DeliveredRecords: payload.Summary.DeliveredRecords,
PendingRecords: payload.Summary.PendingRecords,
ProcessedRecords: payload.Summary.ProcessedRecords,
ErrorRecords: payload.Summary.ErrorRecords,
Categories: payload.Summary.Categories,
},
Errors:{},
CategorySummary: payload.CategorySummary,
Details: payload.Details
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger message='Getting errors details...' level="INFO" doc:name="Logger"/>
<flow-ref name="Add_Errors_to_Payload_Sub_Flow_Enricher_0" doc:name="Add_Errors_to_Payload_Sub_Flow_Enricher_0"/>
<ee:transform doc:name="Transform Message" doc:id="5b0518ac-139a-42f8-ae12-a42cce2449c1" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload
]]></ee:set-payload>
</ee:message>
</ee:transform>
<async doc:name="Async">
<choice doc:name="Choice">
<when expression="#[vars.notify == 'true']">
<flow-ref name="Send_Status_Mail_Flow" doc:name="Send_Status_Mail_Flow" />
</when>
<otherwise>
<logger message="Notification flag is not provided as query parameter." level="INFO" doc:name="Logger" />
</otherwise>
</choice>
</async>
<logger message='Final response: #[payload] jobId: #[vars.jobId]' level="DEBUG" doc:name="Response with Errors" />
<logger message="Ingestion status request done" level="INFO" doc:name="Status request completed" />
<error-handler ref="Global_Errorflow_Choice_Exception_Strategy" doc:name="Global Exception Strategy" />
</flow>
<sub-flow name="Ingestion_Status_Main_Flow_Enricher_22">
<flow-ref name="Get_Job_Status_Details_Flow" doc:description="Job status retrieved from status tables."/>
</sub-flow>
</flow>
<sub-flow name="Get_Job_Status_Details_Flow" doc:description="Job status retrieved from status tables.
For now, different stages of the job processing has been reported.
1. Batches not yet created.
2. Batches created, and parts are getting creating.
3. Parts are created, and processing.
4. Some part uploads are failed.
5. Some parts processing is failed i.e. payload is not created. ">
<scatter-gather doc:name="Scatter-Gather">
<route>
<logger level="INFO" doc:name="Logger" message="Calculating the Job Satus ..." />
<flow-ref name="GetDetailedJobStatus_SubFlow" doc:name="GetDetailedJobStatus_SubFlow" />
<set-payload value="#[{
"detailedStatus": vars.detailedStatus
}]" mimeType="application/java" doc:name="Set Payload" doc:id="5ea3c7bf-b1b1-4231-9af3-c8e39f241866"/>
</route>
<route>
<flow-ref name="Get_Job_Details_Flow_Enricher_0" doc:name="Get_Job_Details_Flow_Enricher_0"/>
<set-payload value="#[{
"jobStatus": vars.jobStatus
}]" mimeType="application/java" doc:name="Set Payload" doc:id="e0a78803-a93c-4523-9551-4cf9218f059b"/>
<logger level="INFO" doc:name="Logger Job Details" doc:id="8b31e869-5630-4e01-902d-3e259ba2734e" message="Scatter-Gather Get_Job_Details_Flow_Enricher_0 payload:#[payload], jobStatus:#[vars.jobStatus]"/>
<choice doc:name="CalcJobStatus_IfNeeded-Choice">
<when expression="#[vars.jobStatus == 'IN_PROGRESS']">
<async doc:name="Async">
<flow-ref name="CalcBatchStatus_Flow" doc:name="CalcBatchStatus_Flow" />
</async>
</when>
<otherwise>
<logger message="job status is final" level="INFO" doc:name="Logger" />
</otherwise>
</choice>
</route>
<route>
<flow-ref name="Get_Records_By_Status_SubFlow_Enricher_1" doc:name="Get_Records_By_Status_SubFlow_Enricher_1"/>
<set-payload value="#[{
"recordsByStatus": vars.recordsByStatus
}]" mimeType="application/java" doc:name="Set Payload" />
</route>
<route>
<logger message='#["jobExecution Duration : "]' level="INFO" doc:name="Logger" />
<flow-ref name="Get_Job_Status_Details_Flow_Enricher_2" doc:name="Get_Job_Status_Details_Flow_Enricher_2"/>
<logger message='payload last_mod_date #[vars] #[vars.lastModDate]' level="INFO" doc:name="Logger" />
<flow-ref name="Get_Job_Status_Details_Flow_Enricher_6" target="jobCreatedDate" targetValue="#[payload[0].CREATED_DATE]" doc:name="Get_Job_Status_Details_Flow_Enricher_6"/>
<set-variable value="#[now()]" doc:name="Current time" doc:id="547a11d2-b9dc-479e-a0c4-36e5b367bcee" variableName="currentTime"/>
<set-variable value='#[%dw 2.0
import * from dw::util::Timer
output application/java
---
((toMilliseconds(vars.lastModDate) - toMilliseconds(vars.jobCreatedDate))/1000) as Number as String {format: ".##"}]' doc:name="Set Variable jobExecutionDurationSeconds" doc:id="499b7eda-f565-4a7c-94bd-9aeaf7306941" variableName="jobExecutionDurationSeconds"/>
<set-variable value='#[%dw 2.0
import * from dw::util::Timer
output application/java
---
((toMilliseconds(vars.currentTime) - toMilliseconds(vars.lastModDate))/1000) as Number as String {format: ".##"}]' doc:name="TimeSinceLastMod" doc:id="0f30996f-1c89-4d45-90e9-915f3d2440bb" variableName="timeSinceLastMod"/>
<set-variable value='#[%dw 2.0
output application/json
---
(((vars.jobExecutionDurationSeconds / 3600) as Number) as String {format: ".##"}) ++ ":" ++ ((((vars.jobExecutionDurationSeconds mod 3600) as Number) / 60 as Number) as String)as String {format: ".##"} ++ ":" ++((((vars.jobExecutionDurationSeconds mod 60) as Number))as String) as String {format: ".##"}]' doc:name="Set Variable jobExecutionDuration" doc:id="eaa22fce-6085-4290-8a31-46fc0bdd90c0" variableName="jobExecutionDuration"/>
<set-variable value='#[%dw 2.0
output application/json
---
(((vars.timeSinceLastMod / 3600) as Number) as String) as String {format: ".##"} ++ ":" ++ ((((vars.timeSinceLastMod mod 3600) as Number) / 60 as Number) as String) as String {format: ".##"} ++ ":" ++((((vars.timeSinceLastMod mod 60) as Number))as String) as String {format: ".##"}]' doc:name="Set Variable TimeSinceLastModString" doc:id="15942ccf-d219-40f1-a783-3e4466bdfbbf" variableName="timeSinceLastModString"/>
<logger message='job_create date #[vars]' level="INFO" doc:name="Logger" />
<set-payload value='#[%dw 2.0 output application/json --- {"jobExecutionDuration": vars.jobExecutionDuration, "jobExecutionDurationSeconds": vars.jobExecutionDurationSeconds, "jobCreatedDate":vars.jobCreatedDate,"lastModDate": vars.lastModDate,"currentTime": vars.currentTime,"timeSinceLastMod": vars.timeSinceLastMod}]' doc:name="Set Payload" doc:id="a97e5020-9d64-411e-87bf-ab325f29cd7f" mimeType="application/json"/>
</route>
</scatter-gather>
<logger message="Flowvars used to invoke the restart-flows: payload: #[payload]" level="INFO" doc:name="RestartFlags - Logger" />
</sub-flow>
I have a csv file which looks like this :
I am iterating the CSV file using For each after converting it to Java ArrayList using Dataweave.
Now, I need to convert one of the elements which is Date from String to Oracle Timestamp. Please help with your thoughts. The source code looks like this :
<flow name="testFlow">
<file:inbound-endpoint path="src/main/resources/input"
moveToDirectory="src/main/resources/output" connector-ref="File"
responseTimeout="10000" doc:name="File"/>
<dw:transform-message metadata:id="4e33c7ff-c33b-4c78-be34-79a154aa16df"
doc:name="Transform Message">
<dw:input-payload doc:sample="sample_data\list_csv.csv"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
</dw:transform-message>
<logger level="INFO" doc:name="Logger"/>
<foreach collection="#[payload]" doc:name="For Each">
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
Flow design looks like this :
Basically what you need to do is to convert the date field which is a string in csv to date format. To do it just provide the format of the incoming data field and it will work for the below input example(csv is comma separated) :-
foo,bar,Name,Date
foo1,baar1,Name1,15-01-2016 12:08
foo2,baar2,Name2,15-01-2016 12:08
you can try below dw :-
%dw 1.0
%output application/java
---
payload map {
foo : $.foo,
bar : $.bar,
Name : $.Name,
Date : $.Date as :date {format: "dd-MM-yyyy hh:mm" }
}
I am practicing on salesforce to mulesoft connection. I am trying to query the account from salesforce, do some logic using Invoke Component and update the Account name in Salesforce. Please find the XML code below. I get the following error when i invoke..
Execution of the expression "Variable:Fname=__object_for_enrichment" failed. (org.mule.api.expression.ExpressionRuntimeException).
XML:
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8092" doc:name="HTTP Listener Configuration"/>
<sfdc:config name="Salesforce__Basic_Authentication" username="username" password="*******" securityToken="Token" doc:name="Salesforce: Basic Authentication" url="https://login.salesforce.com/services/Soap/u/38.0">
<reconnect-forever/>
</sfdc:config>
<spring:beans>
<spring:bean id="Bean" name="UpdateAcc" class="com.pack.salesforceconnect.SFJava"/>
</spring:beans>
<flow name="salesforceconnectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/salesforce" doc:name="HTTP"/>
<sfdc:query-single config-ref="Salesforce__Basic_Authentication" query="dsql:SELECT Id,Name,ShippingCity FROM Account ORDER BY Name DESC" doc:name="Salesforce"/>
<dw:transform-message doc:name="Transform Message">
<dw:input-payload doc:sample="sample_data\Account.dwl"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Id: payload.Id,
Name: payload.Name,
ShippingCity: payload.ShippingCity
}]]></dw:set-payload>
</dw:transform-message>
<enricher source="#[payload.Name]" target="#[Variable:Fname]" doc:name="Message Enricher">
<invoke name="Invoke" object-ref="UpdateAcc" method="ChangeSCity" methodArguments="#[payload.Name]" metadata:id="id"/>
</enricher>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
[{
Id: payload.Id,
Name: variable:FName,
ShippingCity: payload.ShippingCity
}]]]></dw:set-payload>
</dw:transform-message>
<set-payload value="#[payload]" doc:name="Set Payload"/>
</flow>
</mule>
Class:
package com.pack.salesforceconnect;
public class SFJava {
public String ChangeSCity(String Fname){
//String ShippingCity;
System.out.print("Account Name received : "+Fname);
if (Fname == null){
Fname = "New Account";
}else{
Fname = "Washington Industries Inc.,";
}
System.out.print("Account Name: "+Fname);
return Fname;
}
}
Syntax wrong .. it would be target="#[variable:Fname]" instead of target="#[Variable:Fname]"
Or simply you can use target="#[flowVars.Fname]"
I have to concatenate randomly generated number with the field from request in dataweave.
NUMBR: "AA" ++ $.Load.Reference.*Reference ++ RandomNumber
How to achieve this in Mule Dataweave
Not sure what you can do in Datawevae to do this, but you can you set a Random number in a flowVariable and invoke it from your Dataweave script like so:
<set-variable variableName="random"
value="#[new java.util.Random().nextInt(100)]" doc:name="Variable" />
<dw:transform-message doc:name="Transform Message">
<dw:input-variable doc:sample="unknown.dwl" variableName="random" />
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/dw
---
{
"data": ("22" as :number + flowVars.random)
} ]]>
</dw:set-payload>
</dw:transform-message>
You can also use Expression Component to assign it to Payload or variables and then concatenate
<flow name="random-numbersFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/random" allowedMethods="GET" doc:name="HTTP"/>
<expression-component doc:name="Expression"><![CDATA[payload = new java.util.Random().nextInt(100)]]></expression-component>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
data: payload
}]]></dw:set-payload>
</dw:transform-message>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
You can't do this in pure DataWeave but you could use two methods to generate the number elsewhere in the application:
You could call a global MEL function from DataWeave: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#global-mel-functions
You could call a flow that returns the value: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation#expressions-that-call-external-flows
Just set a random value generated by Java into a Flow Variable
<set-variable variableName="Random_Variable" value="#[java.util.Random().nextInt(10)]" doc:name="Random Variable"/>
Then Use that Flow Variable in your Dataweave Transform.
<dw:transform-message doc:name="Transform Message" metadata:id="8098b24c-30c1-4e9e-a3ce-9e8aaaec7bd1">
<dw:input-variable mimeType="application/java" variableName="Random_Variable"/>
<dw:set-payload><![CDATA[%dw 2.0
%output application/json
---
{
NUMBR: "AA" ++ $.Load.Reference.*Reference ++ flowVars.Random_Variable
}]]></dw:set-payload>
</dw:transform-message>
In Mule 4 Dataweave 2 function random()
Returns a pseudo-random number greater than or equal to 0.0 and less than 1.0
MULE 4 DOC:
https://docs.mulesoft.com/dataweave/2.3/dw-core-functions-random
Example:
%dw 2.0
output application/json
{ price: random() * 1000 }