Extract KNA1 General Customer Master Data in CSV File Using ABAP Program

ABAP PROGRAM(S) – CUSTOMER 

ABAP Program Name – Z_KNA1_CUSTOMER 

  • Program Type – ABAP 
  • ABAP Program Name – Z_KNA1_CUSTOMER 
  • TCODE – Z_KNA1_CUST
  • Output File Location – “C:tmp” 
  • Output File Name- KNA1.CSV 
  • Output File Type – CSV (Column with Semicolons Separator (;) ) 
  • SAP Table Name – KNA1 & KNB1 
  • List Of Fields to Be Extracted – 
  • KUNNR 
  • MANDT 
  • LAND1 
  • MCOD1 
  • NAME1 
  • NAME2 
  • NAME3 
  • STRAS 
  • ORT01 
  • PSTLZ 
  • SPRAS 
  • LZONE 
  • KTOKD 

—————Program Start From Below—————————–

REPORT Z_KNA1_CUSTOMER.
TYPE-POOLS: VRM.

DATA: NAME TYPE VRM_ID,
LIST TYPE VRM_VALUES,
VALUE LIKE LINE OF LIST.

PARAMETERS: ENTITY(10) AS LISTBOX VISIBLE LENGTH 10.
PARAMETERS : OPATH TYPE STRING DEFAULT ‘\ServerNametmpKNA1.csv’.

AT SELECTION-SCREEN OUTPUT.

NAME = ‘ENTITY’.

VALUE-KEY = ‘UK01’.
VALUE-TEXT = ‘UK01’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK75’.
VALUE-TEXT = ‘UK75’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK21’.
VALUE-TEXT = ‘UK21’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK31’.
VALUE-TEXT = ‘UK31’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK41’.
VALUE-TEXT = ‘UK41’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK02’.
VALUE-TEXT = ‘UK02’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK73’.
VALUE-TEXT = ‘UK73’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK04’.
VALUE-TEXT = ‘UK04’.
APPEND VALUE TO LIST.

VALUE-KEY = ‘UK61’.
VALUE-TEXT = ‘UK61’.
APPEND VALUE TO LIST.

CALL FUNCTION ‘VRM_SET_VALUES’ EXPORTING ID = NAME VALUES = LIST.START-OF-SELECTION.

type-pools:TRUXS.
data: begin of itab occurs 0,
KUNNR like KNA1-KUNNR,
MANDT like KNA1-MANDT,
LAND1 like KNA1-LAND1,
MCOD1 like KNA1-MCOD1,
NAME1 like KNA1-NAME1,
NAME2 like KNA1-NAME2,
NAME3 like KNA1-NAME3,
STRAS like KNA1-STRAS,
ORT01 like KNA1-ORT01,
PSTLZ like KNA1-PSTLZ,
SPRAS like KNA1-SPRAS,
LZONE like KNA1-LZONE,
KTOKD like KNA1-KTOKD,
BUKRS like KNB1-BUKRS,
end of itab.

data: itab1 type TRUXS_T_TEXT_DATA.
SELECT KNA1~KUNNR
KNA1~MANDT
KNA1~LAND1
KNA1~MCOD1
KNA1~NAME1
KNA1~NAME2
KNA1~NAME3
KNA1~STRAS
KNA1~ORT01
KNA1~PSTLZ
KNA1~SPRAS
KNA1~LZONE
KNA1~KTOKD
KNB1~BUKRS
from KNA1 INNER JOIN KNB1 ON KNA1~KUNNR = KNB1~KUNNR
into table itab
WHERE KNB1~BUKRS = ENTITY.

CALL FUNCTION ‘SAP_CONVERT_TO_CSV_FORMAT’
EXPORTING
I_FIELD_SEPERATOR = ‘,’
TABLES
I_TAB_SAP_DATA = itab
CHANGING
I_TAB_CONVERTED_DATA = itab1
EXCEPTIONS
CONVERSION_FAILED = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = OPATH
TABLES
data_tab = itab1
EXCEPTIONS
OTHERS = 1.

 

Scroll to Top