Skip to Content

SAP Cloud Platform Integration: Call Groovy Script with parameters

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

Add comment
10|10000 characters needed characters exceeded

5 Answers

  • Best Answer
    Posted on Dec 11, 2017 at 12:07 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 11, 2017 at 02:15 PM

    Hey Morten,

    thank you very much. With the void function it didn't worked for me. So thats the code how it works for me.

    import com.sap.gateway.ip.core.customdev.util.Message
    import java.util.HashMap
    def Message logInitialFlatFile(Message message) {
       message = logPayload(message, "initialFlatFile.txt", "text/plain")
    }
    
    def Message logInitialXml(Message message) {
       message = logPayload(message, "initialXML.xml", "application/xml")
    }
    
    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
    }
    Add comment
    10|10000 characters needed characters exceeded

    • Hi Florian. Glad you could use it. As for void, use

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

      instead, and it should work. That requires changing the code as for the return statements.

      Or you can leave it as it is now, if you prefer.

      Regards,

      Morten

  • Posted on Oct 23, 2018 at 04:35 PM

    Hi Florian,


    another solution would be to use properties or header variables. You can than access them with

    map = message.getHeaders();
    myVar = map.get("variableName");

    or

    map = message.getProperties();
    myVar = map.get("variableName");

    Can be another solution that might be more flexibility but adds more cpi specific code.

    Regards,

    Dominic

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 13 at 05:35 AM

    Thanks to Morten for this solution, just to complete this answer for the benefit of other readers trying to figure out how to use the 'Script Function' feature, when configuring the Script Function name under 'Processing' tab, mention only the script name (no need of parenthesis or passing the 'message' parameter, this is implicitly understood by CPI that only a 'message' parameter can be passed through the Script Function), screenshots below showing the incorrect and correct approach (had to figure this out with few trial and errors as I couldn't find any documentations with examples of using this feature).

    Wrong :

    Correct :


    wrong.jpg (17.7 kB)
    correct.jpg (17.5 kB)
    Add comment
    10|10000 characters needed characters exceeded

    • This.

      So, to surmise, user defined functions in Groovy scripts for CPI iFlows:

      1. Must have only one argument and that's message - hence we only need to specify the Script Function name like Bharath has shown in the 'correct' screenshot.

      2. processData - which appears by default when we create a groovy script, is not mandatory to be implemented.

      Morten Wittrock - please comment if these assumptions are right?

  • Posted on Mar 04 at 06:13 PM

    The post of Dominic is the one!

    Thanks

    Matthias

    Add comment
    10|10000 characters needed characters exceeded