Skip to Content
Technical Articles
Author's profile photo Himanshu sachdeva

Avoid Hardcoding in SAP by TVARVC or SETLEAF

At the time of discussing client’s pain points and challenges which they are facing or reviewing the ABAP codes we have noticed that many variables are hardcoded or many Z-tables are present in the SAP system for maintenance of data to use in user exits, BADI’s, Reports, Forms, etc.

Due to Hardcode, code changes is required every time for addition/modification/deletion in the data which requires additional technical efforts along with functional for inputs & testing. Due to Z-table creation also Additional ABAP efforts are required for DDIC creation, table maintenance generator, domains creation or assignment, etc.

A simple Scenario or User Requirement where hardcode/z-tables should be required:

At time of sales order save, Few checks should be done for specific sales organizations that if sales order net value is more than the defined value then error should be displayed. code is required in User exit MV45AFZZ.

 

Code is written with hardcoded sales organizations (1000,2000,3000) and their predefined values as 1400, 1500, 1600 respectively.

 

User Challenges:

  1. In future, The same checks are required for 4000 as well.
  2. Checks are not required for sales org 3000 anymore.
  3. Value should be adjusted for sales org 1000 from 1400 to 1450.

For these small additions/modifications/deletion, ABAP hardcoding will be required or custom table should be created to store the sales organization and their corresponding values which requires additional development efforts (in creation of DDIC, its maintenance, domains, etc.).

 

Solution:

TVARVC:

TVARVC is standard table given by SAP to store the data under variants. we can put the values in TVARVC table to avoid hardcoding and/or creation of custom tables.

 

Transaction code: STVARV

There are two sections under STVARV to store parameters and Selection options.

 

Parameter is used when we have to store only single level of data like a value, single sales organization, etc.

–> Click on Create Button

 

–>

 

In ABAP, we can extract this value by the below snippet for using it in the program:

 

SELECT SINGLE *
  INTO ls_tvarvc
  FROM tvarvc
  WHERE name = 'Z_XYZ_Tolerance_Price'.


Select options used when we have to store multi level of data like a value along with sales organization, etc.

–>Select Select options tab and click on create

 

 

–> Enter the Variable name and click on multiple selections

 

 

–> click on select ranges and enter the values:

 

 

–> Save

Please note: Creation and modification via STVARV don’t ask for Transport request unless we have not marked the checkbox “Include Changed entries in transport request”.

If any user is having the authorization of STVARV or SM30 in production system then entries could be changed without any TR.

 

SETLEAF:

SETLEAF is also a standard table in SAP like TVARVC where we can store the data in sets. Set is structure to store the values and values interval as well (Parameters & select options).

Advantage of sets over TVARVC is that It takes on the domain of the value we are storing, so it can be validated at input time to avoid any wrong entries. we can see the available entries over there.

Transaction code to create the Set: GS01

Transaction code to change the Set: GS02

Transaction code to display the Set: GS03

 

At time of creation of Set, we have to define the table and field name for the domain check.

 

Example / User scenario: For specific sales organizations and customer material number ‘AB123’. something should be done.

 

–> Enter the set name

 

–> Enter the table and click enter

–> Select the field

 

 

–> Click F4 to see the available sales organizations in the system

 

 

Code Snippet:

wa_setleaf type setleaf

SELECT SINGLE * FROM setleaf

into wa_setleaf

WHERE setclass = ‘0000’

AND subclass = space

AND setname = ‘Z_SALES_ORG’

AND valfrom = .vbak-vkorg.

IF (sy-subrc = 0) AND (VBAK-KDMAT = ‘AB123’).

{do something}

ENDIF.

 

Conclusion:

By using TVARVC/SETLEAF, a lot of customer’s effort will be saved at time of changing or enhancing their existing solution which just requires addition or modification in the pre defined values. Along with customer’s effort, Monetary will be saved for additional technical efforts.

Code with the use of TVARVC or SETLEAF is more standardized & Flexible than hardcode or custom tables. It will be easier at time of code migration or for reusing in separate system or client.

 

— Sources of the pictures are screenshot from SAP and no image is taken from the internet.

 

Thank You so much for reading the post. If you want to provide feedback or ask any queries regarding the same then please free to write in the comment section.

Assigned tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Wanderson Carvalho
      Wanderson Carvalho

      Hi,

       

      Congratulations on the post !!!

       

      What is the best way to go about maintenance between environments?

      Author's profile photo Pasumarthi Raviteja
      Pasumarthi Raviteja

      Hi,

      Thanks for this post.

      I have a small query, what if, we have to maintain multiple fields values. Means, we have a z table which will contains multiple fields, in this situation the above will work?

       

      And when we are creating a BAPI we need to create the custom table types and structures for the importing and exporting parameters, do we have any solution for this?

      Author's profile photo YiLiang ZHOU
      YiLiang ZHOU

      Useful blog. Thanks for sharing it.

      Author's profile photo Meera K
      Meera K

      Thanks, nice blog @Himanshu sachdeva. As TVARVC is a buffer table, it may cause performance issues, if no. of entries maintained are more. Right? And how about BRF?

      Author's profile photo Andrea Borgia
      Andrea Borgia

      I had always used the function pair G_SET_GET_ID_FROM_NAME / G_SET_GET_ALL_VALUES but now I see that they're not released to customers, so they're not that much better than reading SETLEAF directly, when it comes to code cleanliness.