SAP - BDC XD02 - Mass Update For Customer marketing details ( Annual Sales, Currency and Sales year)

REPORT zcustomer_marketing_upload.

TYPE-POOLS: truxs, slis.

TYPES: BEGIN OF x_upload,
kunnr TYPE kna1-kunnr,
umsa1 TYPE kna1-umsa1,
uwaer TYPE kna1-uwaer,
umjah TYPE kna1-umjah,
END OF x_upload.

TYPES: BEGIN OF x_msg,
kunnr TYPE kna1-kunnr,
msgtyp(7) TYPE c,
message(200) TYPE c,
END OF x_msg.

DATA: it_upload TYPE TABLE OF x_upload,
wa_upload TYPE x_upload.

DATA: it_bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.

DATA: it_bdcmsg1 TYPE STANDARD TABLE OF bdcmsgcoll,
wa_bdcmsg1 TYPE bdcmsgcoll,
it_bdcmsg2 TYPE STANDARD TABLE OF bdcmsgcoll,
wa_bdcmsg2 TYPE bdcmsgcoll.

DATA: i_raw_data TYPE truxs_t_text_data,
v_fname TYPE rlgrap-filename.

* Table for collecting messsages
DATA: it_msgtab TYPE STANDARD TABLE OF bdcmsgcoll ,
wa_msgtab TYPE bdcmsgcoll.
DATA: it_msg TYPE STANDARD TABLE OF x_msg,
wa_msg TYPE x_msg.

DATA: gd_layout TYPE slis_layout_alv,
fieldcatalog TYPE slis_t_fieldcat_alv,
wa_fieldcatalog TYPE slis_fieldcat_alv.

DATA: t_msg(100) TYPE c.


SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS: p_upload TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN : END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_upload.
PERFORM f4_help.

START-OF-SELECTION.

PERFORM data_from_excel_inttable.
PERFORM bdc_xd02.
PERFORM build_fieldcatalog.
PERFORM build_layout.
PERFORM display_alv_report.



*&---------------------------------------------------------------------*
*& Form f4_help
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f4_help.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
program_name = syst-repid
dynpro_number = syst-dynnr
field_name = 'P_UPLOAD'
CHANGING
file_name = p_upload
EXCEPTIONS
mask_too_long = 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.
ENDFORM. "f4_help

*&---------------------------------------------------------------------*
*& Form data_from_excel_inttable
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM data_from_excel_inttable.
MOVE p_upload TO v_fname.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'    " To load excel sheet data into internel table.
EXPORTING
i_field_seperator = ''
i_line_header = 'X'
i_tab_raw_data = i_raw_data
i_filename = v_fname
TABLES
i_tab_converted_data = it_upload
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.

ENDFORM. "data_from_excel_inttable


*&---------------------------------------------------------------------*
*& Form bdc
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM bdc_xd02.

LOOP AT it_upload INTO wa_upload.

PERFORM bdc_dynpro USING 'SAPMF02D' '0101'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RF02D-D0125'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'RF02D-KUNNR'
wa_upload-kunnr. "'1000005'.
PERFORM bdc_field USING 'RF02D-D0125'
'X'.
PERFORM bdc_dynpro USING 'SAPMF02D' '0125'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KNA1-UMJAH'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=UPDA'.
PERFORM bdc_field USING 'KNA1-UMSA1'
wa_upload-umsa1. "'99999'.
PERFORM bdc_field USING 'KNA1-UWAER'
wa_upload-uwaer. "'INR'.
PERFORM bdc_field USING 'KNA1-UMJAH'
wa_upload-umjah. "'2011'.

CALL TRANSACTION 'XD02' USING it_bdcdata MODE 'E' UPDATE 'S'
MESSAGES INTO it_bdcmsg1.

PERFORM collect_message.

REFRESH: it_bdcdata, it_bdcmsg1.
CLEAR: it_bdcdata, it_bdcmsg1.
ENDLOOP.
ENDFORM. "bdc


*&---------------------------------------------------------------------*
*& Form collect_message
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM collect_message.

LOOP AT it_bdcmsg1 INTO wa_bdcmsg1.

CALL FUNCTION 'FORMAT_MESSAGE'
EXPORTING
id = wa_bdcmsg1-msgid
lang = wa_bdcmsg1-msgspra
no = wa_bdcmsg1-msgnr
v1 = wa_bdcmsg1-msgv1
v2 = wa_bdcmsg1-msgv2
v3 = wa_bdcmsg1-msgv3
v4 = wa_bdcmsg1-msgv4
IMPORTING
msg = t_msg
EXCEPTIONS
not_found = 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.

IF t_msg IS NOT INITIAL.
CONDENSE t_msg.
CLEAR wa_msg.

IF wa_bdcmsg1-msgtyp = 'S'.
wa_msg-msgtyp = 'Success'.
ELSE.
wa_msg-msgtyp = 'Error'.
ENDIF.

wa_msg-message = t_msg.
wa_msg-kunnr = wa_upload-kunnr.
APPEND wa_msg TO it_msg .
ENDIF.
ENDLOOP.

CLEAR: wa_msg, t_msg.

ENDFORM. "collect_message


*&---------------------------------------------------------------------*
*& Form build_fieldcatalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .

wa_fieldcatalog-fieldname = 'KUNNR'.
wa_fieldcatalog-seltext_l = text-002.
wa_fieldcatalog-tabname = 'it_msg'.
wa_fieldcatalog-emphasize = 'X'.
APPEND wa_fieldcatalog TO fieldcatalog.
CLEAR wa_fieldcatalog.

wa_fieldcatalog-fieldname = 'MSGTYP'.
wa_fieldcatalog-seltext_l = text-003.
wa_fieldcatalog-tabname = 'it_msg'.
wa_fieldcatalog-emphasize = 'X'.
APPEND wa_fieldcatalog TO fieldcatalog.
CLEAR wa_fieldcatalog.

wa_fieldcatalog-fieldname = 'MESSAGE'.
wa_fieldcatalog-seltext_l = text-004.
wa_fieldcatalog-tabname = 'it_msg'.
wa_fieldcatalog-emphasize = 'X'.
APPEND wa_fieldcatalog TO fieldcatalog.
CLEAR wa_fieldcatalog.

ENDFORM. "build_fieldcatalog

*&---------------------------------------------------------------------*
*& Form build_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM build_layout .
gd_layout-no_input = 'X'.
gd_layout-colwidth_optimize = 'X'.
gd_layout-info_fieldname = 'LINE_COLOR'.
gd_layout-zebra = 'X'.

ENDFORM. " build_layout

*&---------------------------------------------------------------------*
*& Form display_alv_report
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_alv_report.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = gd_layout
it_fieldcat = fieldcatalog[]
TABLES
t_outtab = it_msg.
ENDFORM. "display_alv_report


*&---------------------------------------------------------------------*
*& Form bdc_dynpro
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PROG text
* -->SCR text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING prog scr.
CLEAR it_bdcdata.
it_bdcdata-program = prog.
it_bdcdata-dynpro = scr.
it_bdcdata-dynbegin = 'X'.
APPEND it_bdcdata.
ENDFORM. "

*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->FNAM text
* -->FVAL text
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR it_bdcdata.
it_bdcdata-fnam = fnam.
it_bdcdata-fval = fval.

*Incase Of BDC Error: Input value is longer than screen field.
*When the value is moving to extreme right add the following statement.

SHIFT it_bdcdata-fval LEFT DELETING LEADING space. " Done for UMSA1 field.
APPEND it_bdcdata.
ENDFORM. "bdc_field


FILE TYPE:








SELECTION SCREEN:





OUTPUT: