Fetching data based on certain selection criteria remains an important requirement for SAP developers. Learn about the various scenarios that can arise and the coding for accessing the employee data from SAP standard and custom infotypes.
Key Concept
In ABAP objects, global interfaces are defined using transaction code SE24. Standard classes can implement these interfaces. Any methods that are contained within the implemented interface can be called using the objects of these classes. Accessing data from various infotypes is an essential requirement for SAP ERP HCM developers. This is typically needed in report programs that HCM users may ask for in order to view data from one or more employee infotypes. SAP provides a number of useful classes and methods for this purpose. Due to the ease and reliability with which data from standard and custom infotypes can be read into ABAP programs, knowledge of this topic is essential for SAP developers. Additionally, using this object-oriented approach is recommended over other techniques, such as use of function modules or obsolete forms of IMPORT statements. ABAP classes and methods are available for both reading and changing the data residing in infotypes. A detailed discussion of ABAP objects is beyond the scope of this article. For more on classes and objects in ABAP, refer to the following link:
https://help.sap.com/saphelp_nw70/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm?frameset=/en/48/4aad83b75211d194f50000e8353423/frameset.htm¤t_toc=/en/d3/2e974d35c511d1829f0000e829fbfe/plain.htm&node_id=424&show_children=false. I provide an overview of the classes and interfaces available for reading infotype data in custom programs. In the first section I cover the steps required to read one or more records from an infotype table. Next, I show how you can use the standard methods for reading a single infotype record for various validity date criteria. Finally, I show how to fetch the long text stored via simple and easy steps for a given infotype record.
Note The primary audience for this article is SAP HR developers and users. I provide coding examples and necessary screenprints to illustrate my points. Readers can easily adapt the coding examples used in this article to suit their requirements.
Classes and Interfaces for Infotype Data Access
Before diving into the details of the programming steps related to infotype data access, let’s take a look at the classes and methods used for this purpose. In this section, I discuss the main classes and methods needed to read data residing in decoupled infotypes. All these classes let you read the data from both standard and custom-developed infotypes. Later in this article, I discuss the coding required to use these classes in order to access infotype data. One of the most important classes used in the reading process is class CL_HRPA_READ_INFOTYPE. It provides a central factory method, GET_INSTANCE, that lets you generate the handle needed to access the relevant infotype data. This handle belongs to the IF_HRPA_READ_INFOTYPE interface (see the next section of this article for more details about this interface). The IF_HRPA_READ_INFOTYPE interface is one of the most important. The necessary methods of this interface and their purposes are shown in
Table 1.
Method name | Purpose |
READ | Reads a set of records from a given infotype table |
READ_SINGLE | Reads a single infotype record based on the supplied parameters |
Table 1
Methods of interface IF_HRPA_READ_INFOTYPE
Note The initial factory method code is the same for calling the READ and READ_SINGLE methods of the IF_HRPA_READ_INFOTYPE interface.
Another important class needed for correct data access is CL_HR_PNNNN_TYPE_CAST. This converts a generic internal table into a meaningful and understandable form of the infotype that developers are required to read. Last but not least, let’s look at the CL_HRPA_TEXT_CLUSTER class, which provides the useful method READ for reading the long text stored for a particular infotype record. (For more information about an important step that needs to be taken before proceeding with the read operation, see the “Getting a Reading Handle” sidebar.)
Getting a Reading Handle Before looking at the coding of the reading process, let’s become familiar with a step needed for all types of read operations. Before calling the method for the relevant read operation, always include the block of code shown in
Figure A.
DATA: READING_HANDLE TYPE REF TO IF_HRPA_READ_INFOTYPE. CL_HRPA_READ_INFOTYPE=>GET_INSTANCE( IMPORTING INFOTYPE_READER = READING_HANDLE ).
Figure A
Getting a reading handle
Here, you call the GET_INSTANCE static method of the CL_HRPA_READ_INFOTYPE class. This gives you a reference to the IF_HRPA_READ_INFOTYPE interface. Once you have this reference, you can use it to call the appropriate method for reading infotype data. All three types of the read operations covered in this article use this method call as a first step.
Reading a Set of Records from an Infotype
Once you are familiar with the interface methods and the factory method required for reading the handle, you can write the code for the read program. For simplicity’s sake, first let’s cover how to read a set of records stored within an infotype. As an example, let’s read data from infotype 22 (education details). First, the steps for fetching a set of records within a specified date range for a given employee number: The first step is to call the GET_INSTANCE method of the CL_HRPA_READ_INFOTYPE class, as shown in
Figure 1.
"" step 1
DATA: reading_handle TYPE REF TO if_hrpa_read_infotype.
cl_hrpa_read_infotype=>get_instance(
importing infotype_reader = reading_handle ).
Figure 1
Getting an instance to read handle
In this case, you use the READ method (with the READING_HANDLE variable) of the IF_HRPA_READ_INFOTYPE interface for reading a set of records from the database. The parameters of the READ method are shown in
Figure 2.
Figure 2
Parameters of the READ method
As you can see, this method has a number of mandatory and optional parameters. The code for reading data from infotype 22 is shown in
Figure 3.
"" step 2
DATA: it_generic_data TYPE hrpad_prelp_tab.
reading_handle->read(
EXPORTING
tclas = 'A'
pernr = '1273'
infty = '0022'
begda = '19900101'
endda = '20150922'
no_auth_check = space
IMPORTING
infotype_tab = it_generic_data
data_exists = data_exists
).
Figure 3
Calling the READ method
Here, you are reading all the records in infotype 22, for employee number 1273 that are valid for start and end dates 1.1.1990 and 22.09.2015. Once the READ method code is executed, the data is returned in the internal IT_GENERIC_TABLE table. (This is defined based on the dictionary table type HRPAD_PRELP_TAB.) This data is not yet in a very useful or understandable form, as shown in
Figure 4.
Figure 4
Table IT_GENERIC_DATA data
As you see here, all the data fields are stored together in a single field, DATA1, of the internal table IT_GENERIC_DATA. This is not sufficient as you can’t tell what the actual data is that corresponds to each field of infotype 22.
The exporting parameter DATA_EXISTS of the READ method contains a value X when data is read from the relevant infotype table. If no data is read, the exporting DATA_EXISTS parameter contains a space. You can check the value of this parameter in order to control the processing of your program.
To convert the data into an understandable form, use the static method PRELP_TO_PNNNN_TAB of the CL_HR_PNNNN_TYPE_CAST class (
Figure 5).
"" step 3
DATA: it_p0022 TYPE p0022ar_tab.
cl_hr_pnnnn_type_cast=>prelp_to_pnnnn_tab(
EXPORTING
prelp_tab = it_generic_data
IMPORTING
pnnnn_tab = it_p0022 ).
Figure 5
Convert generic data into an infotype-specific format
Before the method call, you declared an internal table IT_P0022 based on infotype 22 and table type P0022AR_TAB. (For other infotypes, the corresponding table type is in the form PNNNN_TAB.)
When the method is executed, the data residing in the internal table IT_GENERIC_DATA is converted into a more meaningful form and returned in table IT_P0022, as shown in Figure 6.
Figure 6
Internal table IT_P0022
As you can see, the data is stored in the corresponding fields of the internal table IT_P0022. The developer can then further use the contents of this internal table.
Reading a Single Infotype Record
Now that you know how to read a set of records from an infotype, let’s see how to read a single record from the database. Again, I am using infotype 22 as my example. The steps are shown below. As discussed, you need to call the GET_INSTANCE static method of the CL_HRPA_READ_INFOTYPE to get the instance of the business logic. Then call the READ_SINGLE method and pass appropriate values for the necessary parameters. The parameters of the READ_SINGLE method are shown in
Figure 7.
Figure 7
READ_SINGLE method parameters
As you can see, all the importing parameters of this method are mandatory. Of most importance is the MODE parameter. Providing appropriate values to the MODE parameter allows you to do more without using any complicated conditions. The typical values it may be supplied with are shown in
Table 2.
Supplied value | Meaning |
0 | Overlaps last record of selection period |
1 | Overlaps first record of selection period |
2 | Record that contains end of selection period |
3 | Record that contains start of selection period |
4 | Record with exactly matching key |
Table 2
Permissable MODE values
Since you are supplying all the key field values for the record to be read from table PA0022, (e.g., the record stored in table PA0022 that exactly matches the supplied key), pass the value 4 for the MODE parameter. The code for accessing a single valid record is shown in
Figure 8.
DATA READING_HANDLE TYPE REF TO IF_HRPA_READ_INFOTYPE. DATA P0022 TYPE P0022.
”” step 1 cl_hrpa_read_infotype=>get_instance( IMPORTING infotype_reader = reading_handle ).
”” step 2 reading_handle->read_single( EXPORTING tclas = 'A' pernr = '1273' infty = '0022' subty = '83' objps = ' ' sprps = ' ' mode = '4' begda = '19960101' endda = '19990101' no_auth_check = SPACE importing pnnnn = p0022 ).
Figure 8
Reading a single infotype record from a database
Once the code is executed, the required data is returned in variable P0022 (corresponding to parameter PNNNN), as shown in
Figure 9.
Figure 9
Data shown in variable P0022
Here you get the details of Master’s Degree (subtype 83) stored in infotype 22 for employee number 1273. These may be further refined to suit the requirements of your users. Let’s look at another read single example. Suppose that you now need to read the last record from infotype 22, belonging to subtype 83, that is valid between the dates 01.01.1980 and 01.01.2016. To do this you need to slightly change the coding previously used. In this case, the MODE value to be supplied is 0, while calling the READ_SINGLE function. The code of the method then looks like the one shown in
Figure 10.
reading_handle->read_single(
EXPORTING
tclas = 'A'
pernr = '1273'
infty = '0022'
subty = '83'
objps = ' '
sprps = ' '
mode = '0'
begda = '19800101'
endda = '20160101'
no_auth_check = SPACE
importing
pnnnn = p0022 ).
Figure 10
Use mode value 10
Reading the Long Infotype Text for a Given Record
As already mentioned, it is also possible to read the text stored for a particular infotype record in a program. In another of my
HR Expert articles, “Use Clusters to Access Infotype Text,” [December 15, 2004] I showed a method of reading infotype texts from table PCL1 using macros and import statements. To replace that approach, here is a more up-to-date way of reading infotype texts. As an example, let’s assume that the employee number 1273 (read in the earlier section) has infotype text stored as shown in
Figure 11.
Figure 11
The stored infotype text
Next, write a code to read this text in your program. For this example, let’s assume that you’ve already completed the READ_SINGLE method shown in
Figure 8 and build from there. Use the READ method of the CL_HRPA_TEXT_CLUSTER class. The parameters of the method are shown in
Figure 12.
Figure 12
The parameters of the READ method
As you can see, the main parameter is PSKEY, which is comprised of the fields shown in
Figure 13.
Figure 13
Parameter PSKEY’s fields
Once the key fields of the row whose long text is to be read are known, you can carry out two further steps. Continuing the example (shown in
Figure 8), before writing the code for reading the long text, formulate the key for the infotype record whose text is to be read. This is done by assigning the values contained in structure P0022 to the corresponding fields of the PSKEY structure as shown in
Figure 14.
" step 3
data pskey type pskey.
MOVE-CORRESPONDING p0022 to pskey.
Figure 14
Filling PSKEY
Note The most important parameter of the CL_HRPA_TEXT_CLUSTER=>READ method for reading long text is the PSKEY structure that contains the key fields of the record whose text you are fetching.
Next, call the static method READ of the CL_HRPA_TEXT_CLUSTER class. The text is returned in the internal table INFOTYPE_TEXT based on the associated type HRPAD_TEXT_TAB. This code is shown in
Figure 15.
" step 4
data infotype_text type HRPAD_TEXT_TAB.
cl_hrpa_text_cluster=>read(
EXPORTING
tclas = 'A'
pskey = pskey
no_auth_check = ' '
IMPORTING
Text_tab = infotype_text ).
Figure 15
Reading long text
The complete code listing is shown in
Figure 16.
DATA READING_HANDLE TYPE REF TO IF_HRPA_READ_INFOTYPE. DATA P0022 TYPE P0022.
”” step 1 cl_hrpa_read_infotype=>get_instance( IMPORTING infotype_reader = reading_handle ).
”” step 2 reading_handle->read_single( EXPORTING tclas = 'A' pernr = '1273' infty = '0022' subty = '83' objps = ' ' sprps = ' ' mode = '4' begda = '20150101' endda = '99991231' no_auth_check = SPACE importing pnnnn = p0022 ).
" step 3
data pskey type pskey. MOVE-CORRESPONDING p0022 to pskey.
" step 4
data infotype_text type HRPAD_TEXT_TAB. cl_hrpa_text_cluster=>read( EXPORTING tclas = 'A' pskey = pskey no_auth_check = ' ' IMPORTING Text_tab = infotype_text ).
Figure 16
The complete code listing for reading long text
Once the program is executed, the internal table contains the infotype text, as shown in
Figure 17. The developer can then use it for further processing.
Figure 17
Internal table INFOTYPE_TEXT
You should now understand how to read a set of records from an infotype, a single infotype record, and the text stored in that record. In the
next installment of this series of articles, I cover how to program the updating of data residing in infotypes.
Rehan Zaidi
Rehan Zaidi is a consultant for several international SAP clients (both on-site and remotely) on a wide range of SAP technical and functional requirements, and also provides writing and documentation services for their SAP- and ABAP-related products. He started working with SAP in 1999 and writing about his experiences in 2001. Rehan has written several articles for both SAP Professional Journal and HR Expert, and also has a number of popular SAP- and ABAP-related books to his credit.
You may contact the author at
erpdomain@gmail.com. If you have comments about this article or publication, or would like to submit an article idea, please contact the
editor.