Update SD condition records using FM/BAPI - abap

I am trying to update DATBI field of A* pricing condition tables using the data coming from excel file.I have tried below approaches but unable to update the entry as DATBI is key field. It creates new record instead of updating the existing one. Approaches:
FM: RV_CONDITION_COPY(with mai tain_ mode as B)
FM: RV_CONDITION_MAINTENANCE(using maintain_mode as B)
BAPI_PRICES_CONDITIONS(It changes KONH table but not A* table.Also, as per note#1135984,we shouldn't be using it)
FM:RV_KONDITION_SICHERN_V13A in update mode(This gives sy-subrc 4 in this FM post update A* from table__ syntax as the datbi is new coming from my excel file)
Idoc COND_A04(This also creates new entry instead of updating existing one)
BDC is the only approach that I can think of but looking at VK12 screen, it has been observed that based on key combination and its underlying A* tables, it should be dynamic.
Can you please help in this regard?Is there any dynamic BDC for VK12?

Undoubtedly we shouldn't use BAPI_PRICES_CONDITIONS, and note 94443 describes the problems it can cause when you push incorrect conditions into system, but sometime there is no way to avoid it. I tried this FM and it definitely worked on my system.
Here is the code for updating A009 table:
* header table
APPEND INITIAL LINE TO lt_head ASSIGNING <fs_head>.
<fs_head>-operation = '009'.
<fs_head>-cond_usage = 'A'.
<fs_head>-table_no = '007'.
<fs_head>-applicatio = 'V'.
<fs_head>-cond_type = 'ZP15'.
<fs_head>-valid_from = '20210103'.
<fs_head>-valid_to = '99991231'.
<fs_head>-cond_no = '$000000001'.
<fs_head>-varkey = 'BE110101001000635199993112'.
** items
APPEND INITIAL LINE TO lt_konh ASSIGNING <fs_konh>.
<fs_konh>-operation = '009'.
<fs_konh>-cond_no = '$000000001'.
<fs_konh>-cond_usage = 'A'.
<fs_konh>-table_no = '007'.
<fs_konh>-applicatio = 'V'.
<fs_konh>-cond_type = 'ZP15'.
<fs_konh>-valid_from = '20210103'.
<fs_konh>-valid_to = '99991231'.
<fs_konh>-created_by = sy-uname.
<fs_konh>-creat_date = sy-datum.
APPEND INITIAL LINE TO lt_konp ASSIGNING <fs_konp>.
<fs_konp>-operation = '009'.
<fs_konp>-cond_no = '$000000001'.
<fs_konp>-cond_count = '01'.
<fs_konp>-applicatio = 'V'.
<fs_konp>-cond_type = 'ZP15'.
<fs_konp>-scaletype = 'A'.
<fs_konp>-scalebasin = space.
<fs_konp>-scale_qty = '0'.
<fs_konp>-cond_p_unt = '1'.
<fs_konp>-cond_unit = 'ΠΆ'.
<fs_konp>-calctypcon = 'C'.
<fs_konp>-cond_value = '160'.
<fs_konp>-condcurr = 'EUR'.
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
EXPORTING
pi_initialmode = abap_true
TABLES
ti_bapicondct = lt_head
ti_bapicondhd = lt_konh
ti_bapicondit = lt_konp
ti_bapicondqs = lt_konm
ti_bapicondvs = lt_konw
to_bapiret2 = lt_return
to_bapiknumhs = lt_knumh
to_mem_initial = lt_buffer
EXCEPTIONS
update_error = 1
OTHERS = 2.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = abap_true.
I tried the above code and record with the specified key was properly updated with new valid to field (DATAB). Updating DATBI is a bit more complicated since it is included in the primary key and cannot be updated in one step. You must delete the record with the old DATBI and the create the new one. In this blog you can read more about how to use this BAPI:
https://blogs.sap.com/2019/07/22/how-to-use-bapi_prices_conditions-to-mass-upload-price-conditions/

Related

How do I create a service ticket using CRM_ORDER_MAINTAIN?

I'm trying to learn the Function Module CRM_ORDER_MAINTAIN and so far I managed to create a standard order with partners and their roles, however now I'm having trouble creating a service ticket with its required fields.
I tried debugging the FM when calling it from WEBUI to see what structures and tables are to be filled but I'm having a bit of trouble figuring out which ones to fill and which ones are generated and don't know if I'm missing something small somewhere that's causing it not to save.
I think the error lies in this part of the code that handles Categorization, because the other code that handles adding partners worked for the standard order.
I'm not filling any GUIDS and only filling HANDLES because that's what I did for the standard order.
*****categorization: motive+submotive******
clear ls_input_fields.
clear ls_fieldsname.
ls_fieldsname-fieldname = 'CONC_KEY'.
INSERT ls_fieldsname INTO TABLE ls_input_fields-field_names.
ls_input_fields-ref_handle = 1."
ls_input_fields-ref_kind = gc_object_kind-orderadm_h.
ls_input_fields-objectname = 'SERVICE_OS'.
ls_subject-ref_handle = ls_orderadm_h-handle.
ls_subject-ref_handle_h = ls_orderadm_h-handle.
ls_subject-katalogart = 'Z1'.
ls_subject-codegruppe = 'ZCA00001'.
ls_subject-code = 'Z044'.
append ls_subject TO ls_osset-subject.
ls_osset-ref_handle = ls_orderadm_h-handle.
ls_osset-profile_type = 'A'.
ls_osset-subject_profile = 'ZCCCAST'.
append ls_osset to ls_service_os-osset.
ls_service_os-ref_handle = ls_orderadm_h-handle.
append ls_service_os to it_service_os.
INSERT ls_input_fields INTO TABLE lt_input_fields.
ls_subject-ref_handle = ls_orderadm_h-handle.
ls_subject-ref_handle_h = ls_orderadm_h-handle.
ls_subject-katalogart = 'Z1'.
ls_subject-codegruppe = 'ZCA00002'.
ls_subject-code = 'Z009'.
append ls_subject TO ls_osset-subject.
ls_osset-ref_handle = ls_orderadm_h-handle.
ls_osset-profile_type = 'A'.
ls_osset-subject_profile = 'ZCCCAST'.
append ls_osset to ls_service_os-osset.
ls_service_os-ref_handle = ls_orderadm_h-handle.
append ls_service_os to it_service_os.
INSERT ls_input_fields INTO TABLE lt_input_fields.
Any help is appreciated
For Service OS there is a multilevel categorization up to four levels. You can check the hierarchy via CRM_ORDER_READ FM in parameter ET_SERVICE_OS.
Also, you can check the hierarchy in the below structure CRMT_SRV_OSSET_WRK
To maintain the data for more than 2 levels of hierarchy you have to create a new ref GUID. You have to prepare and fill data to Create OS as below
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_16 = lv_ref_guid.
ls_subject-ref_handle = '0000000000'.
ls_subject-ref_guid = lv_ref_guid. " newly created ref GUID
ls_subject-cat_id = 'As per your req.'.
ls_subject-katalog_type = ''.
ls_subject-mode = 'A'. "For creation A, for update B
" maintain other Subject parameter as per your requirement
APPEND ls_subject TO lt_subject.
" you can skip preparing Ref-Object structure if you don't want to update
ls_refobj-ref_guid = lv_ref_guid. " newly created ref GUID
ls_refobj-product_id = "Product ID". " optional
ls_refobj-ref_handle = '0000000000'.
ls_refobj-main_object = abap_true.
ls_refobj-mode = 'A'. " mode A for creating, B for update
INSERT ls_refobj INTO TABLE lt_refobj.
ls_osset-ref_handle = '0000000000'.
ls_osset-ref_guid = "Header or Item GUID of Service".
ls_osset-subject_profile = 'SERVICE'.
ls_osset-profile_type = 'A'. " service profile type
ls_osset-refobject = lt_refobj.
ls_osset-subject = lt_subject.
INSERT ls_osset INTO TABLE lt_osset.
ls_service_os-ref_guid = "Header or Item GUID of Service".
ls_service_os-ref_kind = "A or B". " A for Header and B for Item
ls_service_os-osset = lt_osset.
INSERT ls_service_os INTO TABLE lt_service_os.

How to remove batch number on delivery?

I need to remove the batch number (LIPS-CHARG) on the delivery item and tried with BAPI_OUTB_DELIVERY_CHANGE without success.
I can change the delivery quantity (LIPS-LFIMG) correctly with this BAPI and change the picking number (LIPS-PKIMG) with WS_DELIVERY_UPDATE_2, but can't remove the batch number.
Does someone already remove the batch number? Don't need to use this BAPI, can be another one. I just need to remove the batch in any way.
The following code works to change the delivery quantity and picking quantity. At a certain moment, I change picking quantity to zero and at this moment I need to remove the batch number.
"&**** Begin structures BAPI_OUTB_DELIVERY_CHANGE *******
ls_header_data-deliv_numb = p_vbeln.
ls_header_control-deliv_numb = p_vbeln.
APPEND INITIAL LINE TO lt_item_data ASSIGNING FIELD-SYMBOL(<ls_item_data>).
<ls_item_data>-deliv_numb = p_vbeln.
<ls_item_data>-deliv_item = lv_posnn.
<ls_item_data>-material = <ls_alv>-matnr.
<ls_item_data>-batch = <ls_alv>-charg.
<ls_item_data>-hieraritem = <ls_lips>-posnr.
<ls_item_data>-usehieritm = '1'.
<ls_item_data>-dlv_qty = lv_delivery_qtd. "my delivery quantity
<ls_item_data>-dlv_qty_imunit = lv_delivery_qtd. "my delivery quantity
<ls_item_data>-fact_unit_nom = <ls_lips>-umvkz.
<ls_item_data>-fact_unit_denom = <ls_lips>-umvkn.
<ls_item_data>-sales_unit = <ls_lips>-vrkme.
APPEND INITIAL LINE TO lt_item_control ASSIGNING FIELD-SYMBOL(<ls_item_control>).
<ls_item_control>-deliv_numb = p_vbeln.
<ls_item_control>-deliv_item = lv_posnn.
<ls_item_control>-chg_delqty = 'X'.
"&**** End structures BAPI_OUTB_DELIVERY_CHANGE *******
CALL FUNCTION 'BAPI_OUTB_DELIVERY_CHANGE'
EXPORTING
header_data = ls_header_data
header_control = ls_header_control
delivery = p_vbeln
TABLES
item_data = lt_item_data
item_control = lt_item_control
return = lt_return.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
"&**** Begin structures WS_DELIVERY_UPDATE_2 *******
APPEND INITIAL LINE TO lt_vbpok ASSIGNING FIELD-SYMBOL(<ls_vbpok>).
<ls_vbpok>-vbeln_vl = <ls_lips>-vbeln.
<ls_vbpok>-posnr_vl = <ls_lips>-posnr.
<ls_vbpok>-vbeln = <ls_lips>-vgbel.
<ls_vbpok>-posnn = <ls_lips>-vgpos.
<ls_vbpok>-matnr = <ls_lips>-matnr.
<ls_vbpok>-werks = <ls_lips>-werks.
<ls_vbpok>-umvkz = <ls_lips>-umvkz.
<ls_vbpok>-umvkn = <ls_lips>-umvkn.
<ls_vbpok>-gewei = <ls_lips>-gewei.
<ls_vbpok>-charg = <ls_alv>-charg.
<ls_vbpok>-pikmg = lv_picking_qtd. "my picking quantity
ls_vbkok-vbeln_vl = <ls_lips>-vbeln.
ls_vbkok-vbtyp_vl = 'J'.
ls_vbkok-komue = 'X'.
ls_vbkok-gewei = <ls_lips>-gewei.
ls_vbkok-anzpk = lv_anzpk.
ls_vbkok-kzapk = 'X'.
"&**** End structures WS_DELIVERY_UPDATE_2 *******
CALL FUNCTION 'WS_DELIVERY_UPDATE_2'
EXPORTING
vbkok_wa = ls_vbkok
delivery = p_vbeln
synchron = abap_true
update_picking = abap_true
nicht_sperren_1 = abap_true
commit = abap_false
if_error_messages_send = abap_false
IMPORTING
ef_error_any = ls_error-any
ef_error_in_item_deletion = ls_error-in_item_deletion
ef_error_in_pod_update = ls_error-in_pod_update
ef_error_in_interface = ls_error-in_interface
ef_error_in_goods_issue = ls_error-in_goods_issue
ef_error_in_final_check = ls_error-in_final_check
ef_error_partner_update = ls_error-partner_update
ef_error_sernr_update = ls_error-sernr_update
TABLES
vbpok_tab = lt_vbpok.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
I found my error!
When we need to remove the batch number, we don't need to send value on ITEM_DATA-DELIV_ITEM.
In my code, I was sending '90000001' on ITEM_DATA-DELIV_ITEM and '00010' on ITEM_DATA-USEHIERITM, but to work I just send ITEM_DATA-USEHIERITM and the batch number was removed correctly.
Thanks all!

Add business objects to contacts

I've successfully created a contact in the SAP IS-U (Release 618) system using the function: BCONTACT_CREATE
EDIT:
Since this question was voted "close" for being "too broad" - here's some very specific code:
DATA:
ls_contact TYPE bpc01_bcontact_auto,
ls_contact_properties TYPE bcont,
lv_contact_text TYPE string,
lv_bp TYPE bu_partner,
lv_bpcontact_id TYPE ct_contact,
lv_no_dialog TYPE flag VALUE abap_true,
lv_repid TYPE syst-repid.
* Main logic
lv_contact_text = 'Test'.
lv_bp = '0010000062'.
ls_contact_properties-cclass = '0003'.
ls_contact_properties-activity = '0001'.
ls_contact_properties-f_coming = '3'.
* Mapping
*--------------------------------------------------------------------*
ls_contact-notice-line = lv_contact_text.
ls_contact-bcontd = ls_contact_properties.
* set flag to use auto data
ls_contact-bcontd_use = abap_true.
lv_repid = sy-repid.
CALL FUNCTION 'BCONTACT_CREATE'
EXPORTING
x_no_dialog = lv_no_dialog
x_auto = ls_contact
x_prgcontext = lv_repid
x_partner = lv_bp
IMPORTING
y_new_bpcontact = lv_bpcontact_id
EXCEPTIONS
existing = 1
foreign_lock = 2
number_error = 3
general_fault = 4
input_error = 5
not_authorized = 6
OTHERS = 7.
When I open the created contact in the BCT2 transaction I see the nothing under Business-Objects:
How can I programmatically add a business object to a contact, so that it is displayed here like this?
I found it a solution!
First create variables (a table and a structure for filling the table) for your business objects that you want to add (I saw some code that had a limit of 5 so I just set that too to be safe):
lt_business_objs TYPE TABLE OF bpc_obj INITIAL SIZE 5,
ls_business_obj TYPE bpc_obj,
Next append your object, in this example I'm just appending one:
* Append business objects
*--------------------------------------------------------------------*
ls_business_obj-objkey = 'The value here may be your business object input value'.
ls_business_obj-objrole = 'DEFAULT'. "Don't know what this is for...
ls_business_obj-objtype = 'OBJECT_NAME'. "Name of your business object - seen in table TOJTB
APPEND ls_business_obj TO lt_business_objs.
And lastly put the object list into the contact structure:
ls_contact-iobjects = lt_business_objs.

Add table rows to a TR programmatically?

I have a problem with adding rows of table to the transport request in programming way.
When i wrote down the transport request number i get the error:
You cannot use request EAMK913244
the code I use is
data lt_variable_changed type table of ztable_task2.
data: l_request type trkorr,
lt_e071 type tr_objects,
lt_e071k type tr_keys,
lv_tabkey type trobj_name,
ls_e071 type e071,
ls_e071k type e071k.
ls_e071-pgmid = 'R3TR'.
ls_e071-object = 'TABU'. "for table
ls_e071-obj_name = 'ZTABLE_TASK2'.
ls_e071-objfunc = 'K'.
append ls_e071 to lt_e071.
loop at lt_variable_changed into ls_variable.
lv_tabkey = ls_variable-num.
ls_e071k-pgmid = 'R3TR'.
ls_e071k-object = 'TABU'.
ls_e071k-objname = 'ZTABLE_TASK2'.
ls_e071k-mastertype = 'TABU'.
ls_e071k-mastername = 'ZTABLE_TASK2'.
ls_e071k-tabkey = lv_tabkey.
append ls_e071k to lt_e071k.
endloop.
call function 'TR_REQUEST_CHOICE'
exporting
iv_suppress_dialog = 'X'
iv_request = var_query
it_e071 = lt_e071
it_e071k = lt_e071k.
message 'Ok' type 'I'.
Screen from se01:
Thanks for help and good luck!
three function modules shall be used to transport changes
call function 'TR_ORDER_CHOICE_CORRECTION'
exporting
iv_category = 'CUST'
importing
ev_order = ev_request
ev_task = ev_task
exceptions
invalid_category = 1
no_correction_selected = 2
others = 3.
call function 'TR_OBJECTS_CHECK'
exporting
iv_no_show_option = abap_true
tables
wt_ko200 = lt_ko200_customizing
wt_e071k = lt_e071k_customizing
exceptions
cancel_edit_other_error = 1
show_only_other_error = 2
others = 3.
call function 'TR_OBJECTS_INSERT'
exporting
wi_order = lv_request
iv_no_show_option = abap_true
tables
wt_ko200 = lt_ko200_customizing
wt_e071k = lt_e071k_customizing
exceptions
cancel_edit_other_error = 1
show_only_other_error = 2
others = 3.
You can also use object-oriented approach for transporting tables.
This piece creates a customizing request and puts contents of a table in it:
DATA(instance) = cl_adt_cts_management=>create_instance( ).
TRY.
instance->insert_objects_in_wb_request( EXPORTING pgmid = 'R3TR'
object = 'TABU'
obj_name = CONV trobj_name( 'Z_TABLE' )
CHANGING trkorr = l_trkorr ).
CATCH cx_adt_cts_insert_error.
RETURN.
ENDTRY.
This piece uses ADT CTS classes and is very flexible. Despite the name wb_request this method is adaptive and will create workbench or customizing request depending on the objects passed.

Update BSEG-ZUONR with Function Module

I successfully change BKPF-BKTXT with FM CHANGE_DOCUMENT but why can't I change BSEG-ZUONR with FM CHANGE_DOCUMENT too?
Here's the FM CHANGE_DOCUMENT:
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
T_BKDF = t_bkdf
T_BKPF = t_bkpf
T_BSEC = t_bsec
T_BSED = t_bsed
T_BSEG = t_bseg
T_BSET = t_bset
* T_BSEG_ADD =
.
Here's the code to change BKPF-BKTXT (succeeded):
wa_t_bkpf-mandt = sy-mandt.
wa_t_bkpf-bukrs = '1000'.
wa_t_bkpf-gjahr = gjahr_import.
wa_t_bkpf-belnr = belnr_import.
wa_t_bkpf-bktxt = zuonr_import.
APPEND wa_t_bkpf TO t_bkpf.
Here's the code to change BSEG-ZUONR (failed):
wa_t_bseg-mandt = sy-mandt.
wa_t_bseg-bukrs = '1000'.
wa_t_bseg-gjahr = gjahr_import.
wa_t_bseg-belnr = belnr_import.
wa_t_bseg-buzei = '1'.
wa_t_bseg-zuonr = zuonr_import.
APPEND wa_t_bseg TO t_bseg.
As author has no time to confirm, I can do this for him as I just tested this case.
If we pass to FM all parameters from its signature the update runs smoothly. For example, like this:
DATA: lt_bkdf TYPE TABLE OF bkdf,
lt_bkpf TYPE TABLE OF bkpf,
wa_bkpf TYPE bkpf,
lt_bsec TYPE TABLE OF bsec,
wa_bseg TYPE bseg,
lt_bsed TYPE TABLE OF bsed,
lt_bseg TYPE TABLE OF bseg,
lt_bset TYPE TABLE OF bset.
wa_bkpf-mandt = sy-mandt.
wa_bkpf-bukrs = '5900'.
wa_bkpf-gjahr = gjahr_import.
wa_bkpf-belnr = belnr_import.
wa_bkpf-bktxt = 'Batch'.
APPEND wa_bkpf TO lt_bkpf.
wa_bseg-mandt = sy-mandt.
wa_bseg-bukrs = '5900'.
wa_bseg-gjahr = gjahr_import.
wa_bseg-belnr = belnr_import.
wa_bseg-buzei = '1'.
wa_bseg-zuonr = '20151131'.
APPEND wa_bseg TO lt_bseg.
CALL FUNCTION 'CHANGE_DOCUMENT'
TABLES
t_bkdf = lt_bkdf
t_bkpf = lt_bkpf
t_bsec = lt_bsec
t_bsed = lt_bsed
t_bseg = lt_bseg
t_bset = lt_bset
.
COMMIT WORK.
All FM table parameters except the last one are mandatory.
Do not use this FM
CALL FUNCTION 'CHANGE_DOCUMENT'
This FM is changing all other fields to initial if not provided.
CALL FUNCTION 'FI_DOCUMENT_CHANGE'
It seems that this FM cannot be used to change line item which has account type (BSEG-KOART) - 'S' (GL Account).
Try this FM:
'FI_ITEMS_MASS_CHANGE'
The field zuonr references to an object it belongs to.
For example a purchase order.
Lets asume you pay a position of a purchase order.
A document in bkpf/bseg is created (and more).
Bseg-Zuonr contains the number of this purchase order position.
If you were allowed to change this field, you would destroy the referential integrity of the data. It would point to a purchase order position it was not created from or one that doesn't exist at all.
So from a business standpoint it makes no sense to ever change this field after it is created, therefore SAP will never allow to change it.