Skip to main content

Hello,

 

i'm fetching the alert email template from DB and Message_Template column will have multiple and different properties placeholders and how will i replace them with the values in a variable emailPropertiesValues using DWL. 

 

In a nutshell i have to replace all the available property placeholders in Muessage_Template with their correspoinding from the emailPropertiesValues key values. Keys can be dynamic and need to be same for other request.

 

Input (DB response):

{  "Application_Name": "Test Applicatioin",  "Condition_Priority": "ERROR",  "Message_Template": "FAILURE\n This is a notification email to inform you about failure in the ${cloudhubEnvironment}  ${appName} with transaction ${transactionId} \n\nHello,\nYou are receiving this alert because:\nThe ${appName} application has a new ${priority} notification with a failure message.\n\n------------------\nFAILURE TEXT\n------------------\n\nError Type: ${errorType}\n\n--------------\nCONTEXT\n--------------\n\nServer : CloudHub\nTime Stamp: ${timeStamp}\n. ${descriptionText}",  "Subject": "Mule ERROR",  "Created_At": "2023-11-20T10:21:26.93",  "Region": "ps-vr2",  "Source": "Applications"}

emailPropertiesValues variable and its values:

{  "transactionId": "cbd0d3049ae8-23-safd-23",  "appName": "demo-qa-app",  "descriptionText": "descriptionText",  "errorType": "errorType",  "alertType": "alertType",  "timeStamp": "2023-11-06T09:31:32.879+05:30",  "cloudhubEnvironment": "dev-ps"  "priority": "ERROR"}

 

expected output:

{  "Application_Name": "Test Applicatioin",  "Condition_Priority": "ERROR",  "Message_Template": "FAILURE\n This is a notification email to inform you about failure in the dev-ps  demo-qa-app with transaction cbd0d3049ae8-23-safd-23 \n\nHello,\nYou are receiving this alert because:\nThe demo-qa-app application has a new ERROR notification with a failure message.\n\n------------------\nFAILURE TEXT\n------------------\n\nError Type: errorType\n\n--------------\nCONTEXT\n--------------\n\nServer : CloudHub\nTime Stamp: 2023-11-06T09:31:32.879+05:30\n. descriptionText",  "Subject": "Mule ERROR",  "Created_At": "2023-11-20T10:21:26.93",  "Region": "ps-vr2",  "Source": "Applications"}

Appreciate your help on this.

 

Thanks

 

 

 

#Mule 4

11 respostas
  1. 12 de dez. de 2023, 14:28

    I have removed everything from your code base and added only one transformation and it will work. Please make further changes according to your requirements.

     

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

     

    <mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"

    xmlns:eilog="http://www.mulesoft.org/schema/mule/eilog"

    xmlns:kafka="http://www.mulesoft.org/schema/mule/kafka" 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/kafka http://www.mulesoft.org/schema/mule/kafka/current/mule-kafka.xsd

    http://www.mulesoft.org/schema/mule/eilog http://www.mulesoft.org/schema/mule/eilog/current/mule-eilog.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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="1df86740-6b8a-42c8-b117-2a4227a3c108" >

    <http:listener-connection host="0.0.0.0" port="8081" />

    </http:listener-config>

    <flow name="main-flow" doc:id="de8c026d-5ae8-4d1e-872e-9ac99c836a03">

    <http:listener doc:name="Listener" doc:id="54fd0ac4-ed66-4494-acd0-af88140846a4" config-ref="HTTP_Listener_config" path="/email"/>

    <ee:transform doc:name="Transform Message" doc:id="eebf9bab-cc01-4186-8bce-965c3341b483" >

    <ee:message >

    <ee:set-payload ><![CDATA[

    %dw 2.0

    output application/json

    var dbselectoutput = readUrl("classpath://db-response.json","application/json")

    ---

    payload map ((item, index) ->

     

    dbselectoutput update {

    case .Message_Template -> namesOf(item.customProperties) reduce ((item1, accumulator=dbselectoutput.Message_Template) ->

    accumulator replace ("\$" ++ "{$(item1)}") with item.customProperties[item1])

    }

     

     

    )]]></ee:set-payload>

    </ee:message>

    </ee:transform>

    </flow>

    </mule>

     

0/9000