One of the drawbacks on SCN is that it isn’t so much beginner friendly. There are very little how-to documents and beginners questions are often locked because there are too basic.
So I came with an idea to start a series of documents called ABAP Beginners guide in which will describe in detail, along with complete code, some of the most often programming requirements.
In this first document we will create classical List report for comparing Customer and Vendor master data where one legal subject is both customer and vendor. Because of filling in two different transactions, this data is often inconsistent, so the goal of this report is to mark differences in data for the same legal subject that is both customer and vendor for one company code.
Tables that will be used are:
First, we are declaring database tables, internal table for data manipulation and some variables:
REPORT z_scn_cust_vend NO STANDARD PAGE HEADING LINE-SIZE 170. “this means that the list will be 170 characters wide
TABLES: kna1,
lfa1,
lfb1, “to connect vendors with company code
knb1, “to connect customers with company code
t001. “to get name of the company
DATA: BEGIN OF final OCCURS 0,
k_stcd1 LIKE kna1-stcd1,
k_stkzn LIKE kna1-stkzn,
k_kunnr LIKE kna1-kunnr,
k_anred LIKE kna1-anred,
k_name1 LIKE kna1-name1,
k_stras LIKE kna1-stras,
k_pstlz LIKE kna1-pstlz,
k_ort01 LIKE kna1-ort01,
k_stcd2 LIKE kna1-stcd2,
k_stcd3 LIKE kna1-stcd3,
k_stceg LIKEkna1-stceg,
l_stcd1 LIKE lfa1-stcd1,
l_stkzn LIKE lfa1-stkzn,
l_lifnr LIKE lfa1-lifnr,
l_anred LIKE lfa1-anred,
l_name1 LIKE lfa1-name1,
l_stras LIKE lfa1-stras,
l_pstlz LIKE lfa1-pstlz,
l_ort01 LIKE lfa1-ort01,
l_stcd2 LIKE lfa1-stcd2,
l_stcd3 LIKE lfa1-stcd3,
l_stceg LIKE lfa1-stceg.
DATA: END OF final.
DATA: cursor_field(50), field_value(50).
DATA rec LIKE final.
DATA comp TYPE t001-butxt.
DATA count(10) TYPE n VALUE '0'.
Next, we need selection screen for defining company code and two radio buttons to select if we look by customers for that company code or by vendors for that company code:
SELECTION-SCREEN BEGIN OF BLOCK first WITH FRAME TITLE text-001.
SELECT-OPTIONS: bukrs FOR lfb1-bukrs NO INTERVALS NO-EXTENSION.
PARAMETERS: r1 RADIOBUTTON GROUP rad1 DEFAULT 'X',
r2 RADIOBUTTON GROUP rad1.
SELECTION-SCREEN END OF BLOCK first.
Now, we do select statement for both cases:
IF r1 = 'X'.
SELECT kn~stcd1 AS k_stcd1
kn~stkzn AS k_stkzn
kn~kunnr AS k_kunnr
kn~anred AS k_anred
kn~name1 AS k_name1
kn~stras AS k_stras
kn~pstlz AS k_pstlz
kn~ort01 AS k_ort01
kn~stcd2 AS k_stcd2
kn~stcd3 AS k_stcd3
kn~stceg AS k_stceg
lf~stcd1 AS l_stcd1
lf~stkzn AS l_stkzn
lf~lifnr AS l_lifnr
lf~anred AS l_anred
lf~name1 AS l_name1
lf~stras AS l_stras
lf~pstlz AS l_pstlz
lf~ort01 AS l_ort01
lf~stcd2 AS l_stcd2
lf~stcd3 AS l_stcd3
lf~stceg AS l_stceg
INTO CORRESPONDING FIELDS OF TABLE final
FROM kna1 AS kn
INNER JOIN lfa1 AS lf
ON lf~lifnr = kn~lifnr
INNER JOIN lfb1 AS lb
ON lb~lifnr = lf~lifnr
WHERE lb~bukrs IN bukrs
AND kn~sperr = ''
AND lf~sperr = ''.
ELSE.
SELECT kn~stcd1 AS k_stcd1
kn~stkzn AS k_stkzn
kn~kunnr AS k_kunnr
kn~anred AS k_anred
kn~name1 AS k_name1
kn~stras AS k_stras
kn~pstlz AS k_pstlz
kn~ort01 AS k_ort01
kn~stcd2 AS k_stcd2
kn~stcd3 AS k_stcd3
kn~stceg AS k_stceg
lf~stcd1 AS l_stcd1
lf~stkzn AS l_stkzn
lf~lifnr AS l_lifnr
lf~anred AS l_anred
lf~name1 AS l_name1
lf~stras AS l_stras
lf~pstlz AS l_pstlz
lf~ort01 AS l_ort01
lf~stcd2 AS l_stcd2
lf~stcd3 AS l_stcd3
lf~stceg AS l_stceg
INTO CORRESPONDING FIELDS OF TABLE final
FROM kna1 AS kn
INNER JOIN lfa1 AS lf
ON lf~lifnr = kn~lifnr
INNER JOIN knb1 AS kb
ON kb~kunnr = kn~kunnr
WHERE kb~bukrs IN bukrs
AND kn~sperr = ''
AND lf~sperr = ''.
ENDIF.
SELECT SINGLE butxt INTO comp FROM t001
WHERE t001~bukrs IN bukrs.
And finally, we build our list for data display that will mark with red color those fields that are different for the same legal subject:
TOP-OF-PAGE.
WRITE: / 'Company name: ', comp.
WRITE: /1 'PIB', 16 'ID num.', 26 'Title', 38 'Name', 69 'Street & number', 98 'Postal code', 109 'City',
132 'Tax num.1' , 144 'Tax num.2' , 158 'VAT'.
ULINE.
START-OF-SELECTION.
LOOP AT final INTO rec.
count = count + 1.
IF rec-k_stcd1 <> rec-l_stcd1 OR rec-k_stkzn <> rec-l_stkzn
OR rec-k_anred <> rec-l_anred OR rec-k_name1 <> rec-l_name1
OR rec-k_stras <> rec-l_stras OR rec-k_pstlz <> rec-l_pstlz
OR rec-k_ort01 <> rec-l_ort01 OR rec-k_stcd2 <> rec-l_stcd2
OR rec-k_stcd3 <> rec-l_stcd3 OR rec-k_stceg <> rec-k_stceg.
IF rec-k_stcd1 <> rec-l_stcd1.
WRITE: rec-k_stcd1(14) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_stcd1(14).
ENDIF.
WRITE: rec-k_kunnr COLOR COL_GROUP.
IF rec-k_anred <> rec-l_anred.
WRITE: rec-k_anred(10) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_anred(10).
ENDIF.
IF rec-k_name1 <> rec-l_name1.
WRITE: rec-k_name1(30) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_name1(30).
ENDIF.
IF rec-k_stras <> rec-l_stras.
WRITE: rec-k_stras(30) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_stras(30).
ENDIF.
IF rec-k_pstlz <> rec-l_pstlz.
WRITE: rec-k_pstlz(8) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_pstlz(8).
ENDIF.
IF rec-k_ort01 <> rec-l_ort01.
WRITE: rec-k_ort01(22) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_ort01(22).
ENDIF.
IF rec-k_stcd2 <> rec-l_stcd2.
WRITE: rec-k_stcd2 COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_stcd2.
ENDIF.
IF rec-k_stcd3 <> rec-l_stcd3.
WRITE: rec-k_stcd3(13) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_stcd3(13).
ENDIF.
IF rec-k_stceg <> rec-k_stceg.
WRITE: rec-k_stceg(12) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-k_stceg(12).
ENDIF.
NEW-LINE.
IF rec-k_stcd1 <> rec-l_stcd1.
WRITE: rec-l_stcd1(14) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_stcd1(14).
ENDIF.
WRITE: rec-l_lifnr COLOR COL_GROUP.
IF rec-k_anred <> rec-l_anred.
WRITE: rec-l_anred(10) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_anred(10).
ENDIF.
IF rec-k_name1 <> rec-l_name1.
WRITE: rec-l_name1(30) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_name1(30).
ENDIF.
IF rec-k_stras <> rec-l_stras.
WRITE: rec-l_stras(30) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_stras(30).
ENDIF.
IF rec-k_pstlz <> rec-l_pstlz.
WRITE: rec-l_pstlz(8) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_pstlz(8).
ENDIF.
IF rec-k_ort01 <> rec-l_ort01.
WRITE: rec-l_ort01(22) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_ort01(22).
ENDIF.
IF rec-k_stcd2 <> rec-l_stcd2.
WRITE: rec-l_stcd2 COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_stcd2.
ENDIF.
IF rec-k_stcd3 <> rec-l_stcd3.
WRITE: rec-l_stcd3(13) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_stcd3(13).
ENDIF.
IF rec-k_stceg <> rec-k_stceg.
WRITE: rec-l_stceg(12) COLOR COL_NEGATIVE.
ELSE.
WRITE: rec-l_stceg(12).
ENDIF.
ULINE.
ENDIF.
ENDLOOP.
SHIFT count LEFT DELETING LEADING '0'.
WRITE: / 'Number of records: ', count.
If someone start to repair these inconsistent data, it would be ideally to do that through transactions FD02 – Change Customer and XK02 – Change Vendor, so we add shortcuts to these transactions on double click on ID number. Off course, clicked customer or vendor are already loaded in transaction.
AT LINE-SELECTION.
GET CURSOR FIELD cursor_field
VALUE field_value.
CASE cursor_field.
WHEN 'REC-K_KUNNR'.
READ LINE sy-lilli FIELD VALUE rec-k_kunnr.
CHECK NOT rec-k_kunnr IS INITIAL.
SET PARAMETER ID: 'KUN' FIELD rec-k_kunnr.
SET PARAMETER ID: 'BUK' FIELD bukrs-low.
CALL TRANSACTION 'FD02' AND SKIP FIRST SCREEN.
WHEN 'REC-L_LIFNR'.
READ LINE sy-lilli FIELD VALUE rec-l_lifnr.
CHECK NOT rec-l_lifnr IS INITIAL.
SET PARAMETER ID: 'LIF' FIELD rec-l_lifnr.
SET PARAMETER ID: 'BUK' FIELD bukrs-low.
CALL TRANSACTION 'XK02' AND SKIP FIRST SCREEN.
ENDCASE.
Only thing left to do is to create transaction for our report. Let’s name it ZCVD (like customer vendor differences). Resulting report looks like this:
In the next document, we will create simple procedural ALV report.
Thank you for reading.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |