ChatGPT: Groovy code help for SAP Cloud Integration

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 the 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 the groovy we will recommend using our new Editor in Figaf. See more here.

I have seen ChatGPT by Open AI be 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.

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 if 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.

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.

I would say I’m pretty impressed with the way you can use this to write groovy. 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.

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 which 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.

ABAP

If you are more to ABAP, then you can also create a program for it. I’m not sure 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.

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 questions. What should I use for my scenario.

In some prompts, it is written that ChatGTP 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 integration the logic for handling the integration. For now it will not be able to create a BPMN iFLow to handle the integration.

I should stop asking dumb questions, it is clearly able to create good guides self. There are parts of this that do not make sense. Like step 3. 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.

Simplify your SAP Integration in under 10 minutes with Figaf DevOps Suite on Cloud.

 
No credit card is required. 30 days free trial.

Latest Articles