cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SAP Cloud Platform Integration: Call Groovy Script with parameters

Florian_Kube
Participant
12,304

Hello,

I am using groovy scripts for logging in different steps. Since I do not want to copy the scripts repeatedly and change just two values, I thought there should be the possibility to use parameters.

Here is the script I want to use:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message logpayload(Message message, String nameOfAttachment, String mimeType ) {
    def payload = message.getBody(String.class);                            
    def messageLog = messageLogFactory.getMessageLog(message);
    messageLog.setStringProperty("Logging", nameOfAttachment);
    messageLog.addAttachmentAsString(nameOfAttachment, payload, mimeType);
    return message;
}

I configurate it like this:

However, when I run it I get the following exception. Does anyone have an Idea how I could solve this issue?


java.lang.NoSuchMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.logpayload(message, "FlatFile.txt", "text/plain")() is applicable for argument types: (com.sap.gateway.ip.core.customdev.processor.MessageImpl) values: [com.sap.gateway.ip.core.customdev.processor.MessageImpl@737f25ed]

Thank you,

Florian

View Entire Topic
MortenWittrock
SAP Mentor
SAP Mentor

Hi Florian

CPI expects the signature of your Script step function to be:

Message functionName(Message message)

You can, however, add as many functions with arbitrary signatures to your script file, as you need.

You could, therefore, change your logPayload method to be:

def void logPayload(Message message, String nameOfAttachment, String mimeType) 

and then add any number of functions calling it, like:

def Message logInitialXml(Message message) {
   logPayload(message, 'initialXML.xml', 'application/xml')
   message // In Groovy, this is equivalent to return message
}

That would remove the duplicated code.

Let me know how it works out.

Regards,

Morten