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

Trying to use bapi_salesorder_createfromdat2 to create sales order with variant item

Former Member
18,223

I'm VERRRRY new to ABAP and am trying to use bapi_salesorder_createfromdat2 to create a sales order that has a variant part.  I have a FM that calls the BAPI and it does create the SO, however the variant data does not load.  I've tried multiple things online that I have found without any luck. Any help would be appreciated.  I've tried to use the BAPI that gets data from the SO to see what the variant data is, again with no luck.

Thanks,

Here is my FM

FUNCTION ZCREATESALESORDERTEST.

*"----------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     VALUE(DOCTYPE) TYPE  AUART

*"     VALUE(SALESORG) TYPE  VKORG

*"     VALUE(SOLD) TYPE  KUNNR

*"     VALUE(SHIP) TYPE  KUNNR

*"     VALUE(MATERIAL) TYPE  MATNR

*"     VALUE(QTY) TYPE  KWMENG

*"     VALUE(PLANT) TYPE  WERKS_D

*"     VALUE(VALUE) TYPE  CUX_VALUE

*"----------------------------------------------------------------------

DATA: v_vbeln LIKE BAPIVBELN-VBELN.

DATA: header LIKE bapisdhd1 OCCURS 0 WITH HEADER LINE.

DATA: headerx LIKE BAPISDHD1X occurs 0 with HEADER LINE.

DATA: uheaderx like BAPISDH1X occurs 0 with header line.

DATA: item    LIKE BAPISDITM OCCURS 0 WITH HEADER LINE.

DATA: itemx   LIKE BAPISDITMx OCCURS 0 WITH HEADER LINE.

DATA: partner LIKE BAPIPARNR  OCCURS 0 WITH HEADER LINE.

DATA: return  LIKE bapiret2    OCCURS 0 WITH HEADER LINE.

DATA: cfg_ref like BAPICUCFG occurs 0 with header line.

DATA: cfg_ref_inst like BAPICUREF occurs 0 with header line.

DATA: cfg_inst like BAPICUINS occurs 0 with header line.

DATA: cfg_part like BAPICUPRT occurs 0 with header line.

DATA: cfg_value like BAPICUVAL occurs 0 with header line.

DATA: lt_schedules_inx   TYPE STANDARD TABLE OF BAPISCHDLx

                          WITH HEADER LINE.

DATA: lt_schedules_in    TYPE STANDARD TABLE OF BAPISCHDL

                          WITH HEADER LINE.

* HEADER DATA

header-doc_type = DOCTYPE.

headerx-doc_type = 'X'.

header-sales_org = SALESORG.

headerx-sales_org = 'X'.

header-distr_chan  = 10.

headerx-distr_chan = 'X'.

header-division = 10.

headerx-division = 'X'.

header-purch_no_c = '12345'.

header-name = 'Chris Ivey'.

append header.

append headerx.

* ITEM DATA

itemx-updateflag = 'I'.

item-itm_number = '000010'.

itemx-itm_number = 'X'.

item-po_itm_no = 12345.

itemx-po_itm_no = 'X'.

item-material = MATERIAL.

itemx-material = 'X'.

item-plant    = PLANT.

itemx-plant   = 'X'.

item-target_qty = QTY.

itemx-target_qty = 'X'.

item-target_qu = 'EA'.

itemx-target_qu = 'X'.

item-gross_wght = 5.

itemx-gross_wght = 'X'.

item-net_weight = 4.

itemx-net_weight = 'X'.

item-untof_wght = 'LB'.

itemx-untof_wght = 'X'.

APPEND item.

APPEND itemx.

* PARTNER DATA

partner-partn_role = 'AG'.

partner-partn_numb = SHIP.

APPEND partner.

partner-partn_role = 'WE'.

partner-partn_numb = SOLD.

APPEND partner.

*   Fill schedule lines

lt_schedules_in-itm_number = '000010'.

lt_schedules_in-sched_line = '0001'.

lt_schedules_in-req_qty    = qty.

APPEND lt_schedules_in.

*   Fill schedule line flags

lt_schedules_inx-itm_number  = '000010'.

lt_schedules_inx-sched_line  = '0001'.

lt_schedules_inx-updateflag  = 'X'.

lt_schedules_inx-req_qty     = 'X'.

APPEND lt_schedules_inx.

*Configuration Ref

cfg_ref-posex = '000010'.

cfg_ref-config_id = '000001'.

cfg_ref-root_id = '00000001'.

APPEND cfg_ref.

*Configuration Ref Inst

cfg_ref_inst-posex = '000010'.

cfg_ref_inst-config_id = '000001'.

cfg_ref_inst-inst_id = '00000001'.

APPEND cfg_ref_inst.

*Configuration Inst

cfg_inst-config_id = '000001'.

cfg_inst-inst_id = '00000001'.

cfg_inst-obj_type = 'MARA'.

cfg_inst-class_type = '300'.

cfg_inst-obj_key = MATERIAL.

cfg_inst-quantity = QTY.

cfg_inst-QUANTITY_UNIT = 'EA'.

append cfg_inst.

*Configuration Part

*cfg_part-parent_id = '00000001'.

*cfg_part-inst_id = '00000001'.

*cfg_part-class_type = '300'.

*cfg_part-obj_type = 'MARA'.

*cfg_part-obj_key = MATERIAL.

*cfg_part-author = '8'.

*cfg_part-part_of_no = '00010'.

*gt_order_cfgs_partof-part_of_guid = '4AFEB56EA28C0263E1008000C0AA760F'.

APPEND cfg_part.

*Configuration Values

cfg_value-config_id = '000001'.

cfg_value-inst_id = '00000001'.

cfg_value-charc = 'PRICE_POINT'.

cfg_value-value = VALUE.

cfg_value-valcode = 1.

*gt_order_cfgs_value-author = '8'.

APPEND cfg_value.

* Check the return table.

LOOP AT return WHERE type = 'E' OR type = 'A'.

   EXIT.

ENDLOOP.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'

     EXPORTING

           ORDER_HEADER_IN     = header

           ORDER_HEADER_INX    = headerx

           "SALESDOCUMENTIN    = v_vbeln

      IMPORTING

           SALESDOCUMENT    = v_vbeln

      TABLES

           return              = return

           ORDER_ITEMS_IN      = item

           order_items_inx     = itemx

           order_schedules_in  = lt_schedules_in

           order_schedules_inx = lt_schedules_inx

           order_partners      = partner

           order_cfgs_ref      = cfg_ref

           ORDER_CFGS_REFINST = cfg_ref_inst

           order_CFGS_INST = cfg_inst

           order_CFGS_VALUE = cfg_value.

IF sy-subrc = 0.

  COMMIT WORK.

   WRITE: / 'Document ', v_vbeln, ' created'.

ELSE.

   WRITE: / 'Error in creating sales document'.

ENDIF.

ENDFUNCTION.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
14,968

Hi Christopher,

Please find the attached document for uploading the sales document with variant configuration.

Hope you find it helpful.

With Regards

KT.

I'm VERRRRY new to ABAP and am trying to use bapi_salesorder_createfromdat2 to create a sales order that has a variant part.  I have a FM that calls the BAPI and it does create the SO, however the variant data does not load.  I've tried multiple things online that I have found without any luck. Any help would be appreciated.  I've tried to use the BAPI that gets data from the SO to see what the variant data is, again with no luck.

Thanks,

Here is my FM

FUNCTION ZCREATESALESORDERTEST.

*"----------------------------------------------------------------------

*"*"Local Interface:

*"  IMPORTING

*"     VALUE(DOCTYPE) TYPE  AUART

*"     VALUE(SALESORG) TYPE  VKORG

*"     VALUE(SOLD) TYPE  KUNNR

*"     VALUE(SHIP) TYPE  KUNNR

*"     VALUE(MATERIAL) TYPE  MATNR

*"     VALUE(QTY) TYPE  KWMENG

*"     VALUE(PLANT) TYPE  WERKS_D

*"     VALUE(VALUE) TYPE  CUX_VALUE

*"----------------------------------------------------------------------

DATA: v_vbeln LIKE BAPIVBELN-VBELN.

DATA: header LIKE bapisdhd1 OCCURS 0 WITH HEADER LINE.

DATA: headerx LIKE BAPISDHD1X occurs 0 with HEADER LINE.

DATA: uheaderx like BAPISDH1X occurs 0 with header line.

DATA: item    LIKE BAPISDITM OCCURS 0 WITH HEADER LINE.

DATA: itemx   LIKE BAPISDITMx OCCURS 0 WITH HEADER LINE.

DATA: partner LIKE BAPIPARNR  OCCURS 0 WITH HEADER LINE.

DATA: return  LIKE bapiret2    OCCURS 0 WITH HEADER LINE.

DATA: cfg_ref like BAPICUCFG occurs 0 with header line.

DATA: cfg_ref_inst like BAPICUREF occurs 0 with header line.

DATA: cfg_inst like BAPICUINS occurs 0 with header line.

DATA: cfg_part like BAPICUPRT occurs 0 with header line.

DATA: cfg_value like BAPICUVAL occurs 0 with header line.

DATA: lt_schedules_inx   TYPE STANDARD TABLE OF BAPISCHDLx

                          WITH HEADER LINE.

DATA: lt_schedules_in    TYPE STANDARD TABLE OF BAPISCHDL

                          WITH HEADER LINE.

* HEADER DATA

header-doc_type = DOCTYPE.

headerx-doc_type = 'X'.

header-sales_org = SALESORG.

headerx-sales_org = 'X'.

header-distr_chan  = 10.

headerx-distr_chan = 'X'.

header-division = 10.

headerx-division = 'X'.

header-purch_no_c = '12345'.

header-name = 'Chris Ivey'.

append header.

append headerx.

* ITEM DATA

itemx-updateflag = 'I'.

item-itm_number = '000010'.

itemx-itm_number = 'X'.

item-po_itm_no = 12345.

itemx-po_itm_no = 'X'.

item-material = MATERIAL.

itemx-material = 'X'.

item-plant    = PLANT.

itemx-plant   = 'X'.

item-target_qty = QTY.

itemx-target_qty = 'X'.

item-target_qu = 'EA'.

itemx-target_qu = 'X'.

item-gross_wght = 5.

itemx-gross_wght = 'X'.

item-net_weight = 4.

itemx-net_weight = 'X'.

item-untof_wght = 'LB'.

itemx-untof_wght = 'X'.

APPEND item.

APPEND itemx.

* PARTNER DATA

partner-partn_role = 'AG'.

partner-partn_numb = SHIP.

APPEND partner.

partner-partn_role = 'WE'.

partner-partn_numb = SOLD.

APPEND partner.

*   Fill schedule lines

lt_schedules_in-itm_number = '000010'.

lt_schedules_in-sched_line = '0001'.

lt_schedules_in-req_qty    = qty.

APPEND lt_schedules_in.

*   Fill schedule line flags

lt_schedules_inx-itm_number  = '000010'.

lt_schedules_inx-sched_line  = '0001'.

lt_schedules_inx-updateflag  = 'X'.

lt_schedules_inx-req_qty     = 'X'.

APPEND lt_schedules_inx.

*Configuration Ref

cfg_ref-posex = '000010'.

cfg_ref-config_id = '000001'.

cfg_ref-root_id = '00000001'.

APPEND cfg_ref.

*Configuration Ref Inst

cfg_ref_inst-posex = '000010'.

cfg_ref_inst-config_id = '000001'.

cfg_ref_inst-inst_id = '00000001'.

APPEND cfg_ref_inst.

*Configuration Inst

cfg_inst-config_id = '000001'.

cfg_inst-inst_id = '00000001'.

cfg_inst-obj_type = 'MARA'.

cfg_inst-class_type = '300'.

cfg_inst-obj_key = MATERIAL.

cfg_inst-quantity = QTY.

cfg_inst-QUANTITY_UNIT = 'EA'.

append cfg_inst.

*Configuration Part

*cfg_part-parent_id = '00000001'.

*cfg_part-inst_id = '00000001'.

*cfg_part-class_type = '300'.

*cfg_part-obj_type = 'MARA'.

*cfg_part-obj_key = MATERIAL.

*cfg_part-author = '8'.

*cfg_part-part_of_no = '00010'.

*gt_order_cfgs_partof-part_of_guid = '4AFEB56EA28C0263E1008000C0AA760F'.

APPEND cfg_part.

*Configuration Values

cfg_value-config_id = '000001'.

cfg_value-inst_id = '00000001'.

cfg_value-charc = 'PRICE_POINT'.

cfg_value-value = VALUE.

cfg_value-valcode = 1.

*gt_order_cfgs_value-author = '8'.

APPEND cfg_value.

* Check the return table.

LOOP AT return WHERE type = 'E' OR type = 'A'.

   EXIT.

ENDLOOP.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'

     EXPORTING

           ORDER_HEADER_IN     = header

           ORDER_HEADER_INX    = headerx

           "SALESDOCUMENTIN    = v_vbeln

      IMPORTING

           SALESDOCUMENT    = v_vbeln

      TABLES

           return              = return

           ORDER_ITEMS_IN      = item

           order_items_inx     = itemx

           order_schedules_in  = lt_schedules_in

           order_schedules_inx = lt_schedules_inx

           order_partners      = partner

           order_cfgs_ref      = cfg_ref

           ORDER_CFGS_REFINST = cfg_ref_inst

           order_CFGS_INST = cfg_inst

           order_CFGS_VALUE = cfg_value.

IF sy-subrc = 0.

  COMMIT WORK.

   WRITE: / 'Document ', v_vbeln, ' created'.

ELSE.

   WRITE: / 'Error in creating sales document'.

ENDIF.

ENDFUNCTION.

9 REPLIES 9
Read only

Former Member
0 Likes
14,969

Hi Christopher,

Please find the attached document for uploading the sales document with variant configuration.

Hope you find it helpful.

With Regards

KT.

Read only

0 Likes
14,968

I guess what I'm asking is how to populate the configuration....I couldn't find in your document where you actually populated it.  You declared it, and then used it....but I didn't see you populating them....

Read only

0 Likes
14,968

Hi Christopher,

Please find the below code in the document:-

*--------------------------------------------------------------------*

*Characteristics & Conditions Data

*--------------------------------------------------------------------*

      IF wa_salesdoc_l-charc IS NOT INITIAL.

        lv_config   = lv_config + 1.

        lv_instance = lv_instance + 1.

        wa_sales_cfgs_ref-posex         = lv_posnr.

        wa_sales_cfgs_ref-config_id     = lv_config.

        wa_sales_cfgs_ref-root_id       = lv_instance.

        wa_sales_cfgs_inst-config_id    = lv_config.

        wa_sales_cfgs_inst-inst_id      = lv_instance.

        wa_sales_cfgs_inst-obj_type     = 'MARA'.

        wa_sales_cfgs_inst-class_type   = '300'.

        wa_sales_cfgs_inst-obj_key      = wa_salesdoc_l-material.

        wa_sales_cfgs_inst-quantity     = wa_salesdoc_l-req_qty.

        CONDENSE wa_sales_cfgs_inst-quantity.

        wa_sales_cfgs_inst-author       = '8'.

        wa_sales_cfgs_refinst-posex     = lv_posnr.

        wa_sales_cfgs_refinst-config_id = lv_config.

        wa_sales_cfgs_refinst-inst_id   = lv_instance.

        APPEND : wa_sales_cfgs_ref TO it_sales_cfgs_ref,

                 wa_sales_cfgs_inst TO it_sales_cfgs_inst,

                 wa_sales_cfgs_refinst TO it_sales_cfgs_refinst.

        CLEAR : wa_sales_cfgs_ref,wa_sales_cfgs_inst,wa_sales_cfgs_refinst.

      ENDIF.

AND

        IF wa_salesdoc-charc IS NOT INITIAL.

          wa_sales_cfgs_value-config_id = lv_config.

          wa_sales_cfgs_value-inst_id   = lv_instance.

          wa_sales_cfgs_value-charc     = wa_salesdoc-charc.

          wa_sales_cfgs_value-value     = wa_salesdoc-value.

          wa_sales_cfgs_value-valcode   = 1.

          APPEND wa_sales_cfgs_value TO it_sales_cfgs_value.

          CLEAR wa_sales_cfgs_value.

        ENDIF.

In this way you can upload the Variant Configuration.

With Regards,

KT

Read only

0 Likes
14,968

I found the code, however I still can't insert the variant data...here is my variant code

*Configuration Ref

cfg_ref-posex = '000010'.  Your lv_posnr

cfg_ref-config_id = '000001'. Your lv_config
cfg_ref-root_id = '00000001'. Your lv_instance

APPEND cfg_ref.



*Configuration Ref Inst

cfg_ref_inst-posex = '000010'.  Your lv_posnr

cfg_ref_inst-config_id = '000001'. Your lv_config

cfg_ref_inst-inst_id = '00000001'. Your lv_instance
APPEND cfg_ref_inst.



*Configuration Inst

cfg_inst-config_id = '000001'. Your lv_config

cfg_inst-inst_id = '00000001'. Your lv_instance

cfg_inst-obj_type = 'MARA'.

cfg_inst-class_type = '300'.

cfg_inst-obj_key = MATERIAL.

cfg_inst-quantity = QTY.

cfg_inst-QUANTITY_UNIT = 'EA'.

cfg_inst-author = '8'.

append cfg_inst.



*Configuration Values

cfg_value-config_id = '000001'.  Your lv_config

cfg_value-inst_id = '00000001'.  Your lv_instance

cfg_value-charc = 'PRICE_POINT'.

cfg_value-value = VALUE.

cfg_value-valcode = 1.

*gt_order_cfgs_value-author = '8'.

APPEND cfg_value.

Read only

0 Likes
14,968

Hi Chris,

I have done something similar before but I was not able to directly insert values without calling functions to instantiate, read, and pass values into the configuration.  The function modules I used were: 'BAPI_CFG_CREATE', 'BAPI_CFG_READ' & 'CUX1_CHARACTS_READ'.  The first one was to create the instance, the second to get the tables to pass into the BAPI and the third was to read the characteristic values so I could tell which ones were required/optional, etc.

Regards,

Ryan Crosby

Read only

0 Likes
14,968

Ryan,

   Thanks for the reply...as I said...I'm very new to this and basically learning as I go and have found that this is not very user friendly so to speak.  I'm normally a .Net programmer but we are implementing SAP and I'm trying to learn.  The problem is is that there isn't many examples on how to do something...so any code examples you would have on how to populate the FM's you mentioned and then push them to the Bapi would be appreciated....

Thanks

Read only

0 Likes
14,968

Hi Chris,

I completely understand... there was a time that I was there as well and this is one of those instances where the code is not really released for use so I spent a lot of time researching the runtime behavior to figure out how to use it.  See my example below (mine is a contract BAPI but you'll notice the FM shell is basically the same):


* Get root instance for VC

  CALL FUNCTION 'BAPI_CFG_CREATE'

    EXPORTING

      object_id     = lv_matnr

      root_name     = lv_cuobj

    IMPORTING

      root_instance = lv_inst

    EXCEPTIONS

      error         = 1

      OTHERS        = 2.

  IF sy-subrc <> 0.

*   Unable to retrieve variant configuration

    CLEAR wa_msg.

    wa_msg-type = lc_e.

    wa_msg-text = 'Unable to retrieve variant configuration'(001).

    APPEND wa_msg TO output-mt_contract_create_response-messages.

    EXIT.

  ELSE.

    CLEAR wa_item.

    wa_item-material   = lv_cuobj.

    wa_item-po_itm_no  = lc_000010.

    wa_item-target_qty = 1.

    APPEND wa_item TO lt_items.

  ENDIF.

* Get values to include in BAPI call

  CALL FUNCTION 'BAPI_CFG_READ'

    EXPORTING

      object_id         = lv_matnr

    IMPORTING

      cfg_header_loaded = wa_cfg

    TABLES

      instances_loaded  = lt_inst

      values_loaded     = lt_value

      part_of_loaded    = lt_part_of.

* Move values from VC to tables for BAPI call

  MOVE-CORRESPONDING wa_cfg TO wa_cfgsref.

  wa_cfgsref-posex = lc_000010.

  APPEND wa_cfgsref TO lt_cfgsref.

  APPEND LINES OF lt_inst TO lt_cfgsinst.

  APPEND LINES OF lt_value TO lt_cfgsvalue.

  APPEND LINES OF lt_part_of TO lt_cfgspartof.

* Get characteristics

  CALL FUNCTION 'CUX1_CHARACTS_READ'

    EXPORTING

      instance                 = lv_inst

    TABLES

      characts                 = lt_chars

      characts_attributes      = lt_charatts

    EXCEPTIONS

      not_found                = 1

      no_characteristic_in_ddb = 2

      OTHERS                   = 3.

  IF sy-subrc <> 0.

*   Unable to retrieve variant configuration

    CLEAR wa_msg.

    wa_msg-type = lc_e.

    wa_msg-text = 'Unable to retrieve variant configuration'(001).

    APPEND wa_msg TO output-mt_contract_create_response-messages.

    EXIT.

  ENDIF.

* Add all charactistic/value pairs into instances

  READ TABLE lt_value INTO wa_value INDEX 1.

  MOVE-CORRESPONDING wa_value TO wa_cfgsvalue.

  LOOP AT input-mt_contract_create_request-characteristics INTO

                                                wa_char_in.

    READ TABLE lt_charatts INTO wa_charatt WITH KEY

                                        atbez = wa_char_in-description.

    IF sy-subrc = 0.

      lv_tabix = sy-tabix.

      wa_cfgsvalue-charc     = wa_charatt-atnam.

      wa_cfgsvalue-charc_txt = wa_char_in-description.

      wa_cfgsvalue-value     = wa_char_in-values.

      wa_cfgsvalue-value_txt = wa_char_in-values.

      APPEND wa_cfgsvalue TO lt_cfgsvalue.

      DELETE lt_charatts INDEX lv_tabix.

      IF wa_char_in-description CS 'duration'.

        lv_valper = wa_char_in-values+0(2).

      ENDIF.

    ENDIF.

  ENDLOOP.

* Ensure that all required characteristics have been supplied a value

  LOOP AT lt_charatts INTO wa_charatt WHERE aterf = lc_x.

*   Unable to retrieve variant configuration

    CLEAR wa_msg.

    wa_msg-type = lc_e.

    CONCATENATE 'Required characteristic value:'(006) wa_charatt-atbez

                INTO wa_msg-text SEPARATED BY SPACE.

    APPEND wa_msg TO output-mt_contract_create_response-messages.

  ENDLOOP.

  IF sy-subrc = 0.

    EXIT.

  ENDIF.

* Create contract - this BAPI is not released for use so there is no

* support on OSS when using it

  CALL FUNCTION 'BAPI_CONTRACT_CREATEFROMDATA'

    EXPORTING

      contract_header_in    = wa_bapisdhd

    IMPORTING

      salesdocument         = lv_vbeln

    TABLES

      return                = lt_return

      contract_items_in     = lt_items

      contract_partners     = lt_partners

      contract_cfgs_ref     = lt_cfgsref

      contract_cfgs_inst    = lt_cfgsinst

      contract_cfgs_part_of = lt_cfgspartof

      contract_cfgs_value   = lt_cfgsvalue

      contract_data_in      = lt_contract.

One thing to note is my requirement was only for contracts with one item so if you are working with multiple items you may have to instantiate the configuration multiple times.

Regards,

Ryan Crosby

Read only

0 Likes
14,968

Hi Christopher,

The "POSEX" of variant configuration table is related with "PO_ITM_NO" of item table. For more information please refer SAP Note "549563".

With Regards,

KT.

Read only

0 Likes
14,968

Kanak,  That was my problem....I knew it was going to be something like that...thanks...