Monday, December 2, 2013

Making use of Global Macro


In SAP R/3 system, one can declare macros locally and globally. The local macro is declared in the reports or objects they are used and their life starts and ends in those objects. In other words, their lifetime is limited to the run time of the object they are associated with. The global macros used in SAP are stored in table TRMAC. SAP has provided some macros in the table, especially the commonly used ‘BREAK’ and its variations, which are used while debugging. The global macros are stored as table entries in TRMAC.

To create a global macro, follow the steps as mentioned below:

1. Create a new entry to Table TRMAC as shown below

2. Add the name of the macro and Macro text as required and save the entry.
3. The macro could be called in different abap objects like user-exits, badi, bapi and even in reports as shown below.
For an example, we are adding the following macro to TRMAC and saving the entry. After saving, one can call the macro in an abap object like SAP report. Now activate the report after syntax check.



The result of the macro executed is as shown below.

4. To ensure the macro is available in different systems in landscape, the transport request containing the table entry in TRMAC needs to be moved. In case the transport request is not automatically generated. One can create a transport in SE01/SE03/SE09 containing this table entry and then use it as needed. If any authorization checks are removed, please do the necessary and add those to the transport request.


Although, we can declare macro using this method., It is highly recommended that the creation of macros using this method is only recommended in case, the macros are meant to be reused in different programs and providing the same functionalities in all programs. Macros are generally discouraged in many programming languages and should be used wisely. Unlike other methods of analyzing, breakpoints will not execute in MACRO and debugging MACRO is a difficult task for any programmer.

Remember the intentions of Macros are :
To encourage code reusability. The global macros can be used  in any objects and thus helps in reusability of code.
To ensure no code repetition is needed. Global macro functionalities will be available throughout the systems
To make the program more readable. Proper usage of Macros, helps in code readability.
To hide the method of execution or implementation. Sometimes, it helps in hiding the techniques used in execution or implementation from programmers/developers. Macro can help in this respect.

No comments:

Post a Comment