Friday, November 23, 2007

sap abap bdc Program Loads the Material Assignment of Routings

Okay this is how it works:

When you try to create, you would find a pushbutton 'INSERT NEW LINES' or something simialr to enter your data in the next line. Say you're entering the Material inthe first row, its field position would be MARA-MATNR(01). Now when you click the Insert Pushbitton the cursor always is at positon 2. so you have to loop the remaining data to enter in MARA-MATNR(02). Yougenereally set a counter and pass that counter value (in this case the counter value is 2 always)

The other case when you don't have a push button to Insert. Enter all the rows and then do a Page down. Now your cursor would sit back at position 2 again. Say if there are 20 rows in the first screen. You would keep incrementing the counter and then when it is 21 you do a pagedown and then reset the counter to 2. You loop the pagedown and in it you loop the counter.

I have attached a BDC where I use the second case (no push button). Do a recording and then you would know all the answers by yourself.

*&---------------------------------------------------------------------*
*& REPORT ZPP0122 *
*&---------------------------------------------------------------------*
*& Module : PP |
*& Application : The program loads the Material Assignment of Routings |
*& |
*&---------------------------------------------------------------------*
REPORT zpp0122 NO STANDARD PAGE HEADING
MESSAGE-ID z0
LINE-SIZE 132
LINE-COUNT 65(2).



*----------------------------------------------------------------------*
* Internal Tables *
*----------------------------------------------------------------------*


*Internal table for the Routing fields.

DATA: BEGIN OF i_rout OCCURS 0,

plnnr(8),
plnal(2),
matnr(18),
werks(4),

END OF i_rout.


DATA:
g_my_rec_in LIKE i_rout.

** Declare internal table for Call Transaction and BDC Session

DATA: i_bdc_table LIKE bdcdata OCCURS 0 WITH HEADER LINE.


*----------------------------------------------------------------------*
* Global Variables *
*----------------------------------------------------------------------*

*
DATA: g_counter(2) TYPE n,
g_field_name(18) TYPE c,
zc_yes TYPE syftype VALUE 'X'.

*----------------------------------------------------------------------*
* Selection Screen *
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK a WITH FRAME TITLE text-001.

PARAMETERS: p_fname1 TYPE localfile .

SELECTION-SCREEN SKIP 1.

SELECTION-SCREEN BEGIN OF BLOCK b WITH FRAME TITLE text-002.
PARAMETERS: p_rloc1 AS CHECKBOX DEFAULT 'X'.


SELECTION-SCREEN BEGIN OF BLOCK c WITH FRAME TITLE text-005.
PARAMETERS p_group(12) OBLIGATORY DEFAULT 'ZROUTING'.

SELECTION-SCREEN END OF BLOCK c.
SELECTION-SCREEN END OF BLOCK b.
SELECTION-SCREEN END OF BLOCK a.

**WRITE the report header
TOP-OF-PAGE.
INCLUDE zheading.

*----------------------------------------------------------------------*
* Start of selection *
*----------------------------------------------------------------------*

START-OF-SELECTION.

* Load Input file
PERFORM f_load_input_file.

* Create BDC records.
PERFORM create_bdc_records .


*&---------------------------------------------------------------------*
*& Form Create_BDC_records
*&---------------------------------------------------------------------*
* perform the BDC for the records in the internal table
*----------------------------------------------------------------------*

FORM create_bdc_records .

IF NOT i_rout[] IS INITIAL.

** Open BDC session

PERFORM open_bdc_session.

LOOP AT i_rout.

g_my_rec_in = i_rout.


AT NEW plnnr.

CLEAR i_bdc_table[].

PERFORM insert_screen_header.

ENDAT.


CONCATENATE 'MAPL-PLNAL(' g_counter ')' INTO g_field_name.

PERFORM bdc_field USING g_field_name i_rout-plnal.

CONCATENATE 'MAPL-MATNR(' g_counter ')' INTO g_field_name.

PERFORM bdc_field USING g_field_name i_rout-matnr.

CONCATENATE 'MAPL-WERKS(' g_counter ')' INTO g_field_name.

PERFORM bdc_field USING g_field_name i_rout-werks.

PERFORM bdc_field USING 'BDC_OKCODE' '/00'.

g_counter = g_counter + 1.


* Page Down for further entries

IF g_counter = 19.

PERFORM bdc_field USING 'BDC_OKCODE' '=P+'.

PERFORM bdc_dynpro USING 'SAPLCZDI' '1010'.

g_counter = 2.

ENDIF.


AT END OF plnnr.

PERFORM bdc_field USING 'BDC_OKCODE' '=BACK'.

PERFORM bdc_dynpro USING 'SAPLCPDI' '1200'.

PERFORM bdc_field USING 'BDC_OKCODE' '=BU'.

PERFORM insert_bdc_new.

ENDAT.

ENDLOOP.

CLEAR i_rout[].

PERFORM close_bdc_session.

** Release the BDC sessions created
PERFORM release_bdc.

ENDIF.


ENDFORM. " open_group


*&---------------------------------------------------------------------*
*& Form bdc_dynpro_start
*&---------------------------------------------------------------------*
* Call the screen for the input of fields
*----------------------------------------------------------------------*
* -->P_G_PROGRAM_1
* -->P_G_SCREEN
*----------------------------------------------------------------------*
FORM bdc_dynpro USING p_g_program_1
p_g_screen.

CLEAR i_bdc_table.
i_bdc_table-program = p_g_program_1.
i_bdc_table-dynpro = p_g_screen.
i_bdc_table-dynbegin = 'X'.
APPEND i_bdc_table.

ENDFORM. " bdc_dynpro_start

*&---------------------------------------------------------------------*
*& Form bdc_field
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*

FORM bdc_field USING f_name f_value.
* IF f_value <> space.
CLEAR i_bdc_table.
i_bdc_table-fnam = f_name.
i_bdc_table-fval = f_value.
APPEND i_bdc_table.
* ENDIF.
ENDFORM. "bdc_insert_field


*&--------------------------------------------------------------------*
*& Form open_bdc_session
*&--------------------------------------------------------------------*
* Create the BDC session
*---------------------------------------------------------------------*
FORM open_bdc_session .


** Open BDC session and creat and update condition records

CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = sy-mandt
* DEST = FILLER8
group = p_group
* HOLDDATE = FILLER8
keep = 'X'
user = sy-uname
* RECORD = FILLER1
* PROG = SY-CPROG
* IMPORTING
* QID =
EXCEPTIONS
client_invalid = 1
destination_invalid = 2
group_invalid = 3
group_is_locked = 4
holddate_invalid = 5
internal_error = 6
queue_error = 7
running = 8
system_lock_error = 9
user_invalid = 10
OTHERS = 11
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

ENDFORM. " create_bdc_session

*&---------------------------------------------------------------------*
*& Form insert_screen_header
*&---------------------------------------------------------------------*
* Header Data inserted
*----------------------------------------------------------------------*

FORM insert_screen_header .

g_counter = 1.

* First screen

PERFORM bdc_dynpro USING 'SAPLCPDI' '1010'.

PERFORM bdc_field USING 'BDC_CURSOR' 'RC271-PLNNR'.

PERFORM bdc_field USING 'BDC_OKCODE' '=ALUE'.

PERFORM bdc_field USING 'RC271-PLNNR' g_my_rec_in-plnnr.

PERFORM bdc_field USING 'RC27M-MATNR' ' '.

PERFORM bdc_field USING 'RC27M-WERKS' ' '.

PERFORM bdc_field USING 'RC271-PLNAL' ' '.

*next screen

PERFORM bdc_dynpro USING 'SAPLCPDI' '1200'.

PERFORM bdc_field USING 'BDC_OKCODE' '=MTUE'.

*next screen

PERFORM bdc_dynpro USING 'SAPLCZDI' '1010'.

ENDFORM. " insert_screen_header

*&---------------------------------------------------------------------*
*& Form insert_bdc
*&---------------------------------------------------------------------*
* Insert the BDC for the transaction
*----------------------------------------------------------------------*

FORM insert_bdc_new .

CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = 'CA02'
* POST_LOCAL = NOVBLOCAL
* PRINTING = NOPRINT
* SIMUBATCH = ' '
* CTUPARAMS = ' '
TABLES
dynprotab = i_bdc_table
EXCEPTIONS
internal_error = 1
not_open = 2
queue_error = 3
tcode_invalid = 4
printing_invalid = 5
posting_invalid = 6
OTHERS = 7
.
IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.


ENDIF.

CLEAR i_bdc_table[].


ENDFORM. " insert_bdc


*&---------------------------------------------------------------------*
*& Form close_bdc_session
*&---------------------------------------------------------------------*
* Close the session
*----------------------------------------------------------------------*

FORM close_bdc_session .

CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
not_open = 1
queue_error = 2
OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " close_bdc_session


*&---------------------------------------------------------------------*
*& Form f_load_input_file
*&---------------------------------------------------------------------*
* Load the data file
*----------------------------------------------------------------------*

FORM f_load_input_file.


* The data file is from Presentation server

IF p_rloc1 = zc_yes.

CALL FUNCTION 'WS_UPLOAD'
EXPORTING
filename = p_fname1
filetype = 'DAT'
TABLES
data_tab = i_rout
EXCEPTIONS
conversion_error = 1
file_open_error = 2
file_read_error = 3
invalid_type = 4
no_batch = 5
unknown_error = 6
invalid_table_width = 7
gui_refuse_filetransfer = 8
customer_error = 9
OTHERS = 10.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.

ENDIF.

ENDIF.

ENDFORM. " f_load_input_file

*&---------------------------------------------------------------------*
*& Form release_bdc
*&---------------------------------------------------------------------*
* Release BDC session
*----------------------------------------------------------------------*

FORM release_bdc.

SUBMIT rsbdcsub WITH mappe EQ p_group
WITH von EQ sy-datum
WITH bis EQ sy-datum
WITH fehler EQ '.'
EXPORTING LIST TO MEMORY
AND RETURN.

ENDFORM.

No comments:

Blog Archive