Data Archive – Retrieval From Archived File in Custom Reports – Part 1
As a fellow member of this archiving project, I was given some challenging requirements to enhance the existing z-reports to provide the option to select the data from a)live b)archive. In this blog, I tried to document all the points I learned so that this will be useful for others too.
Data archiving is the process of moving data which are no longer used (frequently) by the business to a separate storage device for long-term retention. These archive data consists of older data that remains important to the business however it must be retained for future reference or regulatory compliance reasons.
The primary benefits of archiving data are:
- Reduced cost on Storage
- Maintenance and Operational costs
- Data processing performance on the Reports and dashboards
- Backup and Restoration time
In regular business scenario, at the time of archiving we keep only past 3 years data and archived the earlier data. But for some regulatory purposes or analytical or references we need to see them as a report. SAP has provided a way to retrieving the archived data in Standard reports and/or screens by providing a Data Source option.
Objective of this blog is mainly on how to handle the existing custom reports and/or any new reports that are to be developed based on archived data. Because once archived the business data are deleted from the live database and our challenge is to bring them from the archived file and present it as a report on demand.
This blog assumes that the Data archiving part is competed using SARA transaction and data files are stored in the server. Archived objects are created.
In this Blog Example – I used the Archive Object MM_MATBEL
Step -01 : Getting Run Number of the Archiving Object
This step is how to find the Run Number of the Archived entry of object MM_MATBEL. For that Go to SARA transaction and execute it by giving the Archive name. Then Press the statistics button. You can see a output as below
You can see three are lot of Archiving sessions executed with run numbers 28,29,52 and 54. For each archiving session one Run number will be generated. We can refer them either as Document No or Job Number or Run Number.
In this Example We take – Archiving Object : MM_MATBEL — Run Number : 54
Step 02 – Getting Archiving Object Key
In order to read from the Archive – we need to know the two important data
- Archiving Object
- Archiving Object Key
- Archiving Object Offset
We can get these details as below from the tables
- ADMI_FILES – By providing the Run number This gives related Archive Key
- ZARIXMM1 – By providing the Object key in this table – we can get the Offset values
From above table – you can see for Row 54 – the archive key is – 000054-001MM_MATBEL
Next from the below table – we shall get the offsets
So At the end of Step-02 We know
- Archiving Object : MM_MATBEL
- Run Number : 54
- Archiving Key : 000054-001MM_MATBEL
- Archiving Offsets : Three line items ( Meaning in this session – 3 documents are archived)
Step 03 – ABAP Program Logic ( Template )
ZARIXMM1 – is the MM Infostructure table where the index of all archived data are maintained. (Refer to SARI Transaction)
Select the records from ZARIXMM1 for the archive key is equal to what we evaluated above.
Loop the result of above select statement
- Read information from archive ( FM ARCHIVE_READ_OBJECT )
- Get the requested header detail data (FM ARCHIVE_GET_TABLE )
- Get the item detail (FM ARCHIVE_GET_TABLE )
- Collect them in the final table for processing
We shall see below each step mentioned above.
Step 03 A – ABAP Program
The following sections shows the data declaration required for this program.
PARAMETERS : arobj TYPE arch_obj-object OBLIGATORY, docn TYPE admi_run-document. * data declaration DATA: lt_archivekeys TYPE TABLE OF zarixmm1 WITH HEADER LINE, la_archivekeys TYPE zarixmm1, lt_mkpf TYPE TABLE OF mkpf, la_mkpf TYPE mkpf, lt_mseg TYPE TABLE OF mseg, la_mseg TYPE mseg, gt_mseg TYPE STANDARD TABLE OF mseg, gt_mkpf TYPE STANDARD TABLE OF mkpf, wa_admi_files TYPE admi_files, ls_handle LIKE sy-tabix.
Step 03 B – ABAP Program
The Program accepts the Archive Object and the Run number. Based on the input it selects the record from the table zarixmm1. The return data will be a internal table. Then the loop starts on the return internal table, fetch each record archived and store the table data into final table for reporting.
- First select statement finds the archive key from the table admi_files based on the run number selected.
- Second select statement selects all the archived object records with archive key and offset.
- Inside the loop statement
- read_archive_object call will open the archive and return a handle id
- get_header_data will further read the object and get the header table data using the handle id
- get_item_data will read the line item data using the handle id
- Finally all are collected in output table
- Then print the output
START-OF-SELECTION. SELECT SINGLE * FROM admi_files INTO wa_admi_files WHERE document = docn. SELECT * FROM zarixmm1 INTO CORRESPONDING FIELDS OF TABLE lt_archivekeys WHERE archivekey = wa_admi_files-archiv_key. LOOP AT lt_archivekeys INTO la_archivekeys. PERFORM read_archive_object. PERFORM get_header_data. PERFORM get_item_data. ENDLOOP. LOOP AT gt_mkpf INTO la_mkpf. WRITE :/0 la_mkpf-mblnr, 15 la_mkpf-mjahr. ENDLOOP.
Step 03 C – ABAP Program
The sub routines for the above Perform statements.
FORM read_archive_object . *Read information from archive CALL FUNCTION 'ARCHIVE_READ_OBJECT' EXPORTING object = 'MM_MATBEL' archivkey = la_archivekeys-archivekey offset = la_archivekeys-archiveofs IMPORTING archive_handle = ls_handle EXCEPTIONS OTHERS = 1. ENDFORM. FORM get_header_data . *Get the requested header detail data for material document CALL FUNCTION 'ARCHIVE_GET_TABLE' EXPORTING archive_handle = ls_handle record_structure = 'MKPF' all_records_of_object = 'X' automatic_conversion = 'X' TABLES table = lt_mkpf "Header data EXCEPTIONS end_of_object = 1 internal_error = 2 wrong_access_to_archive = 3 OTHERS = 4. LOOP AT lt_mkpf INTO la_mkpf. APPEND la_mkpf TO gt_mkpf. ENDLOOP. ENDFORM. FORM get_item_data . CALL FUNCTION 'ARCHIVE_GET_TABLE' EXPORTING archive_handle = ls_handle record_structure = 'MSEG' all_records_of_object = 'X' automatic_conversion = 'X' TABLES table = lt_mseg "Line item data EXCEPTIONS end_of_object = 1 internal_error = 2 wrong_access_to_archive = 3 OTHERS = 4. LOOP AT lt_mseg INTO la_mseg. APPEND la_mseg TO gt_mseg. ENDLOOP. ENDFORM.
Step 04 – Test the ABAP Program Logic
We shall now execute the above Custom ABAP code to fetch data from the archive object by giving
- Archiving Object
- Run number
The Input parameter are
Archiving Object : MM_MATBEL
Run Number : 54
The Expected output are Three MKPF Document header items.
Go to SE38 and Execute the program. The selection criteria screen provide the Archiving Object and Run number.
Press Execute. The Out are as below:
The results are matched with the expected one.
In summary, we learned about
- The Material Documents (MKPF, MSEG) – the Archiving Objects is MM_MATBEL
- Function Modules to Retrieve data from Archive object
- CALL FUNCTION ‘ARCHIVE_READ_OBJECT’
- CALL FUNCTION ‘ARCHIVE_GET_TABLE’
- Important tables from where we can get the Job numbers and archive key
- zarixmm1 (Archive data index file)
Though some of the SAP standard programs provided the way to access the data from archive file, we also have some custom programs where we need to incorporate the logic to fetch the data from archive files.
We can implement the above logics to fetch the data from archive in the new custom reporting programs or already existing custom reporting programs.
The basic requirement is that knowing of following items are mandatory for this approach
- Which Archiving Object
- What is the Run number
The for the user it is difficult, we may further extend this to build that intelligence inside the custom program.
In my next blog – I shall try to give on Z-report with data source option – possibly user need not to select the archiving object or run number.
very informative blog
...waiting for part 2
Can you give me like same above zreport need for FI documents and mm documents can you provide ?
need to display archived fi documents and mm documents via zreport like fb03
can you provide
Can you please help with data source process?