In integration scenarios, developers might come across a situation where the receiver system may want to have count of the messages in the header of the payload. The count in the header should automatically get incremented whenever a new message is triggered. In SAP_CPI we have a very useful feature ‘Number Ranges’ which automatically increments a particular variable and can easily be used in iflows to highlight the count of the message flow to the receiver.
The ‘Number Ranges’ tile can be found in the path [Overview->Number Ranges] in our tenant. By means of an example, I will demonstrate how a Data Store can be created with a dynamic Entry ID. The dynamic value of the Entry ID will be set by accessing the Number Range value which gets auto incremented.
- We create a new number range ‘zrange_test’ as below-
The value of the number range will be initialized with minimum value ‘1’ and gets incremented up to maximum value 4. The field length specifies the number of characters in the number; for e.g. for field length ‘3’ the values of the number range will be 001, 002, etc.
If the checkbox for ‘Rotate’ is checked, then the value of number range again begins with 001 once it reaches the maximum value. If we leave the checkbox unchecked, then the message will fail with error-
- Create an iflow ‘zflow’ as below using a simple file-to-file scenario by means of SFTP adapter. There are various SAP blogs available that can be referred for working with SFTP adapter in SAP CPI.
The SFTP will pass simple xml file and the content modifier will pass the incoming payload as-is to the groovy script.
In the content modifier step, we create a header variable ‘count’ of type ‘Number Range’ and pass the value as ‘zrange_test’ (the Number Range which was created in step 1).
- In the groovy script we write a small code as below to access the header variable ‘count’ (created in step-2) and concatenate it with some description in a new string. The new string is then set to a new header ‘rangecount’.
- In the next step we use the Write operation for creating the DataStoreOperation as specified in the blog –
Here we can observe that the parameter Entry ID is accessing the header variable ‘rangecount’ which was created in groovy script (as specified in step-3)
- On deploying the iflow and subsequently triggering the files through SFTP we can check the path [Overview->Manage Data Stores] and can be found that the new datastore is created with dynamic Entry ID getting generated due to multiple triggering of the iflow.
In step-1; while creating number range, if we check the checkbox ‘Rotate’, then the message will try to create an Entry ID with Test_001 again, and it will fail with below error-
- From the message log it can be observed how the Number Range flows in the header of the payload to the receiver.
The content modifier receives a payload from SFTP and passes it as-is to the next step-
The groovy script contains the header variable ‘count’ which was created in content modifier step-
The Write step now has an additional header variable ‘rangecount’ which was created in groovy script-
Finally, the payload will have the header details as below-
The count in the header will be auto incremented with subsequent message triggers as it is assigned with number range.
We can also observe that the Entry ID of the Data Store is been dynamically assigned.
This blog can be used as a reference to get an initial understanding of how ‘Number Ranges’ feature can be used in various scenarios. In real time, some receivers want to have messages in sequence and track if any message has got missed or failed during processing. With this counter in the header of the message will help the business in keeping track of the message flows.