Learn how to work with enhancements of a global class — such as post-methods — and get to know how to plan, structure, and implement a project in which you make your enhancements switchable. Assign the relevant packages to a switch, assign the switch to a reversible business function, and switch the whole project on in transaction SFW5.
Key Concept
Using the Switch Framework, a framework in the ABAP stack of SAP NetWeaver, you can make all your enhancements switchable by encapsulating all enhancements with a business function to the switches. If your business function is reversible, you can easily switch off all enhancements of the Enhancement Framework to return an application to its original state.
Get to know a more complex example that uses different ways of enhancing a global class. In this example, you will learn how to use the advantages of the enhancement technology efficiently and thereby build different add-on functionality for the basic application. The use case is not extremely complex and is simplified so that you can focus on the enhancement technology. The basic application allows you to retrieve flight data from a database table. You will develop two enhancement projects. In the first enhancement project, you need to get additional information for every row by adding two columns and retrieve the additional data by selecting it in a post-method. Next, in a second enhancement project, you insert an additional search condition in a new input field and create an additional button. To make the new search criteria work, you need an overwrite method for the controller method and a new method in the back end.
Once you have created and wired up all the enhancements you need for this example, you are going to make the two projects switchable so that you can decide which of the two enhancements to switch on. Your enhancement projects profit from the Switch Framework in two ways:
- You can provide different add-on functionality on top of a standard application, for example, in the global IT department of your company and just switch on or off the new functionality in subsidiaries as needed.
- Encapsulating your enhancements with switches and a business function helps you to decide if errors in the SAP application that you have adapted result from a problem with the SAP code or if they were caused by your code.
Just encapsulate all your enhancements by switches, bundle the switches in a reversible business function, and there you are. If the business function is switched on, the new functionality — including the new UI elements — is available at runtime. If you switch off the new functionality, you return to the former state of the application. Elements that are switched off or deactivated are not even compiled, so they cannot slow down an application or influence its performance in any way. Just switch off your code. If the problem vanishes, the culprit is most probably in your code; if the problem persists, this indicates that the problem lies within the SAP code.
Note
As I will explain in some more detail, you can easily switch off all enhancements of the Enhancement Framework and you get the original state of an application. With switchable ABAP Dictionary elements and customizing, things are more difficult. For the time being, switching them off does not change anything.
Enhance the Global Class with Two Switchable Enhancement Projects
The enhancement example sketched in the “Enhance All Layers of an Application with the New and Classic Enhancement Technologies” article was fairly simple because all the enhancements were in one source code unit. Now I’ll show you how the framework copes with more complex requirements. Again you have a Web Dynpro ABAP user interface and you want to add new features to it.
This is what the application looks like (Figure 1). It is again based on the ABAP flight model from SAP IDES.

Figure 1
The original state of the application
In this application you can search flights by date and you get the result set with the components airline, flight number, date, maximum number of the seats of the plane, and currency.
Suppose that for your first enhancement project, you want to show some more details of the flight, namely the departure and destination city (Figure 2).

Figure 2
Enhancement project 1 adds two columns to the report, plus the relevant changes to fill them
In a second enhancement project, you need to make the search more complex by adding another search criterion and a button that resets the search fields (Figure 3).

Figure 3
Enhancement project 2 adds a new search criterion, a Reset Input button, and the relevant changes in the back end
In the second project you also want to cache the result set in a public attribute of the class that provides the application logic for your search. This is a mere enhancement in a back-end class that is the global class we are enhancing in this project.
Since you know, in principle, how to create enhancements and their containers, the enhancement implementations, I will not dwell on most of the details, but just explain what enhancements you need and show details of some features with which you might not yet be acquainted — most significantly how to make an enhancement switchable.
Why and How to Switch Enhancements
In a way, your enhancements are packed in boxes of their own although at runtime they are processed as part of the application they enhance. What about these enhancements once they are in the system? When an enhancement option is implemented, the code is always processed at runtime. Whether the code is processed in addition to the original code or substitutes some code depends on the kind of enhancement option.
Of course, it would be nice if you could run an enhanced application in its pure state and its enhanced state. This way you could develop and have different flavors of an application in one system and you could switch on the add-on functionality as you need it. This is just what the Switch Framework is for and the reason why switching is important.
All enhancements are switch ready. Just attach a switch to the enhancements, wire up the switch in the proper way, and there you are. If the switch is off, the enhancements are not processed at runtime. Using this technology you can develop the two enhancement projects in one system, transport these enhancements plus the original application to different systems, and just switch on the enhancements you need.
Figure 4 shows you how the Switch Framework works in principle. A customer company has added some enhancements in two packages and assigned a switch to each package. All the enhancements are encapsulated by the business function xyz. (By the way, in a real-world project you should, of course, choose a semantic name for your business function.)

Figure 4
What happens when enhancements are switched off
Enhancements are switched package-wise and the switch is assigned to the respective packages because, in general, you want to switch whole projects that realize some add-on functionality, and not just single syntactic features of an application. In principle, these projects can be comprised of enhancements in different packages. This is why the unit that you can activate or switch on is the business function. If you activate a business function, all the switches assigned to it are switched on (Figure 5). You need not and cannot switch on the single switches individually.

Figure 5
What happens when enhancements are switched on
Note
This graphic in Figure 5 is simplified in one respect. As you already know, all enhancements live in enhancement implementations. However, this intermediate level does not change the principle I want to explain. Therefore, I decided to leave it out of Figure 5.
When the business function and its switches are switched on, all the enhancements that are part of the packages that are switched are processed at runtime in the appropriate position they enhance.
All enhancements of the new Enhancement Framework are switchable, as are many other objects:
- One group of objects can be switched package-wise by assigning the switch to the package: appends, includes for ABAP Dictionary structures, fixed value appends to domains, secondary indexes, append search helps, and Business Configuration Sets (BC Sets)
- Other objects can be switched by direct assignment of a switch to them: screen elements, flow logic, menu entries and functions, IMG nodes, and customizing
If you build your own application from scratch with switchable add-ons, you can use all these objects and switch them as you like. If you as a customer assign a switch to an SAP object with direct switch assignment like a menu entry, this can only be done in the editing tool of the relevant object in the change mode. And it is a modification of the relevant object.
So much for the background of the Switch Framework. Let us now go on and have a look at how the example projects are realized.
Enhancement Project 1
Figures 6, 7, and 8 show the enhancements you need for the first enhancement project.

Figure 6
Enhanced Web Dynpro View of Web Dynpro component Z_DEMO_CLASS_ ENHANCEMENT with two new columns and two new attributes

Figure 7
Enhanced Class ZCL_FL_TAB_MANAGER with new post-method of method GET_FLIGHTS_BY_DATE

Figure 8
Append to structure ZSTRU_FLIGHTS_1
I want to focus here on the class enhancement, because the rest is similar to what I have shown above. In general, it is always possible to overwrite a whole method by an enhancement. However, overwriting a method increases the probable adjustment effort. Usually, you overwrite a method by copying the original code and then adding your changes. If SAP changes the original code later, you must adjust your OVERWRITE method also. For this reason, it is better to realize the changes in a pre- or post-method if possible. Figure 9 shows the original code of method GET_FLIGHTS_BY_DATE in which e_tab_flight is the returning parameter for the resulting flights and i_date is the input date.
SELECT carrid connid fldate price currency FROM sflight INTO corresponding fields of TABLE e_tab_flight WHERE fldate = i_date.
|
Figure 9 |
The code of the original method GET_FLIGHTS_BY_DATE |
In the implementation of the post-exit of method GET_FLIGHTS_BY_DATE you write the following code. By the way, for those readers not so familiar with the terminology of the framework so far: A post-exit of a method is executed after the execution of the method (Figure 10).
FIELD-SYMBOLS <wa> TYPE LINE OF zcl_fl_tab_manager=>zty_tab_flights1. LOOP AT e_tab_flight ASSIGNING <wa>. SELECT SINGLE cityfrom cityto FROM spfli INTO (<wa>-zz_city_from , <wa>-zz_city_to ) WHERE carrid = <wa>-carrid AND connid = <wa>-connid. ENDLOOP.
|
Figure 10 |
The implementation of the post-exit of method GET_FLIGHTS_BY_DATE |
The price for using a post-method is that now you have two SELECTs on the database instead of a join that you could use in an OVERWRITE method. A join would make for better performance. In case of mass data processing, you must find a trade-off between performance and low adjustment effort. There can be no general recommendation on how you should decide in a particular case. This is something that is up to you in the particular situation. All I can do here is to show you the advantages and disadvantages of each solution.
You need not change anything in the interface of the class because the interface is typed with an ABAP Dictionary structure. You should put the Web Dynpro enhancements in a different package than the class and ABAP Dictionary enhancements. In an analogous way, you also choose separate packages for the enhancements of project one and project two so that you end up with four packages. To have the enhancements of the two projects in different packages is very important. Objects that are switched package-wise can only be switched separately if they are in a different package.
Note
If you want to switch your enhancements, you should plan their package assignment diligently. Once you have two enhancements in one package, there is no way to switch them separately.
Enhancement Project 2
What you do in the second project requires some more changes in the code in addition to the changes to the Web Dynpro (Figures 11 and 12). In Figure11, the changes include a new label, new input field, and a new button; one new attribute in context SEATS_MAX; an overwrite method for handler method of search button; and a new method for handler of new button Reset Input. In Figure 12, the enhanced class contains the new method GET_FLIGHTS_BY_SEATS_MAX and a new attribute.

Figure 11
Enhanced Web Dynpro View of Web Dynpro component Z_DEMO_CLASS_ ENHANCEMENT

Figure 12
Application logic enhancements
In contrast to the enhancement project 1, you now need more complex search criteria. The new input that provides the maximal number of seats on the plane must be stored in a new attribute in the context. Adding a second search criterion requires that the action handler of the button is able to handle the additional input. The actual select in the application logic is done by a new method GET_FLIGHTS_BY_SEATS_MAX_DATE, which you add as an enhancement and which now has two optional input parameters. The OVERWRITE method of the action handler calls this new method and raises and handles an exception in case none of the input values is provided.
In the new method GET_FLIGHTS_BY_SEATS_MAX_DATE you check which of the values are initial (these are ones that are not provided) and search accordingly in a different way (Figure 13). If the date is not provided, you need not take it into account in the SELECT statement.
IF i_date IS INITIAL. SELECT p~carrid p~connid p~fldate p~seatsmax p~currency c~cityfrom c~cityto FROM sflight AS p INNER JOIN spfli AS c ON p~carrid = c~carrid AND p~connid = c~connid INTO TABLE e_tab_flight WHERE p~seatsmax > i_seats_max. ELSE. SELECT p~carrid p~connid p~fldate p~seatsmax p~currency c~cityfrom c~cityto FROM sflight AS p INNER JOIN spfli AS c ON p~carrid = c~carrid AND p~connid = c~connid INTO TABLE e_tab_flight WHERE p~fldate = i_date AND p~seatsmax > i_seats_max. ENDIF.
|
Figure 13 |
The new search method GET_FLIGHTS_BY_SEATS_MAX_DATE |
The new button to reset the input just requires a simple handler method that you also provide as an enhancement. I will explain the containers for the enhancements, the simple enhancement implementations, and their structure in the next section.
Make an Enhancement Project Switchable
The way to make an enhancement switchable is to attach a switch to the package to which the enhancement belongs. This is a two-step process. First, you create a switch. Run transaction SFW1, and then enter the name Z_DEMO_WD_CL_S1 as the name of the new switch and click Create (Figure 14).

Figure 14
Create a switch using transaction SFW1
Second, enter the name of the packages to which you want to assign switches, TEST_SPJ_CL_BACKEND and TEST_SPJ_CL_WD, as shown in Figure 15. As a consequence, all enhancements and other switchable elements that are in this package will not be compiled if the switch is switched off.

Figure 15
Assign packages to a switch
In the example, you attach the switch to the TEST_SPJ_CL_WD package, which holds the UI changes, and the TEST_SPJ_CL_BACKEND package, which holds the back-end enhancements by entering the name of the packages in the table as shown in Figure 15. Activate the changes, and there you are. The enhancements that are part of these packages are now dependent on the state of switch Z_DEMO_WD_CL_S1.
Note
A switch that is not assigned to a business function is always switched off. The only way to switch on functionality that is assigned to a new switch is to assign this switch to a business function and to switch on this business function.
The Switch Framework is built to cope with large enhancement projects and so the unit you can switch is the business function, not the single switch. A business function can be assigned to many switches. Once you activate a business function, the dependent switches are switched on. It is not possible to change the state of a switch that is not assigned to a business function. You need to create a business function in transaction SFW2 (Figure 16).

Figure 16
Create a Business Function with transaction SFW2
It is important to choose the appropriate type for a business function. In this case, it is the enterprise business function (Figure 17). The industry business functions, for example, are reserved for the functionality of the SAP Industry Solutions.

Figure 17
Choose the appropriate type of business function
Note
It is very important to choose the right type in this dialog window. You cannot change this property later once this dialog window is closed. If you have chosen the wrong type, you can only create a new business function of another type and delete the initial one.
You can only switch off or deactivate reversible business functions. If you want to deactivate the business function after it has been switched on, you must first make it reversible by selecting the Reversible check box (Figure 18).

Figure 18
Make the business function reversible so that it can be switched off
Before you activate the business function you have to assign the switch to it (Figure 19).

Figure 19
Assign a switch to a business function
After activating the business function, you could now switch it on in the transaction SFW5. However, in my example, you first make the second step of the project switchable in a second business function before switching on both business functions.
To show you that there are different ways to achieve the same aim, this time use one switch for the package of the back-end enhancements and another one for the package of the Web Dynpro enhancements. Assign both switches to a second business function. The project is structured with two business functions:
- Z_DEMO_CL_PART_1 with the switch Z_DEMO_WD_CL_S1
- Z_DEMO_CL_PART_2 with the switches Z_DEMO_WD_CL_S2 and Z_DEMO_WD_CL_S3.
Figure 20 shows you this structure plus the packages, the enhancement implementation, and their elements.

Figure 20
The two business functions in the project
In Figure 21, you can see the effect of the activation of each business function on the application.
- The first business function is activated.
- Both business functions are activated.
- None of the business functions is activated.

Figure 21
The relation of the objects in the project: business functions, switches, enhancement implementations, enhancement implementation elements, and packages
It is very easy to activate one or many business functions. Execute transaction SFW5. As you can see in Figure 22, the Enterprise Business Functions appear in a folder of the same name. Open the folder so you can activate the two business functions you have built. Select the business functions you want to activate by clicking the check box on the right side of the screen. Then click Activate Changes. While you are activating the changes, make sure no other users are on the system. In this example, for such small business functions, the activation is very fast.

Figure 22
How to activate business functions in the transaction SFW5
After you have activated the business function, the icon with the bulb is highlighted in yellow (Figure 23).

Figure 23
The business functions are activated
If the business function is reversible, you can deactivate it by deselecting the relevant property and activating the changes. In the example, you want to be sure that business function Z_DEMO_CL_PART_2 can only be activated if business function Z_DEMO_CL_PART_1 is also activated. You enable this dependency as a property of the relevant Business Function, as shown in Figure 24.

Figure 24
How to define dependencies between business functions
The dependency is also reflected in the transaction SFW5 (Figure 25). Click the dependencies icon
to see the dependencies in the window below the business functions.

Figure 25
How dependencies among business functions are shown in transaction SFW5
The transaction checks the dependencies and makes sure that you only activate a business function if its prerequisite business functions are also activated. It is also not possible to deactivate a business function while its dependent business function is activated.
When structuring and organizing a switchable enhancement project, keep the following considerations in mind:
- Put enhancements that you might want to switch separately from each other in different enhancement implementations. It is not possible to switch some enhancements separately if they are in the same enhancement implementation.
- Make sure that you have a business function that covers the whole functionality that you want to switch on or that you declare the right dependencies among several business functions. No user wants to bother about syntactic and semantic dependencies between development objects that realize some additional functionality. They are interested in activating the whole functionality.
- If your business functions are reversible you can switch them off again. This must be handled with caution. If you switch only code you can be sure that after switching something off the application is really the same as before you switched the functionality on.
Dictionary Appends cannot be switched off before SAP NetWeaver 7.00 Enhancement Package 2 that is available for SAP ERP customers when they install SAP ERP Enhancement Package 5. In general, it is not recommend to switch off ABAP Dictionary objects just for testing, because data on the database might get lost. For example, switching off an additional column of a database table removes the column and all its data from the database.
With customizing things are even more difficult. Once a BC Set is activated and the relevant data is part of the customizing tables, it is not possible to restore the original state of these customizing tables. So, of course, you can switch off a reversible business function with dependent switchable BC Sets, but still you will not get rid of the changes in the customizing tables by switching off the business function.
Conclusion
This article shows you how to enhance an object-oriented application in two steps, in particular on how you use a particular class enhancement efficiently. You have learned how you benefit as a customer from the Switch Framework. It is very easy to create switches and business functions of your own. Doing so you can build an application and switch on different add-on functionality as you need it. The Switch Framework hides all the complexity from the user who just has to switch on one or many business functions. This way the user does not have to fiddle with single switches, but can concentrate on a semantic level of business functions. Making your additions to an SAP application is also a strong advantage when you look for the source of errors or other unwanted behavior of an application. If all your new functionality is properly encapsulated by switches, which in turn are bundled by one or many reversible business functions, you can just switch off the new functionality. This is a very convincing and elegant way to decide whether the source of an error lies in the SAP objects or your code.
Thomas Weiss
Thomas Weiss has a Ph.D. in analytic philosophy and worked as a professional writer before he joined the SAP NetWeaver product management training team in 2001 where his responsibilities included the e-Learning strategy for ABAP. After becoming more involved in writing ABAP material himself, he is now a member of Solution Management of the SAP NetWeaver Application Server ABAP. One of his main interests lies in rolling out ABAP topics both for experts and for beginners by writing weblogs in SDN such as his series on ABAP Unit or his contributions to the weblog series for ABAP beginners series part 3, part 4, and part 7. (You find the whole series with all his contributions here).
You may contact the author at thomas.weiss@sap.com.
If you have comments about this article or publication, or would like to submit an article idea, please contact the editor.