on 05-11-2022 5:14 PM
Has anyone been able to process multiple mail attachments in CPI? I'm able to get one attachment, and I've tried these approaches:
https://blogs.sap.com/2017/02/19/replacing-the-message-body-with-an-attachment/
https://answers.sap.com/questions/13316754/cpi-how-to-process-multiple-pdf-attachments.html
However it results in only capturing the last attachment. What I'm looking to do is process an email and send each attachment to a receiver such as SFTP.
Sending them in a zip archive and using a zip splitter works, but looking for a better solution.
Any help would be greatly appreciated!
Thanks
Hi Dan
Okay, here goes. The solution looks something like this:
The first script step (called "Prepare" in the above) copies the attachments into a property and then clears the attachments from the message. The Looping Process Call then calls the Local Integration Process called "Process one attachment" as long as there are attachments to process. Within the Local Integration Process, the script step picks the next attachment and replaces the message body with its contents. A Send step then sends that message to your SFTP server.
This is the code for the "Prepare" script step:
import com.sap.gateway.ip.core.customdev.util.Message
import javax.activation.DataHandler
def Message processData(Message message) {
// Create a new Map of attachments and store it in a property.
def attachments = new HashMap<String, DataHandler>(message.getAttachments())
message.setProperty('Attachments', attachments)
// Store the number of attachments in a property.
message.setProperty('AttachmentCount', attachments.size())
// Clear the attachments and attachment wrappers.
message.getAttachments().clear()
message.getAttachmentWrapperObjects().clear()
// All done.
return message
}
The Looping Process Call step must be configured as follows:
Finally, here is the code for the "Get next attachment" script step:
import com.sap.gateway.ip.core.customdev.util.Message
def Message processData(Message message) {
def attachments = message.getProperty('Attachments')
if (attachments.isEmpty()) {
// This should never happen!
throw new AssertionError('Empty attachments Map in property Attachments')
}
// Get the key of the next attachment to process.
def nextKey = attachments.keySet().iterator().next()
// Remove the attachment from the Map
def attachment = attachments.remove(nextKey)
// Replace the message body with the attachment's contents.
message.setBody(attachment.getContent())
// Update the AttachmentCount property.
message.setProperty('AttachmentCount', attachments.size())
// All done.
return message
}
Let me know how it works out!
Regards,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi 7a519509aed84a2c9e6f627841825b5a, I have a query that vice versa of this scenario, ie. Sending multiple SFTP files in a single mail receiver with multiple attachments. I have also raised a question about this. Send multiple attachment files in a single mail with multiple files from SFTP
Any idea on that?
Thanks.
HI Morton,
I have the same requirement as Dan, but I have to process only XML and all other Attachment shall be ignored.
this is my groovy script - could you please tell me what is wrong. Is I am getting java.lang.NoSuchMethodException: No signature of method: java.util.HashMap.getContentType() is applicable for argument types: () values: [] during run-time.
import com.sap.gateway.ip.core.customdev.util.Message
import javax.activation.DataHandler
def Message processData(Message message) {
// Create a new Map of attachments and store it in a property.
def attachments = new HashMap<String, DataHandler>(message.getAttachments())
if(attachments.getContentType().contains("xml")){
message.setProperty('Attachments', attachments)
// Store the number of attachments in a property.
message.setProperty('AttachmentCount', attachments.size())
}
// Clear the attachments and attachment wrappers.
message.getAttachments().clear()
message.getAttachmentWrapperObjects().clear()
// All done.
return message
}
thanks for you help.
Markus
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Morten,
I appreciate the response. No, I haven't been able to get it to work yet.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.