DRF Custom ABAP BAdI Filter Implementation
This post describes how to implement your own custom ABAP filter methods in the DRF Data Replication Framework.
It’s useful when you would like to:
- filter the business objects before the replication from S/4, but
- the available standard filters in DRFF do not meet your requirements, and/or
- you would like to add a more complex filtering logic to DRF.
This post shows how to add, configure, implement and monitor a custom ABAP class to the Business Partner (986) replication, but the idea is the same for other business objects as well.
The replication filter (“I need to filter by Partner Number”) can be created and assigned in transaction DRFIMG, and then the exact filter values (to filter by Partner Number = 1234567) can be configured in transaction DRFF.
In DRFIMG > Enhance Default Setting for Outbound Implementations > Define Outbound Implementations we can see, which Filter Object is assigned to the outbound implementation. A Filter Object can contain multiple Filters – more about that later.
Find the outbound implementation of the business object (for the Business Partners via services -> 986 -> 986_3).
Scroll right in the table and find the Filter Object column: the name of the Filter Object for the BP replication is MDG_BP_SFT, that’s the standard preconfigured filter.
By selecting “Assign Segment Filter” on the left, the available segment filter objects of the business object (later these can be configured in DRFF as well) can be also displayed, but now we are just going to assign a filter directly to MDG_BP_SFT
The same Segment filters can be seen in DRFF:
Back to the main topic:
Now we know which Filter Object (MDG_BP_SFT) belongs to the Business Object (986), we can check how the Filter Object is defined in DRFIMG > Enhance Default Setting for Outbound Implementations > Define Filter Objects . There we can see which Filters are assigned to the Filter Object.
Find the Filter Object MDG_BP_SFT in the list.
Additional Info: Note, that there’s no Table Name assigned to this Filter Object – so no explicit simple filters can be assigned to this filter, more details later or in the F1 help, but that’s OK, because we want to implement our own ABAP class)
Select Assign Filters on the right and display the Filters that are assigned to this Filter Object.
Each Filter has a number.
- The first 4 filters are from SAP, the last one is my custom filter.
- That the number of Custom Filters must be 8* or 9*, the rest is reserved by SAP.
- If you assign your custom filter to a number range that is reserved for SAP, you are going to see a warning when adding a new filter.
For Explicit Complex Filters the rightmost column called Filter Class is the place where you can assign your own ABAP class.
- Create your own a class in the customer namespace
- Filter classes must implement the interface IF_DRF_FILTER that comes with a method APPLY_FILTER – this method will be called by DRF during runtime, and in this method you can add your own filtering logic (more about that later).
First, let’s talk about how new filter parameters can be created. These parameters can be configured in DRFF, that of course your class is also going to receive the configured values, so it can dynamically filter the Business Objects.
The column General Filter Parameter contains DDIC structure names.
- The fields from these structures will be visible and can be configured in later in DRFF
Here’s an example:
- The field “Purchasing Category ID” is defined in the structure /SRMSMC/S_PUC_DRF_F for filter 40 “Purchasing Category Filter”
- The fields “Exclude Business Partners” and “Exclude Sites” are defined in MDG_BS_BP_S_DRF_EXCLUDE_FLT for filter 79 “BP Exclusion Filter”
When the filter values are configured in DRFF all these fields
- Purchasing Category ID
- Exclude Business Partners
- Exclude Sites
- (and a lot more)
are visible in the same dropdown. You can add them to the Filter Criteria and configure their values.
See the dropdown in DRFF:
For my custom filter parameters I’ve defined an new DDIC structure ZST_GABOR_MDG_DRF_BP_FLT with 4 fields:
I’ve defined a separate data element for each filed, since the label that will be visible in DRFF comes from the data element. My custom data elements have a certain prefix in the Field Label, this way these can be clearly /visually separated from the standard filters during configuration in DRFF.
4 new fields in the DRFF dropdown:
The other place where the DDIC structure comes into play is the filter class. The ABAP method of the filter class is going to receive the configured filter values with a reference to this DDIC structure name and filed names. (Well, that’s a bit more complex since in DRFF the values can be configured similarly to a SELECT-OPTIONS, but the name of the name of the DDIC structure and the name of the fields are going to play a role even there (more about that later) ).
Implementing the filter method:
As it was already mentioned, the assigned ABAP class has to implement the interface IF_DRF_FILTER, the method APPLY_FILTER is the place where the custom filtering logic can be added.
The method receives multiple input parameters:
The most important are:
- IT_EXTERNAL_CRITERIA contains the filter parameter values configured in DRFF
- IT_UNFILTERED_OBJECTS contains the list of business objects before the custom filter
- ET_FILTERED_OBJECTS the contains the list of business objects after the custom filter
- IO_BAL is a reference to the DRF Application Log, you can add messages to the BAL that will be visible in transaction DRFLOG
In the method you can add your own custom filtering logic and add/remove entries to/from ET_FILTERED_OBJECTS.
It’s a good idea to check the source code of the existing fitler classes delivered by SAP to see how
- the filtering parameters can be fetched from IT_EXTERNAL_CRITERIA, and how
- the FILTERED/UNFILTERED_OBJECTS can be accessed and processed.
Take these classes as examples:
- Remember, that in DRFF it is possible to define both Including and Excluding filter criteria, and the same criteria can be maintained multiple times with different values (SELECT-OPTIONS).
- Note that both ET_FITLERED_OBJECTS and IT_UNFILTERED_OBJECTS are of TYPE SORTED TABLE which is a generic type. To access the fields of this table, you can check which concrete data type is used in the implementations delivered by SAP. For the Business Partner replications it is TYPE TABLE OF mdg_bs_bp_s_drf, but for different Business Objects it might be something else, since the Interface IF_DRF_FILTER is independent of specific Business Object types.
- The before and after lists of business objects can be monitored even in DRFLOG (see the section about the monitoring)
Now. it’s your turn to add your own filtering logic to the method!
- The minimal implementation (without doing any filtering) is ET_FITLERED_OBJECTS = IT_UNFILTERED_OBJECTS which can be used for debugging and testing purposes.
Once the APPLY_FILTER method is implemented, activated and the class is assigned to the DRF Filter, you can set a breakpoint in the method and test the replication.
The replication can be started from DRFOUT or from Transaction BP (at least for Business Partners), and when the object is saved and the replication is triggered, the debugger should stop in the APPLY_FILTER method.
The runtime behavior of the Data Replication Framework can be checked in transaction DRFLOG, that can display the application log entries of DRF. Here you can see:
- When was the replication executed
- Which Objects Types (986 = Business Partner) have been replicated
- Which Filters have been executed
- How many objects have been replicated
- Which Objects have been filtered out by the filters
Here I can see, when the Filter Object MDG_BP_SFT was started, then my Custom Filter, the one that I added with number 91 was also executed.
Drilling down to the details, it can display:
- What filter values were configured and used during runtime – see the IT_EXTERNAL_CRITERIA parameter of the filtering method
- The list of Business Objects before and after the filter – see the IT_UNFILTERED_OBJECTS and ET_FILTERED_OBJECTS parameters of the filtering method
If your filtering method adds any messages to the application log via its IO_BAL reference, like
io_bal->add_msg_string( iv_msgty = 'W' iv_msgtext = 'Hello').
Those messages will be also visible in DRFLOG:
That’s all. I hope you find the above information useful!
- Decide what new fields (if any) you would like to add to the configuration in DRFF.
- Create data elements and a structure in the ABAP Dictionary for these fields.
- Create your own ABAP class that implements the interface IF_DRF_FILTER.
- Find the Filter Object in DRFIMG that belongs to the Business Object type you would like to filter.
- Add a new Filter for the Filter Objects
- Filter number is in the customer range
- Assign your DDIC structure for the new parameters
- Assign your ABAP class
- Configure the filter parameters in DRFF
- Test the replication, check the results in DRFLOG