dataweavemule4

Getting error while sending Email subject with expression


I am getting error while sending email with customized subject with expression :

enter image description here

and getting below error :

while writing Java at 
3| {"DataGenerated" : (now()) as String { format: "dd MMM KK:mm a" } }
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.

3| {"DataGenerated" : (now()) as String { format: "dd MMM KK:mm a" } }
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
  at anonymous::main (line: 3, column: 20)" evaluating expression: "output application/json
---
{"DataGenerated" : (now()) as String { format: "dd MMM KK:mm a" } }".
Element               : mongodbconnectFlow/processors/1 @ datagenerator-api:datagenerator-api.xml:36
Element DSL           : <email:send doc:id="e89769cc-f214-44a7-a772-d4a6f626aada" config-ref="Email_SMTP" fromAddress="techsanko2020@gmail.com" subject="#[output application/json
---
{"DataGenerated" : (now()) as String { format: "dd MMM KK:mm a" } }]">

I want to get the subject as "DataGenerated": "07 Aug 01:43 PM"

Error :

Element               : mdbconnectFlow/processors/1 @ datagenerator-api:datagenerator-api.xml:40
Element DSL           : <email:send doc:id="e89769cc-f214-44a7-a772-d4a6f626aada" config-ref="Email_SMTP" fromAddress="**@gmail.com" subject="#[output application/json
---
{"DataGenerated" : (now() >> 'IST') as String { format: "dd MMM KK:mm a" } }]">
<email:to-addresses>
<email:to-address value="**@gmail.com"></email:to-address>
</email:to-addresses>
<email:cc-addresses>
<email:cc-address value="**@gmail.com"></email:cc-address>
</email:cc-addresses>
<email:body contentType="text/html"></email:body>
</email:send>
Error type            : MULE:EXPRESSION
FlowStack             : at mongodbconnectFlow(mongodbconnectFlow/processors/1 @ datagenerator-api:datagenerator-api.xml:40)

  (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

ERROR 2024-08-08 17:03:46,354 [[MuleRuntime].uber.07: [datagenerator-api].mongodbconnectFlow.BLOCKING @6d0ec794] [processor: ; event: 12e8ea40-557a-11ef-9f55-165afc45a79f] org.mule.runtime.core.privileged.exception.DefaultExceptionListener: 
********************************************************************************
Message               : "java.lang.IllegalStateException - No read or write handler for DataGenerated
java.lang.IllegalStateException: No read or write handler for DataGenerated
    at org.mule.weave.v2.module.pojo.reader.PropertyDefinition.resolveClassType(PropertyDefinition.scala:87)
    at org.mule.weave.v2.module.pojo.reader.PropertyDefinition.classType(PropertyDefinition.scala:114)
    at org.mule.weave.v2.module.pojo.writer.entry.BeanPropertyEntry.entryType(BeanPropertyEntry.scala:23)
    at org.mule.weave.v2.module.pojo.writer.WriterEntry.putValue(WriterEntry.scala:18)
    at org.mule.weave.v2.module.pojo.writer.WriterEntry.putValue$(WriterEntry.scala:11)
    at org.mule.weave.v2.module.pojo.writer.entry.BeanPropertyEntry.putValue(BeanPropertyEntry.scala:18)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.write(JavaWriter.scala:57)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.writeSimpleJavaValue(JavaWriter.scala:414)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.doWriteValue(JavaWriter.scala:265)
    at org.mule.weave.v2.module.writer.Writer.writeValue(Writer.scala:62)
    at org.mule.weave.v2.module.writer.Writer.writeValue$(Writer.scala:48)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.writeValue(JavaWriter.scala:39)
    at org.mule.weave.v2.module.writer.WriterWithAttributes.writeAttributesAndValue(WriterWithAttributes.scala:29)
    at org.mule.weave.v2.module.writer.WriterWithAttributes.writeAttributesAndValue$(WriterWithAttributes.scala:14)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.writeAttributesAndValue(JavaWriter.scala:39)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.doWriteValue(JavaWriter.scala:238)
    at org.mule.weave.v2.module.writer.Writer.writeValue(Writer.scala:62)
    at org.mule.weave.v2.module.writer.Writer.writeValue$(Writer.scala:48)
    at org.mule.weave.v2.module.pojo.writer.JavaWriter.writeValue(JavaWriter.scala:39)
    at org.mule.weave.v2.module.java.JavaInvocationHelper$.transformToJava(JavaInvokeFunction.scala:120)
    at org.mule.weave.v2.el.utils.DataTypeHelper$.transformToJava(DataTypeHelper.scala:168)
    at org.mule.weave.v2.el.utils.DataTypeHelper$.transformToJavaDataType(DataTypeHelper.scala:153)
    at org.mule.weave.v2.el.utils.DataTypeHelper$.toJavaValue(DataTypeHelper.scala:104)
    at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluate(WeaveExpressionLanguageSession.scala:255)
    at org.mule.weave.v2.el.WeaveExpressionLanguageSession.$anonfun$evaluate$4(WeaveExpressionLanguageSession.scala:137)
    at org.mule.weave.v2.el.WeaveExpressionLanguageSession.doEvaluate(WeaveExpressionLanguageSession.scala:270)
    at org.mule.runtime.core.privileged.util.attribute.ExpressionAttributeEvaluatorDelegate.resolve(ExpressionAttributeEvaluatorDelegate.java:56)
    at org.mule.runtime.core.privileged.util.AttributeEvaluator.resolveTypedValue(AttributeEvaluator.java:107)
    at org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionValueResolver.resolveTypedValue(ExpressionValueResolver.java:115)
    at org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionValueResolver.resolve(ExpressionValueResolver.java:99)
    at org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeValueResolverWrapper.lambda$initialise$0(TypeSafeValueResolverWrapper.java:64)
    at org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeValueResolverWrapper.resolve(TypeSafeValueResolverWrapper.java:52)
    at org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeExpressionValueResolver.resolve(TypeSafeExpressionValueResolver.java:66)
    at org.mule.runtime.module.extension.internal.runtime.resolver.ResolverUtils.resolveRecursively(ResolverUtils.java:99)
    at org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet.resolve(ResolverSet.java:128)
    at org.mule.runtime.module.extension.internal.runtime.tracing.TracedResolverSet.resolve(TracedResolverSet.java:40)
    at org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet.resolve(ResolverSet.java:109)
    at org.mule.runtime.module.extension.internal.runtime.operation.ComponentMessageProcessor.getResolutionResult(ComponentMessageProcessor.java:1160)
    at org.mule.runtime.module.extension.internal.runtime.operation.ComponentMessageProcessor.addContextToEvent(ComponentMessageProcessor.java:776)
    
    at java.lang.Thread.run(Thread.java:750)
, while writing Java at 
3| {"DataGenerated" : (now() >> 'IST') as String { format: "dd MMM KK:mm a" } }
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.

3| {"DataGenerated" : (now() >> 'IST') as String { format: "dd MMM KK:mm a" } }
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
  at anonymous::main (line: 3, column: 20)" evaluating expression: "output application/json
---
{"DataGenerated" : (now() >> 'IST') as String { format: "dd MMM KK:mm a" } }".

Solution

  • Outputting an object for the subject of an email makes no sense. Adding to that that the output format JSON will not give the expected output either. Remember that it would have curly braces around the object key-values.

    For a subject line you want to output a string. And for that using application/java is the most efficient way. Just concatenate the strings and escape the double quotes that are inside the string.

    %dw 2.0
    output application/java
    ---
    "\"DataGenerated\": \"" ++ (now() as String { format: "dd MMM KK:mm a" } ) ++ "\"" 
    

    Output:

    "DataGenerated": "08 Aug 11:23 AM"