← Blog

ChatGPT: Groovy code help for SAP Cloud Integration

Testing ChatGPT on Groovy, XSLT and ABAP code for a purchase order mapping, and asking it when to use SAP PI or SAP Cloud Integration.

December 4, 2022 · 7 min read

Frankly, I’m not the best Groovy programmer, I don’t get to spend enough time in it to create good code. But from time to time, good ideas pop up where a few lines of Groovy script will make a big difference. I would often spend some time stack overflowing or finding some guides and piecing them together.

To edit Groovy, we recommend using the Groovy editor in Figaf. We have even added OpenAI APIs to it, making it easier to use with all the context of your integration. See more here.

I have seen ChatGPT by OpenAI mentioned in a lot of places for how to write parts of code, blogs, or university papers on a high level. Some of it sounds good but is wrong. It seems quite powerful. I found that I could try it out in the beta. But how does it stack up in a SAP world?

Groovy

It is really simple to get started: you just write what you want to achieve, and then you get it. My example was to try to convert a purchase order and make some modifications in it. Then it writes as you would type, which is pretty cool. The first time you try a prompt, it will give some more information.

ChatGPT prompt asking for a Groovy script that sorts JSON purchase order lines and adds totals, with the start of its Groovy answer

The full code looks something like this. Granted it did not pick up on the fact that the input should be JSON and it should be parsed.

// define the purchase order
def purchaseOrder = [
    customer: 'Acme Corp',
    items: [
        [
            id: 1,
            sku: 'ABC123',
            quantity: 10,
            price: 9.99
        ],
        [
            id: 2,
            sku: 'DEF456',
            quantity: 5,
            price: 19.99
        ],
        [
            id: 3,
            sku: 'GHI789',
            quantity: 2,
            price: 29.99
        ]
    ]
]

// sort the items in the purchase order by line ID
purchaseOrder.items = purchaseOrder.items.sort { it.id }

// add a line total and purchase order total and quantity fields
purchaseOrder.items.each { item ->
    item.lineTotal = item.quantity * item.price
}
purchaseOrder.total = purchaseOrder.items.sum { it.lineTotal }
purchaseOrder.quantity = purchaseOrder.items.sum { it.quantity }

// convert the purchase order to a string in JSON
def json = groovy.json.JsonOutput.toJson(purchaseOrder)
println json

After the code you get a section where it prints the result, so you can see if it is giving the correct result.

Notice that it figures out to add the line ID in the input document, good comments, and then it calculates the totals.

It did miss the fact that I wanted the input document in JSON. I can change my prompt to ensure this works.

Revised ChatGPT prompt asking the Groovy script to start by parsing the purchase order in JSON format
Groovy code from ChatGPT that calculates a line total for each item and adds the purchase order total and quantity
In a different version, it added total calculations at the line level.

And then I could make a small modification in the code in GroovyIDE, and it was runnable.

The ChatGPT Groovy script running in GroovyIDE in CPI simulation mode, with the JSON purchase order shown as output

I would say I’m pretty impressed with the way you can use this to write Groovy to parse a JSON payload and convert it to XML.

XSLT

If I wanted the same in XSLT, it is just to make a few modifications. And state that I want an XSLT.

ChatGPT prompt and answer creating an XSLT 1.0 stylesheet that converts the purchase order to XML with a Figaf namespace

The XSLT does not work with a JSON input and is only version 1.0. The XSLT is pretty good and it covers the basics of what I want to get with such a document.

I tried to get it as version 3.0, but here the flow was different, and it forgot to calculate order lines and totals. So you will need to perform some sanity checks to see if it does the correct work.

ChatGPT answer with an XSLT 3.0 stylesheet for the purchase order using a figaf namespace prefix

ABAP

If you are more into ABAP, then you can also create a program for it. I’m not sure how to evaluate the quality of it, but it looks pretty cool. I’m pretty impressed that this made it to the tool because of the lack of ABAP code for the usage.

ChatGPT prompt and answer generating an ABAP report that parses the JSON purchase order and converts it to XML

The full ABAP code

REPORT zfigaf_purchase_order.

DATA: lt_json TYPE string,
      lt_purchase_order TYPE STANDARD TABLE OF string,
      ls_purchase_order TYPE string,
      lt_items TYPE STANDARD TABLE OF string,
      ls_item TYPE string,
      lt_lines TYPE STANDARD TABLE OF string,
      ls_line TYPE string,
      ls_total TYPE string,
      ls_quantity TYPE string,
      ls_xml TYPE string.

" define the JSON purchase order
lt_json = '''{
  "customer": "Acme Corp",
  "items": [
    {
      "lineId": 1,
      "sku": "ABC123",
      "quantity": 10,
      "price": 9.99
    },
    {
      "lineId": 2,
      "sku": "DEF456",
      "quantity": 5,
      "price": 19.99
    },
    {
      "lineId": 3,
      "sku": "GHI789",
      "quantity": 2,
      "price": 29.99
    }
  ]
}'''.

" parse the JSON purchase order
CALL TRANSFORMATION json_to_abap SOURCE lt_json
  RESULT lt_purchase_order.

" get the purchase order
READ TABLE lt_purchase_order INTO ls_purchase_order INDEX 1.

" get the items
CALL TRANSFORMATION abap_to_abap SOURCE ls_purchase_order
  RESULT lt_items.

" order the lines by line ID
SORT lt_items BY lineId.

" add a line total to each line
LOOP AT lt_items INTO ls_item.
  DATA(lv_line_total) = ls_item-quantity * ls_item-price.
  ls_item-lineTotal = lv_line_total.
  MODIFY lt_items FROM ls_item.
ENDLOOP.

" add a purchase order total and quantity
DATA(lv_total) = 0.
DATA(lv_quantity) = 0.
LOOP AT lt_items INTO ls_item.
  lv_total = lv_total + ls_item-lineTotal.
  lv_quantity = lv_quantity + ls_item-quantity.
ENDLOOP.

ls_total = |<total>{ lv_total }</total>|.
ls_quantity = |<quantity>{ lv_quantity }</quantity>|.

" convert the items to XML
CALL TRANSFORMATION abap_to_abap SOURCE lt_items
  RESULT lt_lines.

" create the XML string
ls_xml = |<Figaf_purchase_order xmlns="https://figaf.com/orders">
             { ls_purchase_order }
            

SAP PI or Cloud Integration

To round off, I wanted to ask the age-old question: what should I use for my scenario?

ChatGPT answer explaining when to use SAP PI for on premise integration and SAP CPI for cloud integration

In some prompts, it is written that ChatGPT does not have internet access and only articles dating back from 2021, so that is why I used CPI and not Integration Suite/Cloud Integration. I’m pretty impressed with what it was able to figure out to handle this. It goes into a lot of detail.

The last paragraph is the following. You will get the same response from any SAP Cloud Integration consultant if you do not provide enough context about your landscape and the setup.

In summary, you would use SAP PI or SAP CPI depending on your specific integration needs and requirements, such as the type and location of the systems and applications that you are integrating, the integration scenarios and capabilities that you need, and the deployment model that you prefer.

Conclusion

I was shocked to learn that it was possible to try this in the beta program. And that it was possible to use the code created by the tool to generate some code that could be used to start your programming. This could save quite a bit of time for developers to use this as a starting point, and you don’t need to have a large experience to start coding in UDFs in Groovy.

It is also interesting that it has much knowledge about what is going on in an SAP universe, I would have thought it was more difficult to get data about it.

I’m sure we need to collaborate better with some AI to help generate the code. Then we, as integration developers, will get to focus on understanding business requirements and not coding. We probably still will have a job for the next few years. I have no doubt that the functions can improve to create much bigger parts of the logic for handling the integration. For now it will not be able to create a BPMN iFlow to handle the integration.

ChatGPT step by step guide for creating an SAP CPI iFlow from HTTP to FTP with a content modifier
I should stop asking dumb questions, it is clearly able to create good guides by itself. There are parts of this that do not make sense, like step 4. The content modifier cannot format a document from JSON to XML with ease. But you would need to get into the flow to understand such problems.

This post covers these products

Capabilities covered

More from the blog

All posts →

See this working in your landscape

No credit card required.

Integration cost calculator →

Start your free trial

No credit card required

Try Figaf DevOps Suite free for 30 days. It takes less than an hour to get started, and we send everything you need by email.

Join our newsletter

Get the latest about Figaf, SAP and anything relevant to SAP integration, every week in Figaf Weekly.