In this blog, I will take you through the steps for creating an API to convert number to words in SAP S/4HANA Cloud. I will then provide an example on how to make use of this API to display amount in words in a Form Template.
When this requirement came up initially, my first instinct was to search for available APIs on R3 and the first API that I found was the function module SPELL_AMOUNT, which was universally used. One option to consume this would be to wrap this API in an RFC and call it via cloud connector
But this option felt too cumbersome for me and I wanted to try something simpler. That is when I realized I could create a reusable API in Custom Reusable Elements found under Extensibility Tile and use this.
Without any further ado, let me get into what I did.
As a pre-perquisite, please note that your user should need the business role with business catalog Extensibility SAP_CORE_BC_EXT for performing the following steps
Creation of a Custom Business Object
The first step would be to create a Custom Business Object to store the words that are reused for every number. These are as follows.
I will not be covering the steps on creating a custom business object here and that can be found in this link which I had mentioned earlier.
Create a Custom Business Object from the tile Custom Business Objects under the group Extensibility.
Create the business object with the following structure
Check the field UI Generation so that a UI is created where you could maintain the entries that are mentioned above. You could also maintain Determination and Validation, if you need to perform any data validation and check Service Generation so that you could use the oData, but these are optional.
Save and Publish the Custom Business Object.
Once this is done, click on Maintain catalogs to assign the Business Object to a Business catalog.
Add this under the Business catalog SAP_CORE_BC_EXT and click on OK and the Publish it. This will take a few minutes, but once this is published, you can close this screen.
The Custom Business Object application will now be added under the tile Extensibility
Open the App and maintain the entries.
You can add these entries by clicking on Create.
Click on save once the entries are maintained
The entries should look like as given below, once maintained.
Creation of Custom Reusable Element
The second step will be to create the actual logic to convert the number to words.
For this, select the tile Custom Reusable Elements under the group Extensibility
Create a new custom library by clicking on the + button under the tab Custom Library.
Maintain the following and click on create
Add a new method under the newly created custom library
Click on the method to add signature for the method as follows
Save and publish the custom library. You will be able to add logic to the method only after Publishing.
Once the library is published, click on the method id
This will open the method implementation.
Click on Create Draft and enter the following code.
TYPES: BEGIN OF str_d,
num TYPE i,
word1 TYPE string,
word2 TYPE string,
END OF str_d.
DATA: ls_h TYPE str_d,
ls_k TYPE str_d,
ls_m TYPE str_d,
ls_b TYPE str_d,
ls_t TYPE str_d,
ls_o TYPE str_d.
DATA lv_int TYPE i.
DATA lv_inp1 TYPE string.
DATA lv_inp2 TYPE string.