Skip to Content
Technical Articles
Author's profile photo Fatih Pense

Share and Store CPI Groovy Scripts with Links

Since the online Groovy IDE was very well received by the community, I decided to improve it by enabling sharing and integration. Thank you everyone for comments, giving feedback, and positive vibes!

While I was preparing this post a related productivity improvement feature was added to SAP CPI which enables running parts of flows in simulation. You should definitely check it out!

Use Cases

  • Sharing a message exchange input and Groovy code in a question.
  • Preparing challenges for beginners in a company or preparing tutorials.
  • Sharing very specific solutions for a problem.
  • Send postcards with a Groovy link printed as QR code
  • You can store your favorite templates in browser bookmarks!

How to share

Just click on the share button:

Since I decided not to store anything on the server, a long URL will be copied to the clipboard. It is gzipped which benefits a lot working with XML. You can try it with a reasonably large XML file!

  1. XML file: 460KB (no indentation, one-line input which causes UI problems on most editors)
  2. XML file with indentation: 550KB
  3. Groovy IDE URL for the same input(2): 16KB
  4. My profile picture in this page: 17KB

(I set the server URL limit to 50KB)

Tip: If you are sharing it in a blog post you can hide the long URL like this.

How to integrate

You can trigger Groovy IDE by preparing this URL and opening a new tab:

https://groovyide.com/cpi/share/v1/ + URLSafeBase64(gzip($jsonString))

JSON structure as TypeScript type definition:

interface StateType {
    input: {
        body: string,
        headers: object,
        properties: object
    };
    script: {
        code: string
    };
}

Conclusion

Thanks for your feedback again. If you think about some useful new feature you can write it in the comments or contact me.

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Raffael Herrmann
      Raffael Herrmann

      Hi Fatih,

      thanks for letting my wish come true. For me this feature is a real dealbreaker! 🙂

      Author's profile photo Fatih Pense
      Fatih Pense
      Blog Post Author

      Thank you for the feedback Raffael, it is these small things that help the consultant so feedback is very important 🙂

      Regards,
      Fatih

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Good feature!

      Thanks,

      Syam

      Author's profile photo Fatih Pense
      Fatih Pense
      Blog Post Author

      Thank you for the feedback Syambabu!

      Regards,
      Fatih

      Author's profile photo Tang Lambert
      Tang Lambert

      Nice!!!

      Author's profile photo Fatih Pense
      Fatih Pense
      Blog Post Author

      Thank you for the feedback Tang!

      Regards,
      Fatih

      Author's profile photo Xichuan Wu
      Xichuan Wu

      Hi Fatih,

      GroovyIDE is a great tool! Many thanks!

      The following code is working fine in GroovyIDE, but not in CPI DEV tenant.

      import com.sap.gateway.ip.core.customdev.util.Message;
      import java.util.HashMap;
      import groovy.json.*;

      def Message processData(Message message) {
      //Body: JSON file
      def jsonFile = message.getBody();
      def aCandidate = new JsonSlurper().parse(jsonFile);

      //Properties
      def map = message.getProperties();
      def base64PDF = map.get("base64PDF");

      /*
      aCandidate.candidate.resume.file = base64PDF;
      def jsonOut = new JsonBuilder(aCandidate).toPrettyString();
      */

      def builder = new JsonBuilder(aCandidate);
      builder.content.candidate.resume.file = base64PDF; // line 21
      def jsonOut = builder.toPrettyString();

      message.setBody(jsonOut);
      return message;
      }

       

      In CPI DEV tenant, I get the error:

      javax.script.ScriptException: java.lang.Exception: java.lang.NullPointerException: Cannot set property 'file' on null object@ line 21 in script2.groovy, cause: java.lang.NullPointerException: Cannot set property 'file' on null object

      The JSON structure is like this:

      {
      "candidate": {
      "candidate": {
      "contactDetails": {
      "PhoneNumber": "notavailable"
      },
      "email": "john.doe@a.net",
      "firstName": "John",
      "lastName": "Doe"
      },
      "resume": {
      "file": "base64String",
      "fileName": "Profile.pdf"
      }
      }
      }

      What is the root cause of the different behaviour between GroovyIDE and CPI?

       

      Many thanks in advance for your comments!

      Regards, Xi