Application Development 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: 

BDc session for MM02

Former Member
0 Kudos

Hi all,

I wrote a code for BDC session for tr code 'MM02', but that session is not creating

wit hthe following code. can anyone check the code.

REPORT zmara

NO STANDARD PAGE HEADING LINE-SIZE 255.

TABLES mara.

type-pools : truxs.

TYPES : BEGIN OF ty_mara,

matnr TYPE matnr,

meins TYPE meins,

maktx TYPE maktx,

END OF ty_mara.

DATA : bdctab LIKE bdcdata OCCURS 0 WITH HEADER LINE,

gt_mara TYPE TABLE OF ty_mara, gs_mara TYPE ty_mara.

DATA p_file1 TYPE string.

DATA li_tab_raw_data TYPE truxs_t_text_data.

PARAMETERS : p_file LIKE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

EXPORTING

static = 'X'

CHANGING

file_name = p_file.

  • p_file1 = p_file.

START-OF-SELECTION.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

  • I_FIELD_SEPERATOR =

i_line_header = 'X'

i_tab_raw_data = li_tab_raw_data

i_filename = p_file

TABLES

i_tab_converted_data = gt_mara

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.

*include bdcrecx1.

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

client = sy-mandt

  • DEST = FILLER8

group = 'ZMARA'

  • HOLDDATE = FILLER8

keep = 'X'

user = sy-uname

  • RECORD = FILLER1

  • PROG = SY-CPROG

  • DCPFM = '%'

  • DATFM = '%'

  • 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.

*

LOOP AT gt_mara INTO gs_mara.

perform bdc_dynpro using 'SAPLMGMM' '0060'.

perform bdc_field using 'BDC_CURSOR'

'RMMG1-MATNR'.

perform bdc_field using 'BDC_OKCODE'

'/00'.

perform bdc_field using 'RMMG1-MATNR'

gs_mara-matnr.

perform bdc_dynpro using 'SAPLMGMM' '0070'.

perform bdc_field using 'BDC_CURSOR'

'MSICHTAUSW-DYTXT(02)'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_field using 'MSICHTAUSW-KZSEL(02)'

'X'.

perform bdc_dynpro using 'SAPLMGMM' '4004'.

perform bdc_field using 'BDC_OKCODE'

'=BU'.

perform bdc_field using 'BDC_CURSOR'

'MAKT-MAKTX'.

perform bdc_field using 'MAKT-MAKTX'

gs_mara-maktx.

CALL FUNCTION 'BDC_INSERT'

EXPORTING

tcode = 'MM02'

  • POST_LOCAL = NOVBLOCAL

  • PRINTING = NOPRINT

  • SIMUBATCH = ' '

  • CTUPARAMS = ' '

TABLES

dynprotab = bdctab

  • 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.

else.

write : 'Record is updated'.

ENDIF.

ENDLOOP.

*perform bdc_transaction using 'MM01'.

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.

*perform close_group.

&----


*& Form BDC_DYNPRO

&----


  • text

----


  • -->P_0178 text

  • -->P_0179 text

----


FORM bdc_dynpro USING value(p_scr)

value(p_dyn).

bdctab-program = p_scr.

bdctab-dynpro = p_dyn.

bdctab-dynbegin = 'X'.

APPEND bdctab.

CLEAR bdctab.

ENDFORM. " BDC_DYNPRO

&----


*& Form BDC_FIELD

&----


  • text

----


  • -->P_0183 text

  • -->P_0184 text

----


FORM bdc_field USING value(p_fnam)

value(p_fval).

bdctab-fnam = p_fnam.

bdctab-fval = p_fval.

APPEND bdctab.

CLEAR bdctab.

ENDFORM. " BDC_FIELD

Edited by: varisetty madhavi on Jun 2, 2009 8:27 AM

4 REPLIES 4

Former Member
0 Kudos

Hi,

For MM02, try to use BAPI 'BAPI_MATERIAL_SAVEDATA'.

Regards,

Ramesh.

Former Member
0 Kudos

Hi V.Madhav,

Note : Structure truxs does not existed in ECC 6.0.

Steps :

Go to the transaction SHDB in another session ( you can also give TCODE - OSHDB in transaction code window and hit enter ).

1.Create you transaction recording in SHDB . Ensure your transaction recording takes care of input values to all the

fields to which you intend to pass values in the BDC.

2.Once your recording is done correctly create the program from recording in the SHDB transction . You will find the button to create program in SHDB . Choose the option of creating from file when you proceed through the subsequent steps.

3. Define data type t_upload and structure wa_upload in the template program with fields of structure 'RECORD' from the program generated using SHDB recording ( replace the field1, field2 and so on as per your requirement). For your understanding you can remove the suffixes to the

field name but keep the field size unchanged.

4.Copy the coding existing between 'do' and 'enddo' statement from the generated program . Insert the copied code between the loop and endloop code of form 'Do_transaction '. Replace fields of structure 'RECORD' with respective fields of structure wa_upload.Insert constant values wherever possible in transaction recording.You can also handle customised data conversions here.

5.Replace 'TCODE' in the template program with the transaction code you intend to process in this BDC.

6.Please carry out further syntax check and resolve the related issues.

This program will provide for input help to select upload file from local machine. The file needs to be in Tab delimited format and is assumed to have first row as column headers .

On successful creation of session you will be prompted with a Information popup giving the name of session ,and will take you to the SM35 transaction to process your session

Check these program :

REPORT

NO STANDARD PAGE HEADING

LINE-SIZE 200

LINE-COUNT 300.

*--


DATA DECLARATION--


*---Types

DATA : BEGIN OF t_upload,

FIELD1(10),

FIELD2(2),

FIELD3(18),

FIELD4(35),

END OF t_upload.

*--- Tables

DATA : BEGIN OF i_bdcdata OCCURS 0."to hold the transaction record

INCLUDE STRUCTURE bdcdata.

DATA: END OF i_bdcdata.

DATA: i_upload LIKE STANDARD TABLE OF t_upload," to hold file data.

i_upload1 LIKE STANDARD TABLE OF t_upload." to hold file data.

*--- Work Areas

DATA: wa_upload2 LIKE t_upload,

wa_upload LIKE t_upload,

wa_upload1 LIKE t_upload.

*--- Variables

DATA: v_count1(4) TYPE n,

v_error TYPE c,

v_session(12),

v_field(21) TYPE c,

v_message(60) type 'C'.

*--Constants

DATA: c_open TYPE c VALUE '(',

c_close TYPE c VALUE ')',

c_x TYPE c VALUE 'X'.

*---Initialisation

initialization.

refresh : i_upload , i_upload1 ,i_bdcdata.

-------Selection Screen Design -


*Selection screen for input of upload file address

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME.

PARAMETERS : p_file LIKE rlgrap-filename OBLIGATORY.

SELECTION-SCREEN END OF BLOCK blk1.

---AT SELECTION SCREEN -


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

*--For popup to select file.

PERFORM give_help.

-----START OF SELECTION -


START-OF-SELECTION.

*--Data upload using WS_Upload.

PERFORM get_data.

*-- OPEN SESSION

PERFORM open_group.

*--Insert transactions using BDCDATA table in the session.

PERFORM do_transaction .

*-- Close the session.

PERFORM close_group.

END-OF-SELECTION.

&----


*& Form f_get_data

&----


  • For data upload from external file.

----


FORM get_data.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = p_file

filetype = 'DAT'

TABLES

data_tab = i_upload

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.

ELSE.

DELETE I_UPLOAD INDEX 1.

ENDIF.

ENDFORM. " f_get_data

&----


*& Form F_open_group

&----


  • To open session in session management.

----


FORM open_group.

v_session = 'TCODE'.

CALL FUNCTION 'BDC_OPEN_GROUP'

EXPORTING

client = sy-mandt

group = v_session

user = sy-uname

keep = 'X'.

ENDFORM. " F_open_group

&----


*& Form f_do_transaction

&----


  • Insert transactions in session after passing values to BDCDATA

----


FORM do_transaction.

LOOP AT i_upload INTO wa_upload .

*---- insert your generated codes from recording at SHDB here

*----- insertion ends

perform bdc_transaction using 'TCODE'.

REFRESH : I_BDCDATA.

CLEAR : WA_UPLOAD.

  • ENDIF.

ENDLOOP.

ENDFORM. " f_do_transaction

&----


*& Form bdc_dynpro

&----


  • For appending screen details to BDCDATA

----


FORM bdc_dynpro USING program dynpro.

CLEAR i_bdcdata.

i_bdcdata-program = program.

i_bdcdata-dynpro = dynpro.

i_bdcdata-dynbegin = 'X'.

APPEND i_bdcdata.

CLEAR i_bdcdata.

ENDFORM. "bdc_dynpro

&----


*& Form bdc_field

&----


  • For appending field details to bdcdata table

----


FORM bdc_field USING fnam fval.

CLEAR i_bdcdata.

i_bdcdata-fnam = fnam.

i_bdcdata-fval = fval.

APPEND i_bdcdata.

CLEAR i_bdcdata.

ENDFORM. " bdc_field

&----


*& Form bdc_transaction

&----


  • For inserting Transaction in the session

----


FORM bdc_transaction USING tcode.

CALL FUNCTION 'BDC_INSERT'

EXPORTING

tcode = tcode

TABLES

dynprotab = i_bdcdata.

ENDFORM. " bdc_transaction

&----


*& Form F_close_group

&----


  • For closing the session created in Session manager SM35

----


FORM close_group.

CALL FUNCTION 'BDC_CLOSE_GROUP'.

concatenate 'Session ' v_session 'successfully created' into v_field.

MESSAGE v_field type 'I'..

CALL TRANSACTION 'SM35'.

ENDFORM. "f_close_group

&----


*& Form f_give_help

&----


  • For user help to select file

----


FORM give_help.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

mask = ',.,..'

mode = 'O'

IMPORTING

filename = p_file

EXCEPTIONS

inv_winsys = 1

no_batch = 2

selection_cancel = 3

selection_error = 4

OTHERS = 5.

IF sy-subrc <> 0 AND NOT sy-msgty IS INITIAL.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " f_give_help

Reg,

Siva

0 Kudos

Hi,

Please search in SDN or debug thoroughly before posting such huge code in to SDN.

Thanks,

Manjula.S

Former Member
0 Kudos

hi

For ur Reference

try this example (updating Safety Stock field in MRP2 view)

BDC Recording

go to tcode : SHDB, click on new recording

in create recording enter the Recording name & the Tcode that u want to record

eg : MM02 ,then click on start recording

It goes to MM02 enter the material no and press enter

now u can see the view list in that select Basic data1 view and the MRP2 view

and click tick mark,it will ask for Organization levels(Plant & storage loc) enter them

and again click on tick mark

Now u can see the Overview screen with tabs(views) Basic data1,basic data2,sales org1,sales org2

on the Right hand side (end) u can see a button (last button) in the same line of the tabs

if u click on it u can again see the view list(with basic data1 view already selected with a tick mark)

then what u do is select the MRP2 view, what happens here is it

will take u directly to the MRP2 view from Basic data1 view without navigating through the other views

and more over if u do it by this way each and every view has a length of 4 (4 digit code)

here in this case MRP2 view has 4digit code = SP12

capture this in ur recording.Save it and code it through the program.

Now what ever may order of the views for different materials ur

program will work fine bcz u have captured the 4digit code of the view.

Regards