Application Development 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: 

BAPI to create a credit memo request

Former Member
0 Kudos

Is there a standard BAPI that can create a credit memo request?

If not, then can we copy a standard BAPI to a Z-version and amend that?

Sample code appreciated.

Thanks,

John

7 REPLIES 7

ferry_lianto
Active Contributor
0 Kudos

Hi John,

Please check this thread for sample codes.

https://forums.sdn.sap.com/click.jspa?searchID=46332&messageID=2304043

Regards,

Ferry Lianto

Former Member
0 Kudos

Hi,

Try to use the BAPI BAPI_SALESDOCU_CREATEFROMDATA1..

Thanks,

Naren

Former Member
0 Kudos

Partial success.

I have been able to create a credit memo request (CMR) using BAPI_SALESDOCU_CREATEFROMDATA by setting business_object to 'BUS2094'.

The BAPI, however, fails to cater for two things that I need:

(1) service-rendered date;

(2) creating with reference to an invoice.

I was surprised about (2), since it has the fields ref_doc and ref_doc_ca in order_header_in.

I was setting ref_doc to an invoice number and ref_doc_ca to 'M' (= invoice).

Then I tried BAPI_SALESORDER_CREATEFROMDAT1. This failed with error message V1761 in the 'return' export parameter:

"Unpermitted combination of business object BUS2032 and sales doc. category K".

BUS2032 (= sales order) must be hard-coded somewhere in the BAPI, and document category K must have been deduced from my document type.

I can find no business-object parameter.

A year or two ago, on a project that was abandoned, we had, I think, a similar problem with BAPI_SALESORDER_CREATEFROMDAT2. Again there was hard-coding in the BAPI that prevented the creation of a CMR, but I forget the details.

So problem not solved.

Former Member
0 Kudos

HI John

You can consider using FM: SD_SALESDOCUMENT_CREATE which is internally called by the above mentioned BAPI's.

Kind Regards

Eswar

Former Member
0 Kudos

The suggestion to use SD_SALESDOCUMENT_CREATE proved fruitful.

For creating with reference to an invoice:

Setting ref_doc and ref_doc_ca for each line-item in sales_item_in seems necessary for the creation of the document flow. Maybe it also brought through other data from the invoice?

Setting ref_doc and refdoc_cat for sales_header_in caused header text (and maybe other stuff?) to be brought through from the invoice.

It looks like it will do bills of materials - there's hg_lv_item in sales_items_in - but I've yet to try that.

Here's the code of a demo program for a one-line CMR.

REPORT YJNM_CREATE_CMR5 .

  • Uses SD_SALESDOCUMENT_CREATE

parameters:

p_vgbel like vbrk-vbeln default '90175587', "reference invoice

p_fkdat like vbkd-fkdat default sy-datum, "billing date

p_auart like tvak-auart default 'Z002', "sales document type

p_vkorg like tvko-vkorg default '3', "sales org.

p_vtweg like tvtw-vtweg default '01', "distr. chan.

p_spart like tspa-spart default '06', "division

p_vkbur like tvbur-vkbur default '034', "sales office

p_kunnr like kna1-kunnr default '16797', "sold-to

p_bstnk like vbak-bstnk default 'pono00000017',

"PO number

p_bname like vbak-bname default 'Bartholomew Snodgrass',

"name of orderer

p_augru like tvau-augru default '002'. "order reason

selection-screen skip.

selection-screen begin of block i1 with frame title text-001.

parameters:

p_matnr like mara-matnr default '27585', "material number

p_zmeng type dzmengc default '65', "target quantity

p_netwr like vbap-netwr default '1.20', "net amount

p_arktx like vbap-arktx default 'Afternoon tea',

"short text

p_fbuda like vbkd-fbuda default '20060109'."service-rendered date

selection-screen end of block i1.

----


data:

return like bapiret2,

returns like table of return,

salesdocument_ex like bapivbeln-vbeln,

sales_header_in like bapisdhd1,

sales_item_in like bapisditm,

sales_items_in like table of sales_item_in,

sales_condition_in like bapicond,

sales_conditions_in like table of sales_condition_in,

sales_partner like bapiparnr,

sales_partners like table of sales_partner,

incomplete_log like bapiincomp,

incomplete_logs like table of incomplete_log.

----


perform:

fill_sales_header_in,

fill_sales_items_in,

fill_sales_partners,

fill_sales_conditions_in,

call_bapi.

----


FORM call_bapi .

CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'

EXPORTING

  • SALESDOCUMENT =

SALES_HEADER_IN = sales_header_in

  • SALES_HEADER_INX =

  • SENDER =

  • BINARY_RELATIONSHIPTYPE = ' '

  • INT_NUMBER_ASSIGNMENT = ' '

  • BEHAVE_WHEN_ERROR = ' '

  • LOGIC_SWITCH = ' '

BUSINESS_OBJECT = 'BUS2094' "cred. mem. req.

  • TESTRUN =

  • CONVERT_PARVW_AUART = ' '

  • STATUS_BUFFER_REFRESH = 'X'

IMPORTING

SALESDOCUMENT_EX = salesdocument_ex

  • SALES_HEADER_OUT =

  • SALES_HEADER_STATUS =

TABLES

RETURN = returns

SALES_ITEMS_IN = sales_items_in

  • SALES_ITEMS_INX =

SALES_PARTNERS = sales_partners

  • SALES_SCHEDULES_IN =

  • SALES_SCHEDULES_INX =

SALES_CONDITIONS_IN = sales_conditions_in

  • SALES_CONDITIONS_INX =

  • SALES_CFGS_REF =

  • SALES_CFGS_INST =

  • SALES_CFGS_PART_OF =

  • SALES_CFGS_VALUE =

  • SALES_CFGS_BLOB =

  • SALES_CFGS_VK =

  • SALES_CFGS_REFINST =

  • SALES_CCARD =

  • SALES_TEXT =

  • SALES_KEYS =

  • SALES_CONTRACT_IN =

  • SALES_CONTRACT_INX =

  • EXTENSIONIN =

  • PARTNERADDRESSES =

  • SALES_SCHED_CONF_IN =

  • ITEMS_EX =

  • SCHEDULE_EX =

  • BUSINESS_EX =

INCOMPLETE_LOG = incomplete_logs

  • EXTENSIONEX =

  • CONDITIONS_EX =

  • PARTNERS_EX =

  • TEXTHEADERS_EX =

  • TEXTLINES_EX =

  • BATCH_CHARC =

.

perform imcompletion_log.

loop at returns into return where type ne 'S'.

message id return-id type 'I' number return-number

with

return-message_v1

return-message_v2

return-message_v3

return-message_v4.

endloop.

if salesdocument_ex is initial.

write: / 'Document not created'.

else.

commit work.

write: / 'Sales document number:', salesdocument_ex.

endif.

ENDFORM. " call_bapi

----


FORM fill_sales_header_in .

clear sales_header_in.

move:

p_fkdat to sales_header_in-bill_date,

p_vgbel to sales_header_in-ref_doc, "invoice number

'M' to sales_header_in-refdoc_cat, "invoice

p_auart to sales_header_in-doc_type,

p_vkorg to sales_header_in-sales_org,

p_vtweg to sales_header_in-distr_chan,

p_spart to sales_header_in-division,

p_vkbur to sales_header_in-sales_off,

p_bstnk to sales_header_in-purch_no_c,

p_bname to sales_header_in-name,

p_augru to sales_header_in-ord_reason.

ENDFORM. " fill_sales_header_in

----


FORM fill_sales_partners .

clear sales_partner.

sales_partner-partn_role = 'AG'.

sales_partner-partn_numb = p_kunnr.

append sales_partner to sales_partners.

ENDFORM. " fill_sales_partners

----


FORM fill_sales_items_in .

perform one_item_in using:

10

p_matnr

p_zmeng

p_netwr

'GBP'

p_arktx

p_fbuda.

ENDFORM. " fill_sales_items_in

----


FORM one_item_in using value(p_itm_number)

value(p_p_matnr)

value(p_p_zmeng)

value(p_p_netwr)

value(p_p_waerk)

value(p_p_arktx)

value(p_p_fbuda).

clear sales_item_in.

move:

p_itm_number to sales_item_in-itm_number,

p_fkdat to sales_item_in-bill_date,

p_vgbel to sales_item_in-ref_doc, "invoice number

'M' to sales_item_in-ref_doc_ca, "invoice

p_p_matnr to sales_item_in-material,

p_p_zmeng to sales_item_in-target_qty,

p_p_netwr to sales_item_in-target_val,

p_p_waerk to sales_item_in-currency,

p_p_arktx to sales_item_in-short_text,

p_p_fbuda to sales_item_in-serv_date.

append sales_item_in to sales_items_in.

ENDFORM. " one_item_in

----


FORM fill_sales_conditions_in .

refresh sales_conditions_in.

perform one_sales_condition_in using:

10

p_netwr

'GBP'.

ENDFORM. " fill_sales_conditions_in

----


FORM one_sales_condition_in using value(p_itm_number)

value(p_p_netwr)

value(p_p_waerk).

clear sales_condition_in.

move:

p_itm_number to sales_condition_in-itm_number,

'PR00' to sales_condition_in-cond_type,

p_p_netwr to sales_condition_in-cond_value,

p_p_waerk to sales_condition_in-currency.

append sales_condition_in to sales_conditions_in.

ENDFORM. " one_sales_condition_in

----


FORM imcompletion_log .

loop at incomplete_logs into incomplete_log.

write: / incomplete_log-itm_number,

incomplete_log-field_text,

incomplete_log-table_name,

incomplete_log-field_name.

endloop.

ENDFORM. " imcompletion_log

0 Kudos

<b>Setting ref_doc and ref_doc_ca for each line-item in sales_item_in</b>

You got the right point.

<b>Setting ref_doc and refdoc_cat for sales_header_in caused header text (and maybe other stuff?) to be brought through from the invoice</b>

This might depend on the copy control defined between the document types.

Kind Regards

Eswar

Former Member
0 Kudos

Instead of using FM SD_SALESDOCUMENT_CREATE we should use

BAPI_SALESDOCU_CREATEFROMDATA , even not being releleased his use is

supported by SAP .