Skip to Content
author's profile photo Former Member
Former Member

Purchase Order Creation

hi,

Can any one of u please provide me the code for ME21 BDC recording with table control.

Thanks & Regards,

Lavanya

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on May 21, 2008 at 11:17 AM

    Hi

    check this code

    ----


    • Program : ZGMIMM_ME21.

    • Transaction Code :

    • Description : BDC for Purchase order

    ----


    report zgmimm_me21 no standard page heading line-size 255.

    tables: t100.

    type-pools: truxs.

    types: begin of i_upload,

    lifnr like ekko-lifnr, "Vendor Number

    bukrs like ekko-bukrs, "Order type

    bedat like ekko-bedat, "Purchase Order Date

    ekorg like ekko-ekorg, "Purchasing organization

    ausnr like ekko-ausnr, "Purchase order

    ekgrp like ekko-ekgrp, "Purchasing group

    eddat like ekko-bedat, "Delivery date

    werks like ekpo-werks, "Plant

    lgort like ekpo-lgort, "Storage location

    zterm like ekko-zterm, "Payment terms

    ematn like ekpo-ematn, "Material

    menge like ekpo-menge, "PO Quantity

    lewed like ekpo-lewed, "Latest GR Date

    end of i_upload.

    data: begin of i_input occurs 0,

    pbg(10) type c,

    bldat(07) type c, "001

    • BLDAT like BKPF-BUDAT,

    hkont(10) type c,

    kostl(10) type c,

    wrbtr(11) type c,

    dbcrflag(01) type c,

    sgtxt(32) type c,

    end of i_input.

    data: v_counter(2) type n.

    data: v_mod type i.

    data: v_ebelp like ekpo-ebelp.

    data: v_fname(132) type c.

    data: v_fname1(132) type c.

    data: v_tabix like sy-tabix.

    data : ws_text(200) type c.

    data : wa_mag(1).

    data: begin of int_msg occurs 0,

    cnt type i,

    massage(1) type c,

    text(200) type c,

    end of int_msg.

    data : begin of i_final occurs 0,

    lifnr like ekko-lifnr, "Vendor Number

    bukrs like ekko-bukrs, "Order type

    bedat(10),

    ekorg like ekko-ekorg, "Purchasing organization

    ausnr like ekko-ausnr, "Purchase order

    ekgrp like ekko-ekgrp, "Purchasing group

    eddat(10),

    werks like ekpo-werks, "Plant

    lgort like ekpo-lgort, "Storage location

    zterm like ekko-zterm, "Payment terms

    ematn like ekpo-ematn, "Material

    menge type char15, "PO Quantity

    lewed(10),

    ***+EDS

    TAX_CODE type mwskz,

    GR_BASEDIV,

    end of i_final.

    data: it_datatab type standard table of i_upload,

    wa_datatab type i_upload.

    data: it_raw type truxs_t_text_data.

    data: begin of bdcdata occurs 0.

    include structure bdcdata.

    data: end of bdcdata.

    data: begin of messtab occurs 0.

    include structure bdcmsgcoll.

    data: end of messtab.

    data: begin of t_input occurs 0.

    include structure i_input.

    data: end of t_input.

    data: begin of i_local_output occurs 0,

    text(2800) type c,

    end of i_local_output.

    data: begin of i_error occurs 0.

    include structure i_input.

    data: text(50) type c.

    data: end of i_error.

    data : _datum1(10).

    data : t_datum1(10).

    data: msgno like sy-msgno,

    msgid like sy-msgid,

    msgty like sy-msgty,

    message like message.

    *Data : TEXT(50) Type c value 'File Shoud be in Excel Format'.

    data: wa_head type bapimepoheader,

    wa_headx type bapimepoheaderx,

    i_return type bapiret2 occurs 0 with header line,

    i_item type bapimepoitem occurs 0 with header line,

    i_itemx type bapimepoitemx occurs 0 with header line,

    i_sch type bapimeposchedule occurs 0 with header line,

    i_schx type bapimeposchedulx occurs 0 with header line.

    **----


    *

    • SELECTION SCREEN

    **----


    parameters: p_file type rlgrap-filename obligatory modif id app.

    • Write : /3 Text.

    **----


    *

    • START OF SELECTION

    **----


    at selection-screen on value-request for p_file.

    call function 'F4_FILENAME'

    exporting

    field_name = 'P_FILE'

    importing

    file_name = p_file.

    start-of-selection.

    perform data_from_excel_to_sap.

    perform pass_data_to_final_int_tab.

    • perform fill_bdc_data.

    perform fill_bapi_parameters.

    perform call_po_create_bapi.

    &----


    *& Form DATA_FROM_EXCEL_TO

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    form data_from_excel_to_sap.

    call function 'TEXT_CONVERT_XLS_TO_SAP'

    exporting

    • I_FIELD_SEPERATOR =

    i_line_header = 'X'

    i_tab_raw_data = it_raw " WORK TABLE

    i_filename = p_file

    tables

    i_tab_converted_data = it_datatab[] "ACTUAL DATA

    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_TO

    &----


    *& Form PASS_DATA_TO_FINAL_INTERNAL_TA

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    form pass_data_to_final_int_tab.

    loop at it_datatab into wa_datatab.

    move wa_datatab-lifnr to i_final-lifnr. "Vendor Number

    move wa_datatab-bukrs to i_final-bukrs. "Order type

    move wa_datatab-bedat to i_final-bedat. "Purchase Order Date

    concatenate i_final-bedat6(2) '.' i_final-bedat4(2) '.20'

    i_final-bedat+2(2) into _datum1.

    i_final-bedat = _datum1.

    move wa_datatab-ekorg to i_final-ekorg. "Purchasing organization

    move wa_datatab-ausnr to i_final-ausnr. "Purchase order

    move wa_datatab-ekgrp to i_final-ekgrp. "Purchasing group

    move wa_datatab-eddat to i_final-eddat. "Delivery date

    concatenate i_final-eddat6(2) '.' i_final-eddat4(2) '.20'

    i_final-eddat+2(2) into _datum1.

    i_final-eddat = _datum1.

    move wa_datatab-werks to i_final-werks. "Plant

    move wa_datatab-lgort to i_final-lgort. "Storage location

    move wa_datatab-zterm to i_final-zterm. "Payment terms

    move wa_datatab-ematn to i_final-ematn. "Material

    move wa_datatab-menge to i_final-menge. "PO Quantity

    move wa_datatab-lewed to i_final-lewed. "Latest GR Date

    ***Value Hard coaded considering this Program is only for Import.

    I_FINAL-TAX_CODE = 'V0'.

    I_FINAL-GR_BASEDIV = ' '.

    concatenate i_final-lewed6(2) '.' i_final-lewed4(2) '.20'

    i_final-lewed+2(2) into _datum1.

    i_final-lewed = _datum1.

    append i_final.

    endloop.

    endform. " PASS_DATA_TO_FINAL_INTERNAL_TA

    &----


    *& Form FILL_BDC_DATA

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    form fill_bdc_data.

    read table i_final index 1.

    perform f0510_bdcdata using :

    'X' 'SAPMM06E' '0100',

    ' ' 'BDC_CURSOR' 'RM06E-MATKL',

    ' ' 'BDC_OKCODE' '=KOPF',

    ' ' 'EKKO-LIFNR' i_final-lifnr,

    ' ' 'RM06E-BSART' i_final-bukrs,

    ' ' 'RM06E-BEDAT' i_final-bedat,

    ' ' 'RM06E-BSTNR' i_final-ausnr,

    ' ' 'EKKO-EKORG' i_final-ekorg,

    ' ' 'EKKO-EKGRP' i_final-ekgrp,

    ' ' 'RM06E-EEIND' i_final-eddat,

    ' ' 'RM06E-LPEIN' 'T',

    ' ' 'RM06E-WERKS' i_final-werks,

    ' ' 'RM06E-LGORT' i_final-lgort.

    perform f0510_bdcdata using :

    'X' 'SAPMM06E' '0101',

    ' ' 'BDC_OKCODE' '=AB',

    ' ' 'EKKO-ZTERM' i_final-zterm.

    move: '01' to v_counter.

    loop at i_final.

    move: sy-tabix to v_tabix.

    concatenate 'EKPO-EMATN(' v_counter ')' into v_fname.

    concatenate 'EKPO-MENGE(' v_counter ')' into v_fname1.

    perform f0510_bdcdata using :

    'X' 'SAPMM06E' '0120',

    ' ' 'BDC_OKCODE' '=DETZ',

    • ' ' 'RM06E-EBELP' '5',

    ' ' v_fname i_final-ematn,

    ' ' v_fname1 i_final-menge.

    *----


    perform f0510_bdcdata using :

    'X' 'SAPMM06E' '0112',

    ' ' 'BDC_CURSOR' 'EKPO-LEWED',

    ' ' 'BDC_OKCODE' '/00',

    ' ' 'EKPO-LEWED' i_final-lewed,

    ' ' 'EKPO-PRDAT' i_final-bedat.

    • ' ' 'EKPO-IPRKZ' 'D'.

    *----


    v_mod = v_counter mod 5.

    if v_mod eq 0.

    clear v_ebelp.

    move: v_tabix to v_ebelp.

    perform f0510_bdcdata using :

    'X' 'SAPMM06E' '0120',

    ' ' 'BDC_OKCODE' '/00',

    ' ' 'RM06E-EBELP' v_ebelp.

    v_counter = '02'.

    else.

    v_counter = v_counter + 1.

    endif.

    endloop.

    • loop at i_final.

    • clear: v_fname.

    • v_counter = sy-tabix.

    • v_ebelp = sy-tabix.

    • concatenate 'RM06E-TCSELFLAG(' '01' ')' into v_fname.

    • perform f0510_bdcdata using :

    • 'X' 'SAPMM06E' '0120',

    • ' ' 'RM06E-EBELP' v_ebelp,

    • ' ' 'BDC_OKCODE' '/00'.

    *

    • perform f0510_bdcdata using :

    • 'X' 'SAPMM06E' '0120',

    • ' ' 'BDC_CURSOR' 'RM06E-BSTPO(01)',

    • ' ' 'BDC_OKCODE' '=DETZ',

    • ' ' 'RM06E-EBELP' v_ebelp,

  • ' ' v_fname 'X'.

  • *

    • perform f0510_bdcdata using :

    • 'X' 'SAPMM06E' '0112',

    • ' ' 'BDC_CURSOR' 'EKPO-LEWED',

    • ' ' 'BDC_OKCODE' '/00',

    • ' ' 'EKPO-PLIFZ' '91',

    • ' ' 'EKPO-GEWEI' 'KG',

    • ' ' 'EKPO-PRDAT' i_final-bedat,

    • ' ' 'EKPO-IPRKZ' 'D',

    • ' ' 'EKPO-LEWED' i_final-lewed.

    • endloop.

    perform f0510_bdcdata using :

    'X' 'SAPMM06E' '0120',

    ' ' 'BDC_CURSOR' 'RM06E-BSTPO(10)',

    ' ' 'BDC_OKCODE' '=BU',

    ' ' 'RM06E-EBELP' '1'.

    perform f0510_bdcdata using :

    'X' 'SAPLSPO1' '0300',

    ' ' 'BDC_OKCODE' '=YES'.

    call transaction 'ME21' using bdcdata messages into messtab

    mode 'A' update 'S'.

    perform f000_process_errors.

    clear: bdcdata, messtab.

    refresh: bdcdata, messtab.

    loop at int_msg.

    write:/ int_msg-text.

    endloop.

    endform. " FILL_BDC_DATA

    &----


    *& Form F0510_BDCDATA

    &----


    • text

    ----


    • -->dynbegin text

    • -->name text

    • -->value text

    ----


    form f0510_bdcdata using value(dynbegin)

    value(name)

    value(value).

    if dynbegin = 'X'.

    clear bdcdata.

    move: name to bdcdata-program,

    value to bdcdata-dynpro,

    'X' to bdcdata-dynbegin.

    append bdcdata.

    else.

    clear bdcdata.

    move: name to bdcdata-fnam,

    value to bdcdata-fval.

    append bdcdata.

    endif.

    endform. " F0510_BDCDATA

    &----


    *& Form F000_PROCESS_ERRORS

    &----


    • text

    ----


    • --> p1 text

    • <-- p2 text

    ----


    form f000_process_errors.

    loop at messtab.

    clear t100.

    select single * from t100 where sprsl = messtab-msgspra

    and arbgb = messtab-msgid

    and msgnr = messtab-msgnr.

    ws_text = t100-text.

    wa_mag = messtab-msgtyp.

    if ws_text cs '&1'.

    replace '&1' with messtab-msgv1 into ws_text.

    replace '&2' with messtab-msgv2 into ws_text.

    replace '&3' with messtab-msgv3 into ws_text.

    replace '&4' with messtab-msgv4 into ws_text.

    else.

    replace '&' with messtab-msgv1 into ws_text.

    replace '&' with messtab-msgv2 into ws_text.

    replace '&' with messtab-msgv3 into ws_text.

    replace '&' with messtab-msgv4 into ws_text.

    endif.

    condense ws_text.

    move ws_text to int_msg-text.

    move wa_mag to int_msg-massage.

    append int_msg.

    endloop.

    delete int_msg where massage = 'W'.

    endform. " F000_PROCESS_ERRORS

    &----


    *& Form fill_bapi_parameters

    &----


    • Subroutine to fill BAPI Parameters

    ----


    form fill_bapi_parameters.

    clear: wa_head, wa_headx, i_return, i_item, i_itemx, i_sch, i_schx.

    perform fill_bapi_header.

    perform fill_bapi_items.

    endform. " fill_bapi_parameters

    &----


    *& Form fill_bapi_header

    &----


    • Subroutine to fill the bapi header

    ----


    form fill_bapi_header.

    read table i_final index 1.

    check sy-subrc eq 0.

    move: i_final-ausnr to wa_head-po_number,

    i_final-bukrs to wa_head-doc_type,

    i_final-zterm to wa_head-pmnttrms,

    i_final-ekorg to wa_head-purch_org,

    i_final-ekgrp to wa_head-pur_group,

    'FOB' to wa_head-incoterms1,

    'Free On Board' to wa_head-incoterms2.

    call function 'CONVERSION_EXIT_ALPHA_INPUT'

    exporting

    input = i_final-lifnr

    importing

    output = i_final-lifnr.

    move: i_final-lifnr to wa_head-vendor.

    concatenate i_final-bedat+6(4)

    i_final-bedat+3(2)

    i_final-bedat(2) into wa_head-doc_date.

    move: 'X' to wa_headx-po_number,

    'X' to wa_headx-doc_type,

    'X' to wa_headx-vendor,

    'X' to wa_headx-pmnttrms,

    'X' to wa_headx-purch_org,

    'X' to wa_headx-pur_group,

    'X' to wa_headx-doc_date,

    'X' to wa_headx-incoterms1,

    'X' to wa_headx-incoterms2.

    endform. " fill_bapi_header

    &----


    *& Form fill_bapi_items

    &----


    • Subroutine to fill the bapi items

    ----


    form fill_bapi_items.

    loop at i_final.

    call function 'CONVERSION_EXIT_ALPHA_INPUT'

    exporting

    input = i_final-ematn

    importing

    output = i_final-ematn.

    move: sy-tabix to i_item-po_item,

    i_final-ematn to i_item-material,

    i_final-werks to i_item-plant,

    i_final-lgort to i_item-stge_loc,

    i_final-menge to i_item-quantity.

    I_ITEM-TAX_CODE = I_FINAL-TAX_CODE.

    I_ITEM-GR_BASEDIV = I_FINAL-GR_BASEDIV.

    concatenate i_final-lewed+6(4)

    i_final-lewed+3(2)

    i_final-lewed(2) into i_item-gr_to_date.

    move: sy-tabix to i_itemx-po_item,

    'X' to i_itemx-po_itemx,

    'X' to i_itemx-material,

    'X' to i_itemx-plant,

    'X' to i_itemx-stge_loc,

    'X' to i_itemx-quantity,

    'X' to i_itemx-gr_to_date.

    **EDS

    move: 'X' TO i_itemx-TAX_CODE ,

    'X' TO i_itemx-GR_BASEDIV.

    append: i_item, i_itemx.

    *----


    move: sy-tabix to i_sch-po_item,

    '0001' to i_sch-sched_line,

    'D' to i_sch-del_datcat_ext.

    • i_final-menge to i_sch-quantity.

    concatenate i_final-eddat(2)

    i_final-eddat+3(2)

    i_final-eddat+6(4) into i_sch-delivery_date.

    move: sy-tabix to i_schx-po_item,

    '0001' to i_schx-sched_line,

    'X' to i_schx-po_itemx,

    'X' to i_schx-sched_linex,

    • 'X' to i_schx-quantity,

    'X' to i_schx-delivery_date,

    'X' to i_schx-del_datcat_ext.

    append: i_sch, i_schx.

    endloop.

    endform. " fill_bapi_items

    &----


    *& Form call_po_create_bapi

    &----


    • Subroutine to call PO CREATE BAPI

    ----


    form call_po_create_bapi.

    call function 'BAPI_PO_CREATE1'

    exporting

    poheader = wa_head

    poheaderx = wa_headx

    tables

    return = i_return

    poitem = i_item

    poitemx = i_itemx

    poschedule = i_sch

    poschedulex = i_schx.

    read table i_return with key type = 'S'

    id = '06'

    number = '017'.

    if sy-subrc eq 0.

    call function 'BAPI_TRANSACTION_COMMIT'

    exporting

    wait = 'X'.

    write:/ i_return-message.

    else.

    delete i_return where type = 'S'.

    delete i_return where type = 'I'.

    delete i_return where type = 'W'.

    write:/ 'Following Errors occured in PO Creation:'.

    skip.

    loop at i_return.

    write:/ i_return-message.

    endloop.

    endif.

    endform. " call_po_create_bapi

    thanks

    sitaram

Add a comment
10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 21, 2008 at 11:22 AM
    Add a comment
    10|10000 characters needed characters exceeded

  • Before answering

    You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
    You must be Logged in to submit an answer.

    Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.