Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

code help

Former Member
0 Likes
1,451

Hello all,

I am asked to create a program to extract some material information from R/3 system. The information are from several fields of tables MARM, MARA, MAKT..

Anyone can give me a sample code? The fields include Unit of Measure, material names, etc.

What's the logic?

Do I need to create an internal table? Select all the fields from tables and put them into the internal table, loop the internal table, output it to external table? where to output?

Thanks for help,

Linda

Message was edited by:

Linda Tseng

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,423

Hi Linda,

You could create an internal table that is comprised of all the fields you require. You need to determine from what tables the information is comming from and how to drill down to obtain the correct infomation.

logic:

select * (or single *) from MARM into table I_MARM where .....

select * (or single *) from MARA into table I_MARA where .....

select * (or single *) from MAKT into table I_MAKT where .....

once you have your internal tables filled....

LOOP AT I_MARM WHERE ....

READ TABLE I_MARA WITH KEY

VARIABLE = I_MARM-VARIABLE.

IF SY-SUBRC = 0. <--IF A RECORD IS FOUND

READ TABLE I_MAKT WITH KEY

VARIABLE = I_MARA-VARIABLE.

IF SY-SUBRC = 0.

MY_RESULTS-VAR1 = I_MARM-VARIABLE

MY_RESULTS-VAR2 = I_MARA-VARIABLE

MY_RESULTS-VAR3 = I_MAKT-VARIABLE

ENDIF.

ENDIF.

ENDLOOP.

hope this helps.

Warren

13 REPLIES 13
Read only

Former Member
0 Likes
1,423

create internal tables...get data from tbales...loop one table & read other tables with keys and get all required data into final internal table...Use this for eport output.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,423

Here is a sample program.



REPORT ZRICH_0003 . 

* Tables. 
TABLES: MARA. 

* Global ALV Data Declarations 
TYPE-POOLS: SLIS. 

* Internal Tables 
DATA: BEGIN OF ITAB OCCURS 0, 
      MATNR TYPE MARA-MATNR, 
      MAKTX TYPE MAKT-MAKTX, 
      END OF ITAB. 

DATA: FIELDCAT  TYPE SLIS_T_FIELDCAT_ALV. 

* Selection Screen 
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002 . 
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR. 
SELECTION-SCREEN END OF BLOCK B1. 

START-OF-SELECTION. 

  PERFORM GET_DATA. 
  PERFORM CALL_ALV. 

********************************************************************* 
*      Form  GET_DATA 
********************************************************************* 
FORM GET_DATA. 

  SELECT MARA~MATNR MAKT~MAKTX 
          INTO CORRESPONDING FIELDS OF TABLE ITAB 
               FROM MARA 
                 INNER JOIN MAKT 
                  ON MARA~MATNR = MAKT~MATNR 
                   WHERE MARA~MATNR IN S_MATNR 
                     AND MAKT~SPRAS = SY-LANGU. 

ENDFORM. 

************************************************************************ 
*  CALL_ALV 
************************************************************************ 
FORM CALL_ALV. 

  DATA: VARIANT TYPE  DISVARIANT. 

  VARIANT-REPORT = SY-REPID. 
  VARIANT-USERNAME = SY-UNAME. 

  PERFORM BUILD_FIELD_CATALOG. 

* Call ABAP List Viewer (ALV) 
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' 
       EXPORTING 
            IT_FIELDCAT = FIELDCAT 
            IS_VARIANT  = VARIANT 
            I_SAVE      = 'U' 
       TABLES 
            T_OUTTAB    = ITAB. 

ENDFORM. 

************************************************************************ 
* BUILD_FIELD_CATALOG 
************************************************************************ 
FORM BUILD_FIELD_CATALOG. 

  DATA: FC_TMP TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE. 
  DATA: LABEL(30) TYPE C. 

  CLEAR: FIELDCAT. REFRESH: FIELDCAT. 

  PERFORM UPDATE_CATALOG USING 'Material Number' 
                            'ITAB' 
                            'MATNR' 
                            '18' 
                            SPACE 
                            SPACE 
                            'L'. 

  PERFORM UPDATE_CATALOG USING 'Material Description' 
                            'ITAB' 
                            'MAKTX' 
                            '40' 
                            SPACE 
                            SPACE 
                            'L'. 


ENDFORM. 

************************************************************************ 
* UPDATE_CATALOG 
************************************************************************ 
FORM UPDATE_CATALOG USING COL_HEAD 
                          TABLE 
                          FIELD 
                          OUTPUTLEN 
                          DO_SUM 
                          NO_OUT 
                          JUST. 

  DATA: TMP_FC TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE. 

  TMP_FC-REPTEXT_DDIC = COL_HEAD. 
  TMP_FC-FIELDNAME    = FIELD. 
  TMP_FC-TABNAME      = TABLE. 
  TMP_FC-OUTPUTLEN    = OUTPUTLEN. 
  TMP_FC-JUST         = JUST. 
  TMP_FC-DECIMALS_OUT = 0. 
  TMP_FC-DO_SUM      = DO_SUM. 
  TMP_FC-NO_OUT      = NO_OUT. 
  APPEND TMP_FC TO FIELDCAT. 

ENDFORM.

Regards,

Rich Heilman

Read only

0 Likes
1,423

Hi Rich,

Could you please explain what function module 'REUSE_ALV_GRID_DISPLAY' does? Also what do BUILD_FIELD_CATALOG, UPDATE_CATALOG do in your sample code? Appreciate!

Linda

Read only

0 Likes
1,423

The function module is the ALV grid function module which gives you the nice output screen, which allows for sorting, filtering, and other things., BUILD_FIELDCATALOG is a form routine that builds the FC, which tells the ALV function module what columns it should display, usually it is the same columns as in the internal table with the data. UPDATE_CATALOG is actually just another form routine which abstracts the actually updating of the FC, you don't have to use it this way, you can simply update directly.

Regards,

Rich Heilman

Read only

0 Likes
1,423

Thank you very much!

Linda

Read only

Former Member
0 Likes
1,423

Hi,

Select all necessary info from MARA & MARM in one internal table joining matnr.

Then update material description from makt for the same matnr in the same internal table .

M in hurry so unable to give u details code.

Cheers...Bye

Read only

Former Member
0 Likes
1,424

Hi Linda,

You could create an internal table that is comprised of all the fields you require. You need to determine from what tables the information is comming from and how to drill down to obtain the correct infomation.

logic:

select * (or single *) from MARM into table I_MARM where .....

select * (or single *) from MARA into table I_MARA where .....

select * (or single *) from MAKT into table I_MAKT where .....

once you have your internal tables filled....

LOOP AT I_MARM WHERE ....

READ TABLE I_MARA WITH KEY

VARIABLE = I_MARM-VARIABLE.

IF SY-SUBRC = 0. <--IF A RECORD IS FOUND

READ TABLE I_MAKT WITH KEY

VARIABLE = I_MARA-VARIABLE.

IF SY-SUBRC = 0.

MY_RESULTS-VAR1 = I_MARM-VARIABLE

MY_RESULTS-VAR2 = I_MARA-VARIABLE

MY_RESULTS-VAR3 = I_MAKT-VARIABLE

ENDIF.

ENDIF.

ENDLOOP.

hope this helps.

Warren

Read only

0 Likes
1,423

Thanks for your quick responses. Let me try.

Linda

Read only

0 Likes
1,423

One question.

Based on the code, I extract the data out from those tables into the internal table. How to export the data from the internal table to a file? Is there a function module I can use? Thanks!

Linda

Read only

0 Likes
1,423

Yes, you can use the function module GUI_DOWNLOAD.

Also maybe you can reward points for helpful answers. You can do so by setting the radiobuttons next to each answer.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,423

You could also use:

OPEN DATASET P_OUTF FOR OUTPUT IN TEXT MODE.

LOOP AT I_RESULTS.

TRANSFER I_RESULTS TO P_OUTF.

ENDLOOP.

CLOSE DATASET P_OUTF.

**p_outf being the parameter for the output on your selection screen

**I_results being your internal table.

Read only

Former Member
0 Likes
1,423

Linda,

I should have mentioned earlier that you can use this to choose your file.

Place it between your initialization and start-of-selection.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_OUTF.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

FILE_NAME = P_OUTF.

Read only

0 Likes
1,423

Hi.

my question is kind if late.

Is there a specific format of the file name I want to save data to? I tried this code, but I always got error of no authorization? Thanks!

Linda