Standard service - a fixed amount of work that is performed at a fixed price.  Post Service

  

Tuesday, 26 February 2019 15:45

How to pass Reversal Date & Reason to BAPI_ACC_DOCUMENT_POST?

Written by 
Rate this item
(0 votes)

One reason why the programmer’s job is the most exciting one is; you learn new stuff every day. No matter, whether you are a fresher or a developer

with more than decades of experience. Every other day, you learn about a new syntax. You get to know about a new concept. Out of the blue, you suddenly understand and realize why you have been programming a certain way your whole life.

Those are the “flash of the genius” moments. ?

The other day, my client gave me a very simple requirement. Create an Invoicing Document with Reversal Reason and Reversal date(It is called Auto Reversing Document in our project). Please note, it is not reversal posting. It is a regular posting (Accrual/Deferral Doc) (using t-code FBS1) which is ready for reverse posting (using t-code F.81).

Normal Document (t-code FB01) looks like below –

Accrual/Deferral Doc (t-code FBS1) looks like below –

Check, it has Reversal Reason (STGRD) and Reversal Date (STODT) as mandatory header field.

Also ReadMy first ABAP Program in S/4HANA

With little experience, we learn that BAPI ‘BAPI_ACC_DOCUMENT_POST’ is the one which would help us to post accounting document.

But the tricky part is, the DOCUMENTHEADER parameter has a field called reversal reason, but it does not have the reversal date.

Since the normal BAPI Parameters and tables do not have the required fields, therefore we need to take help of the EXTENSION2 Tables Parameter of BAPI BAPI_ACC_DOCUMENT_POST.

The high level steps are:

  1. Pass structure ‘ACCIT’, valuepart1 as Reversal Date and valuepart2 as Reversal Reason in EXTENSION2 of BAPI
  2. Implement BADI ‘ACC_DOCUMENT’
  3. Modify C_ACCIT table in method ‘CHANGE’ of BADI ‘ACC_DOCUMENT’

Let’s do it.

Also CheckGPS like tool in SAP using Google Map API

Go to t-code SE18 and provide the BADI name.

Check if the BADI is already implemented in your project. There is not custom implementation in our project.

Hit Implementation->Create

When you try to activate, it would give the below error.

BKPFF is the Reference for us.

Once you provide the filter, it activates successfully.

If you would be asked for the New Enhancement Implementation, just give the name and text and hit the create button.

Also CheckCreate your first OData Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<code>*WorkareaforExtension2fromBAPI
DATA:lwa_extension2TYPEbapiparex.
* Field Symbol for Accounting Interface: Item Information
FIELD-SYMBOLS&lt;ls_accit>TYPEaccit.
* C_ACCIT would have data. Just update the Reversal Reason and Date
LOOP ATc_accitASSIGNING&lt;ls_accit>.
READ TABLEc_extension2INTOlwa_extension2
WITHKEYstructure='ACCIT'.
IFsy-subrc=0.
&lt;ls_accit>-stodt=lwa_extension2-valuepart1." Reversal Reason
&lt;ls_accit>-stgrd=lwa_extension2-valuepart2." Reversal Date
ENDIF.
ENDLOOP.</code>

Activate the method and then run your job which creates the document with Reversal Reason and Reversal Date. After the document is created, go to t-code FB03 and check the data.

T-Code FB03

Go to the header and check the reversal date and reason.

We are sure; someone would surely get this requirement in their project. Now, you know what to do. If there are other fields to be enhanced, the method is the same. Enhance the extension and find the right place to populate the new extension fields.

Free Video Training on SAP DebuggingSAP Debugging for Non-Technical Consultants and ABAP Beginners

Below is a code snippet showing how to use the BAPI ‘BAPI_ACC_DOCUMENT_POST’ and also pass the Extension2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<code>*&amp;---------------------------------------------------------------------*
*&amp; Form CREATE_AUTO_REVERSING_DOC
*&amp;---------------------------------------------------------------------*
* Create Auto Reversing Document using BAPI
*----------------------------------------------------------------------*
FORMcreate_auto_reversing_doc.
DATA:lwa_documentheaderTYPEbapiache09,
lwa_lease_mapTYPEzfi_lease_map,
lwa_accountglTYPEbapiacgl09,
wa_arTYPEbapiacar09,
lwa_amountTYPEbapiaccr09,
lwa_returnTYPEbapiret2,
lv_costcTYPEkostl,
lv_lenTYPEi,
lt_accountglTYPESTANDARD TABLE OFbapiacgl09,
lt_arTYPESTANDARD TABLE OFbapiacar09,
lt_amountTYPESTANDARD TABLE OFbapiaccr09,
lt_returnTYPESTANDARD TABLE OFbapiret2.
DATA:
lt_extension2TYPESTANDARD TABLE OFbapiparex,
lwa_extension2TYPEbapiparex,
lv_next_monthTYPEsy-datum.
* Document Header
lwa_documentheader-obj_type='BKPFF'.
lwa_documentheader-obj_key='$'.
lwa_documentheader-obj_sys=sy-sysid.
lwa_documentheader-bus_act='RFBU'.
lwa_documentheader-username=sy-uname.
lwa_documentheader-doc_date=sy-datum.
lwa_documentheader-comp_code=rec_detail-bukrs." Company code
lwa_documentheader-pstng_date=sy-datum." posting date
lwa_documentheader-doc_type=rec_detail-auto_rev_doc_ty." Document type J1 or J0
lwa_documentheader-ref_doc_no=rec_detail-xblnr." REFERENCE
lwa_documentheader-header_txt=rec_detail-sgtxt." Header text (also Line item level)
READ TABLEit_lease_mapINTOlwa_lease_mapWITHKEYlease_group_id=rec_detail-lease_group.
CLEARlwa_accountgl.
lwa_accountgl-itemno_acc=1.
IFrec_detail-auto_rev_doc_ty='J1'.
lwa_accountgl-gl_account=lwa_lease_map-prepay_account.
ELSEIFrec_detail-auto_rev_doc_ty='J0'.
lwa_accountgl-gl_account=lwa_lease_map-acc_account.
ENDIF.
lwa_accountgl-de_cre_ind='S'.
lwa_accountgl-comp_code=rec_detail-bukrs.
APPENDlwa_accountglTOlt_accountgl.
CLEARlwa_accountgl.
lwa_accountgl-itemno_acc=2.
lwa_accountgl-gl_account=rec_detail-hkont.
lwa_accountgl-de_cre_ind='H'.
lwa_accountgl-comp_code=rec_detail-bukrs.
IFrec_detail-co_objISNOTINITIAL.
CASErec_detail-co_id.
WHEN'1'." Cost Center
CALL FUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=rec_detail-co_obj
IMPORTING
output=lwa_accountgl-costcenter.
CALL FUNCTION'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input=lwa_accountgl-costcenter
IMPORTING
output=lv_costc.
CONCATENATE'1'lv_costcINTOlwa_accountgl-profit_ctr.
WHEN'2'." WBS
lwa_accountgl-wbs_element=rec_detail-co_obj.
WHEN'3'." Order
lv_len=strlen(rec_detail-co_obj).
IFlv_lenLE12.
* Back fill zeros to Order
CALL FUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=rec_detail-co_obj
IMPORTING
output=lwa_accountgl-orderid
EXCEPTIONS
OTHERS=1.
ENDIF.
WHEN'4'." Order
lv_len=strlen(rec_detail-co_obj).
IFlv_lenLE12.
* Back fill zeros to Order
CALL FUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=rec_detail-co_obj
IMPORTING
output=lwa_accountgl-orderid
EXCEPTIONS
OTHERS=1.
ENDIF.
WHEN'5'." Profit Center
lv_len=strlen(rec_detail-co_obj).
IFlv_lenLE10.
* Back fill zeros to profit center
CALL FUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=rec_detail-co_obj
IMPORTING
output=lwa_accountgl-profit_ctr
EXCEPTIONS
OTHERS=1.
ENDIF.
WHENOTHERS.
* do nothing
ENDCASE.
ENDIF.
APPENDlwa_accountglTOlt_accountgl.
* Amount
CLEARlwa_amount.
lwa_amount-itemno_acc=1.
lwa_amount-currency='USD'.
lwa_amount-amt_doccur=v_auto_rev_amount.
APPENDlwa_amountTOlt_amount.
CLEARlwa_amount.
lwa_amount-itemno_acc=2.
lwa_amount-currency='USD'.
lwa_amount-amt_doccur=v_auto_rev_amount*-1.
APPENDlwa_amountTOlt_amount.
CLEARv_auto_rev_amount.
REFRESHlt_extension2[].
* Pass the Reversal Reason and Date to the BAPI
lwa_extension2-structure='ACCIT'." Accounting Interface: Item Information
CALL FUNCTION'/MRSS/RMOR_GET_NEXT_MONTH'
EXPORTING
iv_date=sy-datum
IMPORTING
ev_current_date=lv_next_month.
lwa_extension2-valuepart1=lv_next_month." Reversal Month Date
lwa_extension2-valuepart1+6(2)='15'." Custom Requirement - Always make the date 15th of the next Month
lwa_extension2-valuepart2='03'." Reversal Reason 03 = Accrual
APPENDlwa_extension2TOlt_extension2.
* Post Accounting Document
CALL FUNCTION'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader=lwa_documentheader
TABLES
accountgl=lt_accountgl
accountreceivable=lt_ar
currencyamount=lt_amount
return=lt_return
extension2=lt_extension2.
READ TABLElt_returnINTOlwa_returnWITHKEYtype='S'
id='RW'
number='605'.
IFsy-subrc=0.
CALL FUNCTION'BAPI_TRANSACTION_COMMIT'.
WRITE:lwa_return-type,
lwa_return-id,
lwa_return-number,
lwa_return-message,
lwa_return-message_v1,
lwa_return-message_v2,
lwa_return-message_v3,
lwa_return-message_v4.
ELSE.
LOOP ATlt_returnINTOlwa_returnWHEREtype='E'.
WRITE:lwa_return-type,
lwa_return-id,
lwa_return-number,
lwa_return-message,
lwa_return-message_v1,
lwa_return-message_v2,
lwa_return-message_v3,
lwa_return-message_v4.
ENDLOOP.
ENDIF.
ENDFORM.</code>

Your feedback and comments keep us motivating. Please keep them coming.

Comments Please!!!

We have a very active Telegram (App) SAP Technical Group with more than 4200+ SAP Technical Practitioners from 6 Continents of the SAP World. Please join it using below link.
Telegram SAP Technical Discuss Group. You need to install the Telegram App first on your mobile device. Once you have it on your mobile, you can join the group and also access it from the Web on your computer and laptop.

Some Useful Enhancements in SAP

Read 143 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.