Google Search - Blog...........

ABAP - BDC Program For Open Sales Order - SD (VA01).

Description
Open Sales Order*---------------------------------------------------------------------*
*Report : ZDR162
*Description : Open Sales Order
*Date :
*Author : Asis Mohanty, Satyam
*---------------------------------------------------------------------*
REPORT zdr162
NO STANDARD PAGE HEADING LINE-SIZE 255.
*************************Internal Table*****************************
DATA: BEGIN OF i_tab OCCURS 0,
auart(4) TYPE c, "Sales document type
vkorg(4) TYPE c, "Sales Organization
vtweg(2) TYPE c, "Distribution Channel
spart(2) TYPE c, "Division
kunnr(10) TYPE c, "Customer
bstkd(35) TYPE c, "PO number
bstdk(10) TYPE c, "PO Date
matnr(18) TYPE c, "Material
kwmeng(18) TYPE c, "Order quantity
text(100) TYPE c, "LONG TEXT
END OF i_tab.

DATA: BEGIN OF i_tab_dtl OCCURS 0,
bstkd(35) TYPE c, "PO number
auart(4) TYPE c,
matnr(18) TYPE c, "Material
kwmeng(18) TYPE c, "Order quantity
END OF i_tab_dtl.

DATA: BEGIN OF i_tab_hdr OCCURS 0,
bstkd(35) TYPE c, "PO number
auart(4) TYPE c, "Sales document type
bstdk(10) TYPE c, "PO Date
vkorg(4) TYPE c, "Sales Organization
vtweg(2) TYPE c, "Distribution Channel
spart(2) TYPE c, "Division
kunnr(10) TYPE c, "Customer
matnr(18) TYPE c, "Material
kwmeng(18) TYPE c, "Order quantity
text(100) TYPE c, "LONG TEXT
END OF i_tab_hdr.

DATA: BEGIN OF i_header.
INCLUDE STRUCTURE thead.
DATA: END OF i_header.

DATA: BEGIN OF i_lines OCCURS 0.
INCLUDE STRUCTURE tline.
DATA: END OF i_lines.

* Batchinputdata of single transaction
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE.

* messages of call transaction
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.

* error session opened (' ' or 'X')
DATA: e_group_opened.

* message texts
TABLES: t100.

DATA: BEGIN OF i_messages OCCURS 0,
bstkd LIKE vbkd-bstkd,
auart LIKE vbak-auart,
* MATNR LIKE VBAP-MATNR,
longtext(225).
INCLUDE STRUCTURE bdcmsgcoll.
DATA: END OF i_messages.

DATA: nodata(1) VALUE '/' . "nodata**************************Session

Creation***************************
*INCLUDE bdcrecx1.

*************************Selection Screen****************************

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS p_mode LIKE ctu_params-dismode DEFAULT 'N'.
*A: show all dynpros
*E: show dynpro on error only
*N: do not display dynpro
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t01.
PARAMETERS: pfile LIKE rlgrap-filename DEFAULT space.
SELECTION-SCREEN END OF BLOCK b2.

**********************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR PFILE.

CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = space
def_path = pfile
mask = ',*.*,*.*.'
mode = 'E'
title = 'Choose File Name'
IMPORTING
filename = pfile
EXCEPTIONS
inv_winsys = 04
no_batch = 08
selection_cancel = 12
selection_error = 16.
IF sy-subrc EQ '04'.
MESSAGE e999(zpa) WITH 'FILE NOT FOUND'(e01).
ENDIF.***

**************************Main Logic*******************************
START-OF-SELECTION.

PERFORM f_upload_file.
perform f_delete_blank.
PERFORM f_create_header.
PERFORM f_create_detail.
PERFORM f_create_output_heading.
PERFORM f_call_va01.
PERFORM f_save_messages.
PERFORM f_write_mesg.

*&---------------------------------------------------------------------*
*& Form f_upload_file
*&---------------------------------------------------------------------*
* Upload File
*----------------------------------------------------------------------*
FORM f_upload_file .
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
filename = pfile
filetype = 'DAT'
TABLES
data_tab = i_tab.
ENDFORM. " f_upload_file

*&---------------------------------------------------------------------*
*& Form f_call_va01
*&---------------------------------------------------------------------*
* Call VA01
*----------------------------------------------------------------------*

FORM f_call_va01 .
DATA: v_bdcfd1(100), v_bdcfd2(100).
DATA: cnt(2) TYPE n.
LOOP AT i_tab_hdr.
PERFORM header.
cnt = 1.
LOOP AT i_tab_dtl WHERE bstkd = i_tab_hdr-bstkd
AND auart = i_tab_hdr-auart.

PERFORM bdc_dynpro USING 'SAPMV45A' '4001'.* PERFORM bdc_field USING 'BDC_CURSOR'
* 'VBAP-POSNR(01)'.
*
PERFORM bdc_field USING 'BDC_OKCODE'
'=POAN'.

CONCATENATE 'RV45A-KWMENG(' cnt ')' INTO v_bdcfd1.

PERFORM bdc_field USING 'BDC_CURSOR'
v_bdcfd1.

CONCATENATE 'RV45A-MABNR(' cnt ')' INTO v_bdcfd2.

PERFORM bdc_field USING v_bdcfd2
i_tab_dtl-matnr.

PERFORM bdc_field USING v_bdcfd1
i_tab_dtl-kwmeng.

cnt = 2.

ENDLOOP.**Save Code
PERFORM bdc_dynpro USING 'SAPMV45A' '4001'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SICH'.

* PERFORM bdc_transaction USING 'VA01'.

CALL TRANSACTION 'VA01' USING bdcdata
MODE p_mode
MESSAGES INTO messtab.

PERFORM f_fill_messages .
*Fetch the message record having Sales Order
READ TABLE messtab WITH KEY tcode = 'VA01'
dyname = 'SAPMV45A'
dynumb = '4001'
msgtyp = 'S'
msgspra = 'E'
msgid = 'V1'
msgnr = '311'.

IF sy-subrc EQ 0.
PERFORM f_header_text.
ENDIF.
REFRESH: messtab, bdcdata.

ENDLOOP.

ENDFORM. "f_call_va01

*&---------------------------------------------------------------------*
*& Form f_create_detail
*&---------------------------------------------------------------------*
* Store Detail Prgs.
*----------------------------------------------------------------------*
FORM f_create_detail .
LOOP AT i_tab.

TRANSLATE: i_tab-auart TO UPPER CASE,
i_tab-bstkd TO UPPER CASE.
i_tab_dtl-bstkd = i_tab-bstkd.
i_tab_dtl-auart = i_tab-auart.
i_tab_dtl-matnr = i_tab-matnr.
i_tab_dtl-kwmeng = i_tab-kwmeng.
APPEND i_tab_dtl.
CLEAR i_tab_dtl.
ENDLOOP.
ENDFORM. " f_create_detail

*&---------------------------------------------------------------------*
*& Form F_CREATE_HEADER
*&---------------------------------------------------------------------*
* Create Header
*----------------------------------------------------------------------*
FORM f_create_header .
DATA: v_po LIKE vbkd-bstkd,
v_so LIKE vbak-auart.

SORT i_tab BY bstkd auart.
LOOP AT i_tab.
TRANSLATE: i_tab-auart TO UPPER CASE,
i_tab-bstkd TO UPPER CASE,
i_tab-vkorg TO UPPER CASE,
i_tab-vtweg TO UPPER CASE,
i_tab-spart TO UPPER CASE,
i_tab-kunnr TO UPPER CASE.

IF v_po NE i_tab-bstkd OR v_so NE i_tab-auart.
i_tab_hdr-bstkd = i_tab-bstkd.
i_tab_hdr-bstdk = i_tab-bstdk.
i_tab_hdr-auart = i_tab-auart.
i_tab_hdr-vkorg = i_tab-vkorg.
i_tab_hdr-vtweg = i_tab-vtweg.
i_tab_hdr-spart = i_tab-spart.
i_tab_hdr-kunnr = i_tab-kunnr.
i_tab_hdr-text = i_tab-text.
APPEND i_tab_hdr.
CLEAR i_tab_hdr.

v_po = i_tab-bstkd.
v_so = i_tab-auart.
ENDIF.
ENDLOOP.
ENDFORM. " F_CREATE_HEADER

*&---------------------------------------------------------------------*
*& Form header
*&---------------------------------------------------------------------*
* Populate Header fields in sales order
*----------------------------------------------------------------------*
FORM header .
PERFORM bdc_dynpro USING 'SAPMV45A' '0101'.
PERFORM bdc_field USING 'BDC_CURSOR'
'VBAK-SPART'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'VBAK-AUART'
* 'ZVOR'.
i_tab_hdr-auart.
PERFORM bdc_field USING 'VBAK-VKORG'
* 'PSPR'.
i_tab_hdr-vkorg.
PERFORM bdc_field USING 'VBAK-VTWEG'
* 'DL'.
i_tab_hdr-vtweg.
PERFORM bdc_field USING 'VBAK-SPART'
* '01'.
i_tab_hdr-spart.
PERFORM bdc_dynpro USING 'SAPMV45A' '4001'.
PERFORM bdc_field USING 'BDC_OKCODE'
'/00'.
PERFORM bdc_field USING 'BDC_CURSOR'
'VBKD-BSTDK'.
PERFORM bdc_field USING 'VBKD-BSTKD'
* 'PO'.
i_tab_hdr-bstkd.
PERFORM bdc_field USING 'VBKD-BSTDK'
* '09.09.2003'.
i_tab_hdr-bstdk.
PERFORM bdc_field USING 'KUAGV-KUNNR'
* '1'.
i_tab_hdr-kunnr.
ENDFORM. " header

*&---------------------------------------------------------------------*
*& Form F_HEADER_TEXT
*&---------------------------------------------------------------------*
* Save Sales Order Header Text
*----------------------------------------------------------------------*
FORM f_header_text .

DATA: v_salord(10).
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = messtab-msgv2
IMPORTING
output = v_salord. i_header-tdobject = 'VBBK'.
i_header-tdname = v_salord.
i_header-tdid = 'Z002'.
i_header-tdspras = 'EN'.
i_header-tdtitle = 'Sales Order Header Text'.
i_lines-tdformat = '*'.
i_lines-tdline = i_tab_hdr-text.
APPEND i_lines.
CLEAR i_lines. CALL FUNCTION 'SAVE_TEXT'
EXPORTING
header = i_header
* insert = 'X'
savemode_direct = 'X'
TABLES
lines = i_lines. 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.
REFRESH i_lines.
CLEAR i_header.

ENDFORM. " F_HEADER_TEXT

*&---------------------------------------------------------------------*
*& Form f_fill_messages
*&---------------------------------------------------------------------*
FORM f_fill_messages . DATA: l_mstring(250). LOOP AT messtab.
i_messages-bstkd = i_tab_hdr-bstkd.
i_messages-auart = i_tab_hdr-auart.
SELECT SINGLE * FROM t100 WHERE sprsl = messtab-msgspra
AND arbgb = messtab-msgid
AND msgnr = messtab-msgnr.
IF sy-subrc = 0.
l_mstring = t100-text.
IF l_mstring CS '&1'.
REPLACE '&1' WITH messtab-msgv1 INTO l_mstring.
REPLACE '&2' WITH messtab-msgv2 INTO l_mstring.
REPLACE '&3' WITH messtab-msgv3 INTO l_mstring.
REPLACE '&4' WITH messtab-msgv4 INTO l_mstring.
ELSE.
REPLACE '&' WITH messtab-msgv1 INTO l_mstring.
REPLACE '&' WITH messtab-msgv2 INTO l_mstring.
REPLACE '&' WITH messtab-msgv3 INTO l_mstring.
REPLACE '&' WITH messtab-msgv4 INTO l_mstring.
ENDIF.
CONDENSE l_mstring.

IF messtab-msgtyp = 'E'.

WRITE: /2 i_tab_hdr-bstkd,
14 i_tab_hdr-auart,
24 messtab-msgtyp,
36 l_mstring COLOR 6.

ELSE.
WRITE: /2 i_tab_hdr-bstkd,
14 i_tab_hdr-auart,
24 messtab-msgtyp,
36 l_mstring.

ENDIF.
i_messages-msgtyp = messtab-msgtyp.
i_messages-longtext = l_mstring.
i_messages-dyname = messtab-dyname.
i_messages-dynumb = messtab-dynumb.
i_messages-msgspra = messtab-msgspra.
i_messages-msgid = messtab-msgid.
i_messages-msgnr = messtab-msgnr.
i_messages-env = messtab-env.
i_messages-fldname = messtab-fldname.
APPEND i_messages.
CLEAR i_messages.

ELSE.
WRITE: /2 messtab. i_messages-dyname = messtab-dyname.
i_messages-dynumb = messtab-dynumb.
i_messages-msgtyp = messtab-msgtyp.
i_messages-msgspra = messtab-msgspra.
i_messages-msgid = messtab-msgid.
i_messages-msgnr = messtab-msgnr.
i_messages-env = messtab-env.
i_messages-fldname = messtab-fldname.
APPEND i_messages.
CLEAR i_messages.

ENDIF.
ENDLOOP.

ENDFORM. " f_fill_messages

*&---------------------------------------------------------------------*
*& Form f_save_messages
*&---------------------------------------------------------------------*
* Save message on local disk
*----------------------------------------------------------------------*
FORM f_save_messages .
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
filename = 'C:\SOORDER.TXT'
filetype = 'DAT'
TABLES
data_tab = i_messages.
ENDFORM. " f_save_messages
*----------------------------------------------------------------------*
* 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 f_write_mesg
*&---------------------------------------------------------------------*
* Print message on screen
*----------------------------------------------------------------------*
FORM f_write_mesg .
SKIP.
ULINE.
WRITE: /2 'Log file downloded at C:\SOORDER.TXT'.ENDFORM. " f_write_mesg
*&---------------------------------------------------------------------*
*& Form f_create_output_heading
*&---------------------------------------------------------------------*
FORM f_create_output_heading . WRITE: /2 'PO No.' COLOR 1,
14 'SO Type' COLOR 1,
24 'Msg. Type' COLOR 1,
36 'Message' COLOR 1.
ULINE.
ENDFORM. " f_create_output_heading
*&---------------------------------------------------------------------*
*& Form f_delete_blank
*&---------------------------------------------------------------------*
* Delete blank lines
*----------------------------------------------------------------------*
form f_delete_blank .
delete i_tab where bstkd = space and auart = space.
endform. " f_delete_blank



ALSO READ:

- BDC Program To Upload Material Master Data (MM01).

- BDC Program For Vendor XK01.

- BDC Program To Send Mails And Trigger Jobs.

- BDC Program To Load The Material Assignment Of Routing (CA02) - PP.

- Sample Code To Handle Multiple Records In BDC Table Control (IB01).

- HR ABAP - BDC For Uploading Absence Data Into PA30 Transaction.

- HR ABAP - BDC To Upload The OM Relations Through T-code PO13.

- HR ABAP - BDC Upload For The Org Unit Relationship Through T-Code PO10.

- HR ABAP - BDC For The Action Transfer Of Location.

No comments:

Post a Comment