🎓beginner⏱20 min.SAP BTP ABAP EnvironmentBeginnerABAP DevelopmentSAP Business Technology Platform
You will learn
✔How to define internal early numbering
✔How to implement internal early numbering
✔How to preview and test enhanced travel app In the previous exercise, you’ve enhanced the data model of the business object (BO) entity Travel. In the present exercise, you will define and implement the unmanaged internal early numbering to set the primary key TravelID of new Travel instances during their creation in your application. You will also use the static field control to specify some fields to read-only. A number range object will be used to determine the unique travel identifiers.
You need to have access to an SAP BTP, ABAP environment, or SAP S/4HANA Cloud, ABAP environment or SAP S/4HANA (release 2022 or higher) system.
For example, you can create free trial user on SAP BTP, ABAP environment.
Make sure, your system has the ABAP flight reference scenario. If your system hasn’t this scenario. You can download it here. The trial systems have the flight scenario included.
Reminder: Do not forget to replace the suffix placeholder ### with your chosen or assigned group ID in the exercise steps below.
Numbering: Numbering is about setting values for primary key fields of entity instances during runtime. Different types of numbering are supported in RAP which can be divided into two main categories:
Early numbering: In an early numbering scenario, the primary key value is set instantly after the modify request for the CREATE is executed. The key values can be passed externally by the consumer or can be set internally by the framework or an implementation of the FOR NUMBERING method. The latter will be implemented in the present exercise.
Late numbering: In a late numbering scenario, the key values are always assigned internally without consumer interaction after the point of no return in the interaction phase has passed, and the SAVE sequence is triggered. Further reading: Numbering
Step 1Define internal early numbering
—
Define the (unmanaged) internal early numbering in the behavior definitionbdef iconof the Travel entity.
Open the behavior definitionbdef iconZRAP100_R_TravelTP_### of the Travel entity.
Specify the statement provided below just after the statement authorization master( global ), just before the opening curly bracket { as shown on the screenshot.
ABAP
earlynumbering
The warning message Early Numbering for CREATE ZRAP100_R_TRAVELTP_### is not implemented is now displayed for the statement create;. You can hover the yellow underlined statement to display the message or have a look at the Problems view.
You can ignore it for now. You will handle it later.
Travel BO Behavior Definition
Delete following statement:
ABAP
field(mandatory:create)TravelID;
Specify the field TravelID as read-only field since it will be set at runtime by the internal early numbering.
Info: The static field control is used to restrict properties of particular fields.
For that, replace the statement
ABAP
field(readonly)TravelID;
You can use the ABAP Pretty Printer function (Shift+F1) to format the source code.
To complete the definition, you need to declare the required method in behavior implementation class. You can use the ADT Quick Fix to do that.
Set the cursor on the statement create; and press Ctrl+1 to open the Quick Assist view.
Select the entry Add earlynumbering method for create of entity zrap100_i_travel_### in local handler from the dialog to add the FOR NUMBERING method earlynumbering_create to the local handler class lcl_travel of the behavior poolclass iconZRAP100_BP_TRAVEL_###.
Travel BO behavior Definition
The behavior implementation classclass iconZRAP100_BP_TRAVEL_### will be enhanced appropriately.
You are through with the definition of the early numbering and can now go ahead and implement its logic.
You will now implement the logic for the unmanaged internal early numbering in behavior implementation class (aka behavior pool)class iconZRAP100_BP_TRAVELTP_### of the Travel BO entity. A number range object will be used to determine the IDs of new Travel BO entity instances.
Please note: Due to time constraints (and simplification reasons), the proper error handling when using number ranges is not part of this implementation example.
Nevertheless, you can find a more comprehensive implementation example of a managed BO with a number range object in the behavior implementation class /DMO/BP_TRAVEL_M located in the package /DMO/FLIGHT_MANAGED. A description of this implementation is provided in the RAP development guide Developing Managed Transactional Apps on the SAP Help Portal.
Check the method interface of the method earlynumbering_create in the declaration part of the local handler class lcl_travel.
For that, set the cursor on the method name and press F2 to open the ABAP Element Info view and examine the full method interface, for example, the importing and changing parameters. You can navigate to the different (derived) types.
Travel BO Behavior Pool
Signature of the FOR NUMBERING method for managed business objects:
IMPORTING parameter entities - includes all entities for which keys must be assigned
Implicit CHANGING parameters (return parameters):
mapped - used to provide the consumer with ID mapping information
failed - used for identifying the data set where an error occurred
reported - used to return messages in case of failure.
Now go ahead and implement the method earlynumbering_create in the implementation part of the implementation class. First, it must be ensured that the imported Travel entity instances do not yet have an ID set. This must especially be checked when the BO is draft-enabled. For that, remove all instances with a non-initial TravelID from the imported parameter entities which contains all Travel entities for which a key must be assigned. Insert the code snippet provided below into the method implementation and replace all occurrences of the placeholder ### with your group ID.
ABAP
DATA:entityTYPE STRUCTUREFORCREATEZRAP100_R_TravelTP_###,travel_id_maxTYPE/dmo/travel_id," change to abap_false if you get the ABAP Runtime error 'BEHAVIOR_ILLEGAL_STATEMENT'
use_number_rangeTYPE abap_boolVALUEabap_false."Ensure Travel ID is not set yet (idempotent)- must be checked when BO is draft-enabled
LOOP AT entitiesINTOentityWHERETravelIDIS NOT INITIAL.APPENDCORRESPONDING#(entity)TOmapped-travel.ENDLOOP.DATA(entities_wo_travelid)=entities."Remove the entries with an existing Travel ID
DELETEentities_wo_travelidWHERETravelIDIS NOT INITIAL.
Travel BO Behavior Pool
Get the exact number range for the new ID according to number of relevant Travel entity instances stored in the internal table entities_wo_travelid and determine the current max ID. The number range object /DMO/TRV_M of the ABAP Flight Reference Scenario (located in the package /DMO/FLIGHT_REUSE) is used in the example implementation provided below.
Please note: All participants are using the same number range object /DMO/TRV_M, therefore, the assigned Travel ID will not be gap-free.
For that, enhance the method implementation with the provided code snippet as shown on the screenshot below. As already mentioned, the error handling is kept to the minimum here.
ABAP
IFuse_number_range=abap_true."Get numbers
TRY.cl_numberrange_runtime=>number_get(EXPORTINGnr_range_nr='01'object='/DMO/TRV_M'quantity=CONV#(lines(entities_wo_travelid))IMPORTINGnumber=DATA(number_range_key)returncode=DATA(number_range_return_code)returned_quantity=DATA(number_range_returned_quantity)).CATCHcx_number_rangesINTODATA(lx_number_ranges).LOOP AT entities_wo_travelidINTOentity.APPENDVALUE#(%cid=entity-%cid%key=entity-%key%is_draft=entity-%is_draft%msg=lx_number_ranges)TOreported-travel.APPENDVALUE#(%cid=entity-%cid%key=entity-%key%is_draft=entity-%is_draft)TOfailed-travel.ENDLOOP.EXIT.ENDTRY."determine the first free travel ID from the number range
travel_id_max=number_range_key-number_range_returned_quantity.ELSE."determine the first free travel ID without number range
"Get max travel ID from active table
SELECTSINGLEFROMzrap100_atrav###FIELDSMAX(travel_id)AStravelIDINTO@travel_id_max."Get max travel ID from draft table
SELECTSINGLEFROMzrap100_dtrav###FIELDSMAX(travelid)INTO@DATA(max_travelid_draft).IFmax_travelid_draft>travel_id_max.travel_id_max=max_travelid_draft.ENDIF.ENDIF.
Travel BO Behavior Pool
Hint: If you get the following error message: ABAP Runtime error ‘BEHAVIOR_ILLEGAL_STATEMENT’ then change the value of the variable use_number_range to abap_false. use_number_range TYPE abap_bool VALUE abap_true.
Set the Travel ID for new Travel instances without identifier. Enhance the method implementation with the following code snippet as shown on the screenshot below.
ABAP
"Set Travel ID for new instances w/o ID
LOOP AT entities_wo_travelidINTOentity.travel_id_max+=1.entity-TravelID=travel_id_max.APPENDVALUE#(%cid=entity-%cid%key=entity-%key%is_draft=entity-%is_draft)TOmapped-travel.ENDLOOP.
Remember to regularly use the ABAP Pretty Printer function (Shift+F1) to format your source code.
You can now preview and test the changes by creating a new travel instance in the travel app.
Refresh your application in the browser using F5 if the browser is still open - or go to your service bindingsrvb iconZRAP100_UI_TRAVEL_O4_### and start the Fiori elements App preview for the Travel entity set.
Create a new Travel instance.
Travel App Preview
No dialog for manually entering a Travel ID should be displayed now. The Travel ID will be assigned automatically by the logic you just implemented.
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 4
1. Define internal early numbering2. Implement internal early numbering3. Preview and test enhanced travel app4. Test yourself
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.