cancel
Showing results for 
Search instead for 
Did you mean: 

processing Email using Inbound Email Adapter and Groovy Script

amit023
Explorer
0 Kudos

import com.sap.gateway.ip.core.customdev.util.Message

import java.util.Map

import java.util.Iterator

import javax.activation.DataHandler

def Message processData(Message message) {

def attachments = message.getAttachments();

def bTypeSupported = false;

if ((attachments!=null)&&(!attachments.isEmpty())) {

attachments.values().each{ attachment ->

if (bTypeSupported == true){

return;

}

def sContentType = attachment.getContentType()

message.setProperty("MailContentType", sContentType);

sContentType = sContentType.substring(0, sContentType.lastIndexOf(";")).toLowerCase();

if (sContentType.contains("csv")){

sContentType="text/csv";

bTypeSupported = true;

}

if (bTypeSupported == true){

message.setProperty("MailContentTypeSupported", "True");

message.setBody(attachment.getContent());

message.setHeader("attachment_fileName",attachment.getName());

message.setHeader("attachment_type",sContentType);

}

}

}

return message

}

MortenWittrock
Active Contributor
0 Kudos

Hi

It would be easier to look into this, if you:

1) Used the CODE button to insert the script (without it, the formatting is all over the place)

2) Removed all commented-out code before posting

Regards,

Morten

amit023
Explorer
0 Kudos

Hi Morten thanks a lot for replying,

First attempt is always failing with this script and it is working fine only when we retry the script again to process the attachment in email.

Not sure if this is impacted due to groovy script or the Email adapter in SAP BTP.

Accepted Solutions (0)

Answers (2)

Answers (2)

amit023
Explorer
0 Kudos

another problem is you cannot keep tracing enabled for longer duration and once again another pathetically designed adapter for handling things like attachment for which we have to take help from groovy script.

vishalakshmi
Contributor
0 Kudos

Hello AR,

you can introduce some error handling and retries in your script, retry the below logic,

import com.sap.gateway.ip.core.customdev.util.Message def MAX_RETRIES = 3 def Message processData(Message message) { def retryCount = 0 while (retryCount < MAX_RETRIES) { try { processAttachments(message) return message } catch (Exception e) { retryCount++ if (retryCount < MAX_RETRIES) { log.error("Error processing attachments. Retrying (Attempt $retryCount)") } else { log.error("Max retries reached. Unable to process attachments.") throw e } } } return message } def processAttachments(Message message) { def attachments = message.getAttachments() def bTypeSupported = false if (attachments != null && !attachments.isEmpty()) { attachments.values().each { attachment -> if (bTypeSupported) { return } def sContentType = attachment.getContentType() message.setProperty("MailContentType", sContentType) sContentType = sContentType.substring(0, sContentType.lastIndexOf(";")).toLowerCase() if (sContentType.contains("csv")) { sContentType = "text/csv" bTypeSupported = true } if (bTypeSupported) { message.setProperty("MailContentTypeSupported", "True") message.setBody(attachment.getContent()) message.setHeader("attachment_fileName", attachment.getName()) message.setHeader("attachment_type", sContentType) } } } }

a MAX_RETRIES constant to specify the maximum number of retry attempts.

The processData function now includes a retry loop that attempts to process attachments. If an exception is caught, it will retry up to the maximum number of times defined by MAX_RETRIES. The processAttachments function remains mostly the same for processing attachments. This retry logic should help address the issue of attachments not being fully loaded or available on the first attempt. If there's an error during processing, the script will log the error and retry until the maximum number of retries is reached or the processing is successful.

Alternately you can check the below blog for configuring mail sender Adapter in Integration suite.

https://blogs.sap.com/2023/02/26/inbound-email-integration-from-o365-to-sap-s4-via-sap-cpi/

Thanks,

Lakshmi.

amit023
Explorer
0 Kudos

Did'nt helped.