Monday 31 May 2021

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.

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

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

Click on Create Button

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

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

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

Enter the Variable name and click on multiple selections

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

click on select ranges and enter the values:

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

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

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

Enter the table and click enter

Select the field

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

Click F4 to see the available sales organizations in the system

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Learning, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

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.

Source: sap.com

No comments:

Post a Comment