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

how can i handle errors in bdc - call transaction method using tcode xk01

Former Member
0 Likes
1,818

Hi,

i want to know how can i handle errors in call transaction method using tcode xk01. I have written a program in se38 but its not working.

TYPES : BEGIN OF itab,

lifnr TYPE rf02k-lifnr,

ktokk TYPE rf02k-ktokk,

END OF itab.

DATA : i_tab TYPE TABLE OF itab INITIAL SIZE 0,

wa_tab TYPE itab.

DATA : i_bdc TYPE TABLE OF bdcdata INITIAL SIZE 0,

wa_bdc TYPE bdcdata.

DATA : i_message LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE,

wa_message TYPE bdcmsgcoll.

DATA : fname TYPE string VALUE 'd:\manish.txt'.

DATA: p_text(100) TYPE c.

START-OF-SELECTION.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = fname

filetype = 'ASC'

has_field_separator = 'X'

TABLES

data_tab = i_tab.

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 i_tab INTO wa_tab.

PERFORM bdc_dynpro USING 'SAPMF02K' '0100'.

PERFORM bdc_field USING 'RF02K-LIFNR' wa_tab-lifnr.

PERFORM bdc_field USING 'RF02K-KTOKK' wa_tab-ktokk.

PERFORM bdc_field USING 'BDC_OKCODE' '= AUSW'.

CALL TRANSACTION 'XK01' USING i_bdc MODE 'N'. " UPDATE 'S' MESSAGES

"INTO i_message.

REFRESH i_bdc.

ENDLOOP.

IF sy-subrc NE 0.

READ TABLE i_message WITH KEY msgtyp = 'E'.

IF sy-subrc = 0.

LOOP AT i_message INTO wa_message.

CALL FUNCTION 'FORMAT_MESSAGE'

EXPORTING

id = i_message-msgid

lang = 'EN'

no = i_message-msgnr

v1 = i_message-msgv1

v2 = i_message-msgv2

v3 = i_message-msgv3

v4 = i_message-msgv4

IMPORTING

msg = p_text

EXCEPTIONS

not_found = 1

OTHERS = 2.

WRITE:/ p_text.

ENDLOOP.

ENDIF.

ENDIF.

&----


*& Form bdc_dynpro

&----


  • text

----


  • -->PROGRAM text

  • -->DYNPRO text

----


FORM bdc_dynpro USING program dynpro.

CLEAR wa_bdc.

wa_bdc-program = program.

wa_bdc-dynpro = dynpro.

wa_bdc-dynbegin = 'X'.

APPEND wa_bdc TO i_bdc.

ENDFORM. "bdc_dynpro

&----


*& Form bdc_field

&----


  • text

----


  • -->FNAM text

  • -->FVAL text

----


1 ACCEPTED SOLUTION
Read only

dev_parbutteea
Active Contributor
0 Likes
1,292

Hi,

modify the codes as mentioned above and check each loop in debug mode and verify whether each call transaction is successful or not!

Regards.

9 REPLIES 9
Read only

Former Member
0 Likes
1,292

Hi manish

you said it 's not working . What do you mean ? Please give me a detail .

Regards

Wiboon

Read only

0 Likes
1,292

when i execute the program its not showing any errors log in sm37.

u suggest what should i do.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,292

Hi,

you should treat each call transaction's message within the loop itself.

LOOP AT i_tab INTO wa_tab.

PERFORM bdc_dynpro USING 'SAPMF02K' '0100'.

PERFORM bdc_field USING 'RF02K-LIFNR' wa_tab-lifnr.

PERFORM bdc_field USING 'RF02K-KTOKK' wa_tab-ktokk.

PERFORM bdc_field USING 'BDC_OKCODE' '= AUSW'.

<b>CALL TRANSACTION 'XK01'

USING i_bdc

MODE 'N'

UPDATE 'S' MESSAGES

INTO i_message.

IF sy-subrc NE 0.

READ TABLE i_message WITH KEY msgtyp = 'E'.

IF sy-subrc = 0.

CALL FUNCTION 'FORMAT_MESSAGE'

EXPORTING

id = i_message-msgid

lang = 'EN'

no = i_message-msgnr

v1 = i_message-msgv1

v2 = i_message-msgv2

v3 = i_message-msgv3

v4 = i_message-msgv4

IMPORTING

msg = p_text

EXCEPTIONS

not_found = 1

OTHERS = 2.

WRITE:/ p_text.

ENDIF.</b>

REFRESH i_bdc.

REFRESH i_message..

ENDLOOP.

Regards.

Read only

0 Likes
1,292

even after making changes i cant find any result what should i do?

Read only

Former Member
0 Likes
1,292

Hi,

Check the following link:

http://sap-img.com/abap/retreive-error-message-from-bdc.htm

Regards,

Bhaskar

Read only

Former Member
0 Likes
1,292

hi manish,

loop the internal table i_message.

and in the loop u are using FORMAT_MESSAGE fm

it will give some return value in p_text.

for each rec. whether it is succcessful/ error the info will be stored in p_text.

append this info into another internal table.

loop this internal table outside the loop of i_message int table.

validate the message id.

if msgid eq 'E'.

store that rec into eror internal table.

and using that error internal table reprocess the error rec.

<b>Reward points if useful</b>

Chandra

Read only

Former Member
0 Likes
1,292

Hi,

Below is BDC using call transaction for TCode : MR21 (MAterial price chnage). Change your code exactly with this, as I used Error handling in this BDC and even it is working properly.

************************************************************************

*PROGRAM ID : ZMATERIAL_PRICE_CHANGE

*AUTHOR :

*DESCRIPTION : BDC PROGRAM TO CHANGE THE MATERIAL EXISTING PRICE

*TRANSACTION : MR21

*PROJECT :

*VERSION : 1.0

*REQUEST NO :

************************************************************************

report ZMATERIAL_PRICE_CHANGE

no standard page heading line-size 255.

TABLES : T100.

DATA : BEGIN OF IT_MATERIAL OCCURS 0,

INDEX(4), "Index Number

BUDAT(10), "Posting Date

BUKRS(4), " Company Code

WERKS(4), "Plant Number

XBLNR(16), "Reference

BKTXT(25), "Doc. Header Text

MATNR(18), "Material Number

BWTAR(10), "Valuation Type

NEWVALPR(11), " Valuation New Price

NEWPEINH(5), " New Price Per Unit

ER_MESSAGE(100), "Error Message

END OF IT_MATERIAL.

  • Internal table to get the error data

DATA : it_error like IT_MATERIAL occurs 0 with header line.

*Internal table for Batch input: New table field structure

DATA : BDCDATA like BDCDATA occurs 0 with header line.

*Internal table for message

DATA : i_msgtab like bdcmsgcoll occurs 0 with header line.

************************************************************************

  • Internal table to find the error from the legacy data *

************************************************************************

DATA : begin of it_erfind occurs 0,

INDEX(10), "INDEX Number

er_message(100), "For Error Message

end of it_erfind.

************************************************************************

  • Variables declaration *

************************************************************************

DATA : g_message(200),

time(10),

date(10) ,

v_error_filename like RLGRAP-FILENAME.

date = sy-datum.

time = sy-uzeit.

************************************************************************

*Initialization

************************************************************************

initialization.

*Generating Error file name with date and time.

perform make_file_name.

************************************************************************

*Selection Screen

************************************************************************

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

parameter: p_file like IBIPPARMS-PATH obligatory.

SELECTION-SCREEN:END OF BLOCK B1.

at selection-screen ON VALUE-REQUEST FOR p_file .

perform get_filename.

************************************************************************

*START OF SELECTION

************************************************************************

start-of-selection.

perform upload_data.

perform fill_data.

end-of-selection.

********************************************************************

  • Uploading data file to internal table. *

********************************************************************

FORM upload_data.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = p_file

FILETYPE = 'DAT'

TABLES

DATA_TAB = IT_MATERIAL

EXCEPTIONS

CONVERSION_ERROR = 1

INVALID_TABLE_WIDTH = 2

INVALID_TYPE = 3

NO_BATCH = 4

UNKNOWN_ERROR = 5

GUI_REFUSE_FILETRANSFER = 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.

ENDFORM. "upload_data

&----


*& Form fill_data

&----


  • text

----


FORM fill_data.

loop at IT_MATERIAL.

REFRESH BDCDATA.

perform bdc_dynpro using 'SAPRCKM_MR21' '0201'.

perform bdc_field using 'BDC_CURSOR'

'MR21HEAD-BKTXT'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_field using 'MR21HEAD-BUDAT'

IT_MATERIAL-BUDAT.

perform bdc_field using 'MR21HEAD-BUKRS'

IT_MATERIAL-BUKRS.

perform bdc_field using 'MR21HEAD-WERKS'

IT_MATERIAL-WERKS.

perform bdc_field using 'MR21HEAD-XBLNR'

IT_MATERIAL-XBLNR.

perform bdc_field using 'MR21HEAD-BKTXT'

IT_MATERIAL-XBLNR.

perform bdc_field using 'MR21HEAD-SCREEN_VARIANT'

'MR21_LAGERMATERIAL_0250'.

perform bdc_dynpro using 'SAPRCKM_MR21' '0201'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_field using 'BDC_CURSOR'

'CKI_MR21_0250-BWTAR(01)'.

perform bdc_field using 'MR21HEAD-SCREEN_VARIANT'

'MR21_LAGERMATERIAL_BWKEY_0250'.

perform bdc_field using 'CKI_MR21_0250-MATNR(01)'

IT_MATERIAL-MATNR.

perform bdc_field using 'CKI_MR21_0250-BWTAR(01)'

IT_MATERIAL-BWTAR.

perform bdc_dynpro using 'SAPRCKM_MR21' '0201'.

perform bdc_field using 'BDC_OKCODE'

'=ENTR'.

perform bdc_field using 'BDC_CURSOR'

'CKI_MR21_0250-NEWPEINH(01)'.

perform bdc_field using 'MR21HEAD-SCREEN_VARIANT'

'MR21_LAGERMATERIAL_BWKEY_0250'.

perform bdc_field using 'CKI_MR21_0250-NEWVALPR(01)'

IT_MATERIAL-NEWVALPR.

perform bdc_field using 'CKI_MR21_0250-NEWPEINH(01)'

IT_MATERIAL-NEWPEINH.

perform bdc_dynpro using 'SAPRCKM_MR21' '0201'.

perform bdc_field using 'BDC_OKCODE'

'=SAVE'.

perform bdc_field using 'BDC_CURSOR'

'CKI_MR21_0250-MATNR(02)'.

perform bdc_field using 'MR21HEAD-SCREEN_VARIANT'

'MR21_LAGERMATERIAL_BWKEY_0250'.

perform bdc_transaction tables i_msgtab using 'MR21' 'A' 'L'.

************************************************************************

*To fetch the error message from the standard error table

************************************************************************

select single * from T100 where SPRSL = 'E'

and ARBGB = SY-MSGID

and MSGNR = SY-MSGNO.

G_MESSAGE = T100-TEXT.

***********************************************************************

*subroutine to change the error message for every document number

***********************************************************************

perform REPLACE_PARAMETERS using SY-MSGV1

SY-MSGV2

SY-MSGV3

SY-MSGV4

changing G_MESSAGE.

write: / 'System variables:'.

skip.

write: / ' Sy-msgty:', SY-MSGTY.

write: / ' Sy-msgid:', SY-MSGID.

write: / ' Sy-msgno:', SY-MSGNO.

write: / ' Sy-msgv1:', SY-MSGV1.

write: / ' Sy-msgv2:', SY-MSGV2.

write: / ' Sy-msgv3:', SY-MSGV3.

write: / ' Sy-msgv4:', SY-MSGV4.

skip.

write: / 'Message:'.

skip.

write: / SY-MSGTY, G_MESSAGE.

************************************************************************

*To find out the error in the legacy data if there is anything and pass

*the document no with error message to the seperate internal table

*called it_erfind

************************************************************************

if sy-msgty = 'E'.

it_erfind-INDEX = IT_MATERIAL-INDEX.

it_erfind-er_message = G_MESSAGE.

append it_erfind.

clear it_erfind.

endif.

*Finally we are segregating the error and downloading the error data.

----


at last.

*To segregate the error

----


perform segregate_error.

*To download the error from it_error internal table with err mesg

----


perform error_download.

perform display_message.

endat.

endloop.

clear IT_MATERIAL.

ENDFORM. "fill_data

*Subrotine to replace the parameters

----


FORM REPLACE_PARAMETERS USING P_PAR_1 P_PAR_2 P_PAR_3

P_PAR_4 CHANGING P_MESSAGE.

  • erst mal pruefen, ob numerierte Parameter verwendet wurden

----


DO.

REPLACE '&1' WITH P_PAR_1 INTO P_MESSAGE.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

DO.

REPLACE '&2' WITH P_PAR_2 INTO P_MESSAGE.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

DO.

REPLACE '&3' WITH P_PAR_3 INTO P_MESSAGE.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

DO.

REPLACE '&4' WITH P_PAR_4 INTO P_MESSAGE.

IF SY-SUBRC <> 0.

EXIT.

ENDIF.

ENDDO.

  • falls keine numerierten Parameter vorh., ersetzen wie gehabt

----


REPLACE '&' WITH P_PAR_1 INTO P_MESSAGE.

CONDENSE P_MESSAGE.

IF SY-SUBRC EQ 0.

REPLACE '&' WITH P_PAR_2 INTO P_MESSAGE.

CONDENSE P_MESSAGE.

IF SY-SUBRC EQ 0.

REPLACE '&' WITH P_PAR_3 INTO P_MESSAGE.

CONDENSE P_MESSAGE.

IF SY-SUBRC EQ 0.

REPLACE '&' WITH P_PAR_4 INTO P_MESSAGE.

CONDENSE P_MESSAGE.

ENDIF.

ENDIF.

ENDIF.

ENDFORM. "REPLACE_PARAMETERS

*Subroutine to segregate the error data from the legacy data

----


FORM segregate_error.

loop at it_erfind.

loop at IT_MATERIAL where INDEX = it_erfind-INDEX.

move-corresponding IT_MATERIAL to it_error.

it_error-er_message = it_erfind-er_message.

append it_error.

clear it_error.

endloop.

endloop.

ENDFORM. "segregate_error

********************************************************************

*Subroutine to download the error data from the it_error table.

********************************************************************

FORM error_download.

if it_error[] is not initial.

call function 'WS_DOWNLOAD'

EXPORTING

  • CODEPAGE = 'IBM'

FILENAME = v_error_filename

FILETYPE = 'DAT'

TABLES

DATA_TAB = IT_ERROR.

endif.

ENDFORM. "error_download

----


  • Start new screen *

----


FORM BDC_DYNPRO USING PROGRAM DYNPRO.

CLEAR BDCDATA.

BDCDATA-PROGRAM = PROGRAM.

BDCDATA-DYNPRO = DYNPRO.

BDCDATA-DYNBEGIN = 'X'.

APPEND BDCDATA.

ENDFORM. "BDC_DYNPRO

----


  • Insert field *

----


FORM BDC_FIELD USING FNAM FVAL.

  • IF FVAL <> NODATA.

CLEAR BDCDATA.

BDCDATA-FNAM = FNAM.

BDCDATA-FVAL = FVAL.

APPEND BDCDATA.

  • ENDIF.

ENDFORM. "BDC_FIELD

FORM bdc_transaction tables MESSTAB USING TCODE CTUMODE CUPDATE .

CALL TRANSACTION TCODE USING BDCDATA

MODE CTUMODE

UPDATE CUPDATE

MESSAGES INTO MESSTAB.

REFRESH BDCDATA.

clear BDCDATA.

ENDFORM. " bdc_transaction

&----


*& Form display_message

&----


FORM display_message .

if it_error[] is initial.

message i019(zmsg). "Success

else.

message e020(zmsg). "Failed

endif.

ENDFORM. " display_message

&----


*& Form get_filename

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_filename .

CALL FUNCTION 'F4_FILENAME'

EXPORTING

PROGRAM_NAME = SYST-CPROG

DYNPRO_NUMBER = SYST-DYNNR

  • FIELD_NAME = p_file

IMPORTING

FILE_NAME = p_file .

if sy-subrc NE 0 .

write : / 'Enter File Name'.

endif.

ENDFORM. " get_filename

&----


*& Form make_file_name

&----


FORM make_file_name .

write sy-datum to date MM/DD/YYYY.

write sy-uzeit to time USING EDIT MASK ' __ __ __'.

concatenate Text-002 date time '.txt'

into v_error_filename.

ENDFORM. " make_file_name

Thanks,

Sriram Ponna.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,293

Hi,

modify the codes as mentioned above and check each loop in debug mode and verify whether each call transaction is successful or not!

Regards.

Read only

Former Member
0 Likes
1,292

my problem is answered