Skip to Content
Technical Articles
Author's profile photo Rahul Yadav

Using Script Collection across various Integration Flows in a Package in SAP CPI

Code reusability is one of the strongest feature of any tool. Earlier SAP Cloud Integration was lacking a prime feature of script reusability like we used to reuse the objects of different software component versions in SAP PI/PO. Till now we were creating same script in many flows as Scripts were referred locally by an Integration flow. We were keeping redundant code on the server like logging payload/headers etc.

But now this issue has been addressed in SAP CPI and script reusability feature is available in Integration Suite to avoid redundant script writing. We can create multiple scripts under one artifact ( script Collection) and that can be called in multiple packages/integration flows

This blog will illustrate the steps to create a script collection and using the underlying script in any package/integration flow.

Step 1: Go to Package -> Edit -> Add Artifact -> Scrip Collection

 

Step 2: Put the name and description of the collection

Step 3: Click on Script collection & edit. Create the Groovy or Java Script repository under this artifact.

Step 4: Create required reusable Java/Groovy scripts.

Step 5: Save and Deploy the artifact.

Now we

Step 6: Go to Integration flow where you want to use script collection. Click on Resources tab. Under that References and Add References.

 

Step 7: Add the Script Collection References. It will list down all the packages those are having Script Collection. Select your package and Script Collection to create a reference link.

Once library is added. It will be visible under reference tab. You can see the package name once you hover the mouse on collection name.

 

Step 8: Now simply add Groovy/Java Script palette and assign the script from reference tab. It will list down all the scripts available in script collection artifact.

 

Step 9: Save and Deploy the Flow and Script Collection artifact

Step 10: Trigger the test payload from Postman and you can see the logPayload scrip will be executed from the referenced package/artifact/Script Collection

 

 

SAP has delivered very nice and much awaited feature but it still it needs some refinement.

  • After deploying the script library it should show where used feature so that any developer can find the interfaces those are using a specific script or script collections.
  • Currently, Script Collection created in package can be used in different integration flows

 

 

Assigned Tags

      15 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Morten Wittrock
      Morten Wittrock

      Hi Rahul

      For now, you can only access scripts in a script collection from within the same package. Sharing scripts across packages will be added later.

      Regards,

      Morten

      Author's profile photo Rahul Yadav
      Rahul Yadav
      Blog Post Author

      Thanks Morten,

      I have updated the blog. That seems a big restriction. Do you know when SAP is releasing the feature where we can access collection across various packages ?

       

      Author's profile photo Morten Wittrock
      Morten Wittrock

      No problem. As for release date, i don't think there is one yet. But it's on the roadmap.

      Regards,

      Morten

      Author's profile photo Philippe Addor
      Philippe Addor

      This has been delivered by now. Just mentioning in case somebody reads this today and thinks it's still valid. 🙂

      Author's profile photo Jens Bühre
      Jens Bühre

      Hi Rahul,

      we wrote a local groovy script with a "helper"-class and many helping functions (like conversions etc):

       

      package src.main.resources.script
      
      public class Helper{
      
         static void convertXYZ(){
            ... do something
         }
      }

      Yet this script is called in some other scripts in the same iflow by importing the script and afterwards by calling the different methods like in this example:

      import src.main.resources.script.Helper
      
      def Message processData(Message message){
         Helper.convertXYZ()
         return message
      }

      We now want to put this helper class in a script collection and calling its methods directly from a local groovy.
      In your example I only see that you call a method from a reference instead of a local groovy. But we want to call a method from a reference IN a local groovy.

      Is there a similar way how to import and use these methods?

       

      Best regards

      Author's profile photo Ankit Vimal
      Ankit Vimal

      Hi Jens,

      I have the same question. Did you find any way to implement this?

      Regards,

      Ankit

      Author's profile photo Jens Bühre
      Jens Bühre

      No, not yet 🙁

      Author's profile photo Thomas Buerki
      Thomas Buerki

      There is a workaround: Define instance methods in the helper class. In a first script, define the class and instantiate an object of this class. Then add a property with the instance of this object to the message:

      import com.sap.gateway.ip.core.customdev.util.Message
      
      public class ClsDummy{
         String getMyName(){
              return 'my name is Dummy'
          }
      }
      
      def Message processData(Message message) {
          def objDummy = new ClsDummy()
          message.setProperty('objDummy',objDummy )
          return message
      }

      In subsequent Groovy-Scripts you can get the property holding the the instance and use it for calling methods:

      import com.sap.gateway.ip.core.customdev.util.Message
      
      def Message processData(Message message) {
          def objDummy = message.getProperty('objDummy')
          message.setProperty('DummyName',objDummy.getMyName())
          return message
      }
      Author's profile photo Philippe Addor
      Philippe Addor

      What to do if I have a JAR file in a script collection, but that JAR is a third-party library and contains no instance methods? I get "class not found errors" in my scripts that are trying to access a static method in that global JAR...

      Author's profile photo Rahul Yadav
      Rahul Yadav
      Blog Post Author

      Hey Philippe,

      JAR

      Package: com.sap.test

      Class name: TestClass

      Method: protected static add(int a, int b)

       

      Groovy Script

      Import class in your package and then directly call static method.

      import com.sap.test.TestClass

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

      def Message processData(Message message) {

      def output = TestClass.add(4,5)

      //code

      }

       

      This should work

      Author's profile photo Philippe Addor
      Philippe Addor

      Thanks Rahul for the hint. But my case was different: The jar is not my own but a library (OpenCSV).

      I hoped that there is a way to upload this library jar into a script collection and then use its (static) methods from any Iflow by just attaching the Script Colleciton to it. But I ended up with uploading the opencsv.jar into every Iflow where I need it...

      Best regards,

      Philippe

      Author's profile photo Andrey Tkachuk
      Andrey Tkachuk

      Hello everyone! A very cool feature!

      But what about scripts (custom function) in the mapping?

      They also repeat from IFlow to IFlow

       

      Best regards

      Author's profile photo Bogdan Popescu
      Bogdan Popescu

      Thank you. Just read this after posted my comment. I hope they get this done soon.

       

      Author's profile photo Shanmukha Siddartha Cherukumudi
      Shanmukha Siddartha Cherukumudi

      Hi,

      I am having an issue importing the classes if in case there are multiple classes present in my single groovy script file. But the Groovy Language as such supports it

       

      Is it a limitation in the SAP CPI

      Kindly let me know

      Author's profile photo Bogdan Popescu
      Bogdan Popescu

      Hello there,

       

      Anybody knows if the functionality of importing scripts in message mapping (ref: https://help.sap.com/docs/CLOUD_INTEGRATION/368c481cd6954bdfa5d0435479fd4eaf/3d5cb7ff43fb4a4b9c28153472f6d0f1.html search Step 3) could be enhanced to use scripts from Script collection?

      "You can create your own custom mapping function. Here's how:

        1. In the Mapping Expression screen area, choose  .
        2. Enter the name of the script, which will be the name of the custom function and choose OK.

          The script editor will be launched.

        3. Enter the script for the custom function you want to create and choose OK"

      I think it would be a nice addition to the Script collection as functionality.

      Regards,

      Bogdan Popescu