We've all been there - address structures can be unpredictable, and you need to output them in a consistent format... The following ABAP code sample allows you to take the ADRC structure, put them into some kind of priority, split into a table, ignore the blank lines, and then concatenate into the final target field...
types: begin of tty_addr,
addrline type char255,
end of tty_addr.
data: t_addrlines type table of tty_addr,
w_addrlines type tty_addr.
data: w_adrc type adrc.
select single * from adrc into w_adrc where ADDRNUMBER = l_adrnr.
check sy-subrc = 0.
concatenate w_adrc-name_text
w_adrc-street
w_adrc-city1
w_adrc-STR_SUPPL1
w_adrc-STR_SUPPL2
w_adrc-STR_SUPPL3
w_arc-POST_CODE1
w_arc-POST_CODE2
w_arc-POST_CODE3
"Whatever fields MIGHT be populated from ADRC...
into l_DispatchAddress separated by '<&,>'.
split l_DispatchAddress at '<&,>' into table t_addrlines.
clear l_DispatchAddress.
loop at t_addrlines into w_addrlines.
if w_addrlines is not initial.
concatenate l_DispatchAddress w_addrlines-addrline into l_DispatchAddress separated by ', '.
endif.
endloop.
Thursday, 20 November 2014
Wednesday, 29 October 2014
Stupid Flowerboxes
This one is roughly the same size as the form it's describing, and tells us absolutely nothing useful. Needless to say I scrubbed this pollution as quickly as I could, and got on with my code... (after ShamingButNotNaming on my blog, of course...)
Tuesday, 28 October 2014
Iterate Up the HR Structure
Thanks to Kat for this one... I've got to get the UserID of the Users Manager... trouble is, sometimes the OrgStructure positions aren't filled - so I have to see who's sitting in the position. If there's no-one in it, then I loop back round and get the OrgStructure's superior position (since there's no Personnel person to check on!). I repeat this until I find someone, and stop at 20 goes... If this has happened, it's either
the guy at the top of the tree (we try 20 times to work out if the guy has a useful boss, but fail because it's Paul Solomon :)
OR
there's such a massive Org Structure that's not populated with stuff, that you should probably question what the point of the structure is!!
do 20 times. "Don't want to infinite loop...
refresh t_leadpos.
CALL FUNCTION 'RH_GET_LEADING_POSITION'
EXPORTING
plvar = w_plvar
otype = l_otype "can be US (user), P (Person), S (position) or O (Org unit)
sobid = l_sobid
date = sy-datum
auth = ' '
TABLES
leading_pos = t_leadpos
EXCEPTIONS
no_lead_pos_found = 1
OTHERS = 2.
check t_leadpos[] is not initial.
read table t_leadpos into w_leadpos index 1.
REFRESH t_holders.
CALL FUNCTION 'HRWPC_RFC_PP_HOLDERS'
EXPORTING
plvar = w_plvar "usually 01
otype = 'S'
objid = w_leadpos-objid "from above table read
TABLES
holder = t_holders
EXCEPTIONS
no_active_plvar = 1
infty_not_found = 2
nothing_found = 3
OTHERS = 4.
IF sy-subrc = 0 AND t_holders IS NOT INITIAL.
READ TABLE t_holders INTO w_holders INDEX 1.
read table t_pa0105 into w_pa0105 with key pernr = w_holders-pernr
subty = '0001'.
if sy-subrc = 0.
p_output-manager = w_pa0105-usrid.
exit.
endif.
else. "Didn't find holders for the position... Go and find the Super-Position.
l_otype = w_leadpos-otype. "Probably 'S' but hey, let's make it dynamic.
l_sobid = w_leadpos-objid.
endif. "Found holders for the position.
enddo. "Iterate up the structure.
the guy at the top of the tree (we try 20 times to work out if the guy has a useful boss, but fail because it's Paul Solomon :)
OR
there's such a massive Org Structure that's not populated with stuff, that you should probably question what the point of the structure is!!
do 20 times. "Don't want to infinite loop...
refresh t_leadpos.
CALL FUNCTION 'RH_GET_LEADING_POSITION'
EXPORTING
plvar = w_plvar
otype = l_otype "can be US (user), P (Person), S (position) or O (Org unit)
sobid = l_sobid
date = sy-datum
auth = ' '
TABLES
leading_pos = t_leadpos
EXCEPTIONS
no_lead_pos_found = 1
OTHERS = 2.
check t_leadpos[] is not initial.
read table t_leadpos into w_leadpos index 1.
REFRESH t_holders.
CALL FUNCTION 'HRWPC_RFC_PP_HOLDERS'
EXPORTING
plvar = w_plvar "usually 01
otype = 'S'
objid = w_leadpos-objid "from above table read
TABLES
holder = t_holders
EXCEPTIONS
no_active_plvar = 1
infty_not_found = 2
nothing_found = 3
OTHERS = 4.
IF sy-subrc = 0 AND t_holders IS NOT INITIAL.
READ TABLE t_holders INTO w_holders INDEX 1.
read table t_pa0105 into w_pa0105 with key pernr = w_holders-pernr
subty = '0001'.
if sy-subrc = 0.
p_output-manager = w_pa0105-usrid.
exit.
endif.
else. "Didn't find holders for the position... Go and find the Super-Position.
l_otype = w_leadpos-otype. "Probably 'S' but hey, let's make it dynamic.
l_sobid = w_leadpos-objid.
endif. "Found holders for the position.
enddo. "Iterate up the structure.
Tuesday, 14 October 2014
Creating an Invoice...
Clearly you'll need to make sure that the GLAccounts, Payment Terms etc exist, but feel free to use this as a template to create your Invoices...
*&---------------------------------------------------------------------*
*& Include Z_PD_CAPS1_DEMOS_INV_CREATE *
*&---------------------------------------------------------------------*
data:
obj_type like bapiache02-obj_type,
obj_key like bapiache02-obj_key,
obj_sys like bapiache02-obj_sys,
documentheader like bapiache09,
accountgl like bapiacgl09 occurs 0 with header line,
tax_accountgl like bapiacgl09 OCCURS 0 WITH HEADER LINE,
currencyamount like bapiaccr09 occurs 0 with header line,
tax_currencyamount like bapiaccr09 OCCURS 0 WITH HEADER LINE,
return like bapiret2 occurs 0 with header line,
extension1 like bapiextc occurs 0 with header line,
t_edidd like edidd occurs 0 with header line,
bapi_retn_info like bapiret2 occurs 0 with header line.
data: error_flag.
data: wa_akont TYPE akont.
data: t_accountreceivable TYPE TABLE OF BAPIACAR09 WITH HEADER LINE.
datA: T_MWDAT TYPE TABLE OF RTAX1U15 WITH HEADER LINE.
data: wa_wrbtr type wrbtr.
data: curr_li TYPE POSNR_ACC.
data: v_tabix like sy-tabix.
documentheader-username = sy-uname.
documentheader-header_txt = 'Test using BAPI'.
documentheader-comp_code = 'CF01'.
documentheader-doc_date = sy-datum.
documentheader-pstng_date = sy-datum.
documentheader-doc_type = 'SA'.
documentheader-REF_DOC_NO = 'INVOICE_01'.
clear accountgl.
clear wa_akont.
t_accountreceivable-ITEMNO_ACC = '1'.
t_accountreceivable-customer = 'C524'.
"t_accountreceivable-TAX_CODE = 'A1'.
t_accountreceivable-gl_account = '0000400000'.
t_accountreceivable-PMNTTRMS = 'Z030'.
t_accountreceivable-ALLOC_NMBR = 'LORDS0820'.
t_accountreceivable-ITEM_TEXT = 'Facility Name'.
append t_accountreceivable.
accountgl-itemno_acc = '2'.
accountgl-gl_account = '0000802010'.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-profit_ctr = '0000069314'.
"accountgl-costcenter = '0069314001'.
accountgl-ITEM_TEXT = 'Item Text'.
accountgl-ORDERID = 'LORDS0820'.
accountgl-TAX_CODE = 'A0'.
append accountgl.
clear accountgl.
accountgl-itemno_acc = '3'.
accountgl-gl_account = '0000802010'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = accountgl-gl_account
IMPORTING
OUTPUT = accountgl-gl_account.
accountgl-ITEM_TEXT = 'Item Text'.
accountgl-ORDERID = 'LORDS0820'.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-TAX_CODE = 'A1'.
append accountgl.
clear accountgl.
accountgl-itemno_acc = '4'.
accountgl-gl_account = '0000711350'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = accountgl-gl_account
IMPORTING
OUTPUT = accountgl-gl_account.
.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-TAX_CODE = 'V0'.
accountgl-costcenter = '69314001'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = accountgl-costcenter
IMPORTING
OUTPUT = accountgl-costcenter.
append accountgl.
currencyamount-itemno_acc = '1'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '470.00'.
append currencyamount.
currencyamount-itemno_acc = '2'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-230.00'.
append currencyamount.
currencyamount-itemno_acc = '3'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-235.00'.
append currencyamount.
currencyamount-itemno_acc = '4'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-5.00'.
append currencyamount.
"PD We're now calculating the tax. Need to calculate any tax at the
"appropriate rate.
"work out the first line that tax can go on.
loop at accountgl.
endloop.
curr_li = accountgl-ITEMNO_ACC.
curr_li = curr_li + 1.
loop at accountgl.
READ TABLE currencyamount with key ITEMNO_ACC =
accountgl-ITEMNO_ACC.
check sy-subrc = 0.
v_tabix = sy-tabix.
wa_wrbtr = currencyamount-amt_doccur.
CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'
EXPORTING
I_BUKRS = accountgl-comp_code
I_MWSKZ = accountgl-TAX_CODE
I_WAERS = currencyamount-currency
I_WRBTR = wa_wrbtr
TABLES
T_MWDAT = t_mwdat
EXCEPTIONS
BUKRS_NOT_FOUND = 1
COUNTRY_NOT_FOUND = 2
MWSKZ_NOT_DEFINED = 3
MWSKZ_NOT_VALID = 4
ACCOUNT_NOT_FOUND = 5
DIFFERENT_DISCOUNT_BASE = 6
DIFFERENT_TAX_BASE = 7
TXJCD_NOT_VALID = 8
NOT_FOUND = 9
KTOSL_NOT_FOUND = 10
KALSM_NOT_FOUND = 11
PARAMETER_ERROR = 12
KNUMH_NOT_FOUND = 13
KSCHL_NOT_FOUND = 14
UNKNOWN_ERROR = 15
OTHERS = 16.
IF SY-SUBRC <> 0.
CONTINUE.
ENDIF.
READ TABLE T_MWDAT INDEX 1.
check sy-subrc = 0.
"There was tax there!!
if t_mwdat-wmwst is not INITIAL.
clear tax_accountgl.
tax_accountgl-gl_account = '402560'.
tax_accountgl-ITEMNO_ACC = curr_li.
tax_accountgl-tax_code = accountgl-tax_code.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = tax_accountgl-gl_account
IMPORTING
OUTPUT = tax_accountgl-gl_account.
append tax_accountgl.
clear tax_currencyamount.
tax_currencyamount = currencyamount.
tax_currencyamount-amt_doccur = T_MWDAT-wmwst.
tax_currencyamount-ITEMNO_ACC = curr_li.
append tax_currencyamount.
currencyamount-amt_doccur = T_MWDAT-KAWRT.
modify currencyamount index v_tabix.
curr_li = curr_li + 1.
endif.
endloop.
append LINES OF tax_accountgl to accountgl.
append LINES OF tax_currencyamount to currencyamount.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
DOCUMENTHEADER = documentheader
TABLES
ACCOUNTGL = accountgl
ACCOUNTRECEIVABLE = t_accountreceivable
CURRENCYAMOUNT = currencyamount
RETURN = return .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
loop at return.
write return-message.
endloop.
*&---------------------------------------------------------------------*
*& Include Z_PD_CAPS1_DEMOS_INV_CREATE *
*&---------------------------------------------------------------------*
data:
obj_type like bapiache02-obj_type,
obj_key like bapiache02-obj_key,
obj_sys like bapiache02-obj_sys,
documentheader like bapiache09,
accountgl like bapiacgl09 occurs 0 with header line,
tax_accountgl like bapiacgl09 OCCURS 0 WITH HEADER LINE,
currencyamount like bapiaccr09 occurs 0 with header line,
tax_currencyamount like bapiaccr09 OCCURS 0 WITH HEADER LINE,
return like bapiret2 occurs 0 with header line,
extension1 like bapiextc occurs 0 with header line,
t_edidd like edidd occurs 0 with header line,
bapi_retn_info like bapiret2 occurs 0 with header line.
data: error_flag.
data: wa_akont TYPE akont.
data: t_accountreceivable TYPE TABLE OF BAPIACAR09 WITH HEADER LINE.
datA: T_MWDAT TYPE TABLE OF RTAX1U15 WITH HEADER LINE.
data: wa_wrbtr type wrbtr.
data: curr_li TYPE POSNR_ACC.
data: v_tabix like sy-tabix.
documentheader-username = sy-uname.
documentheader-header_txt = 'Test using BAPI'.
documentheader-comp_code = 'CF01'.
documentheader-doc_date = sy-datum.
documentheader-pstng_date = sy-datum.
documentheader-doc_type = 'SA'.
documentheader-REF_DOC_NO = 'INVOICE_01'.
clear accountgl.
clear wa_akont.
t_accountreceivable-ITEMNO_ACC = '1'.
t_accountreceivable-customer = 'C524'.
"t_accountreceivable-TAX_CODE = 'A1'.
t_accountreceivable-gl_account = '0000400000'.
t_accountreceivable-PMNTTRMS = 'Z030'.
t_accountreceivable-ALLOC_NMBR = 'LORDS0820'.
t_accountreceivable-ITEM_TEXT = 'Facility Name'.
append t_accountreceivable.
accountgl-itemno_acc = '2'.
accountgl-gl_account = '0000802010'.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-profit_ctr = '0000069314'.
"accountgl-costcenter = '0069314001'.
accountgl-ITEM_TEXT = 'Item Text'.
accountgl-ORDERID = 'LORDS0820'.
accountgl-TAX_CODE = 'A0'.
append accountgl.
clear accountgl.
accountgl-itemno_acc = '3'.
accountgl-gl_account = '0000802010'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = accountgl-gl_account
IMPORTING
OUTPUT = accountgl-gl_account.
accountgl-ITEM_TEXT = 'Item Text'.
accountgl-ORDERID = 'LORDS0820'.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-TAX_CODE = 'A1'.
append accountgl.
clear accountgl.
accountgl-itemno_acc = '4'.
accountgl-gl_account = '0000711350'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = accountgl-gl_account
IMPORTING
OUTPUT = accountgl-gl_account.
.
accountgl-comp_code = 'CF01'.
accountgl-pstng_date = sy-datum.
accountgl-doc_type = 'SA'.
accountgl-TAX_CODE = 'V0'.
accountgl-costcenter = '69314001'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = accountgl-costcenter
IMPORTING
OUTPUT = accountgl-costcenter.
append accountgl.
currencyamount-itemno_acc = '1'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '470.00'.
append currencyamount.
currencyamount-itemno_acc = '2'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-230.00'.
append currencyamount.
currencyamount-itemno_acc = '3'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-235.00'.
append currencyamount.
currencyamount-itemno_acc = '4'.
currencyamount-currency = 'GBP'.
currencyamount-amt_doccur = '-5.00'.
append currencyamount.
"PD We're now calculating the tax. Need to calculate any tax at the
"appropriate rate.
"work out the first line that tax can go on.
loop at accountgl.
endloop.
curr_li = accountgl-ITEMNO_ACC.
curr_li = curr_li + 1.
loop at accountgl.
READ TABLE currencyamount with key ITEMNO_ACC =
accountgl-ITEMNO_ACC.
check sy-subrc = 0.
v_tabix = sy-tabix.
wa_wrbtr = currencyamount-amt_doccur.
CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'
EXPORTING
I_BUKRS = accountgl-comp_code
I_MWSKZ = accountgl-TAX_CODE
I_WAERS = currencyamount-currency
I_WRBTR = wa_wrbtr
TABLES
T_MWDAT = t_mwdat
EXCEPTIONS
BUKRS_NOT_FOUND = 1
COUNTRY_NOT_FOUND = 2
MWSKZ_NOT_DEFINED = 3
MWSKZ_NOT_VALID = 4
ACCOUNT_NOT_FOUND = 5
DIFFERENT_DISCOUNT_BASE = 6
DIFFERENT_TAX_BASE = 7
TXJCD_NOT_VALID = 8
NOT_FOUND = 9
KTOSL_NOT_FOUND = 10
KALSM_NOT_FOUND = 11
PARAMETER_ERROR = 12
KNUMH_NOT_FOUND = 13
KSCHL_NOT_FOUND = 14
UNKNOWN_ERROR = 15
OTHERS = 16.
IF SY-SUBRC <> 0.
CONTINUE.
ENDIF.
READ TABLE T_MWDAT INDEX 1.
check sy-subrc = 0.
"There was tax there!!
if t_mwdat-wmwst is not INITIAL.
clear tax_accountgl.
tax_accountgl-gl_account = '402560'.
tax_accountgl-ITEMNO_ACC = curr_li.
tax_accountgl-tax_code = accountgl-tax_code.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = tax_accountgl-gl_account
IMPORTING
OUTPUT = tax_accountgl-gl_account.
append tax_accountgl.
clear tax_currencyamount.
tax_currencyamount = currencyamount.
tax_currencyamount-amt_doccur = T_MWDAT-wmwst.
tax_currencyamount-ITEMNO_ACC = curr_li.
append tax_currencyamount.
currencyamount-amt_doccur = T_MWDAT-KAWRT.
modify currencyamount index v_tabix.
curr_li = curr_li + 1.
endif.
endloop.
append LINES OF tax_accountgl to accountgl.
append LINES OF tax_currencyamount to currencyamount.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
DOCUMENTHEADER = documentheader
TABLES
ACCOUNTGL = accountgl
ACCOUNTRECEIVABLE = t_accountreceivable
CURRENCYAMOUNT = currencyamount
RETURN = return .
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
loop at return.
write return-message.
endloop.
Tuesday, 26 August 2014
Getting the Buffer Quality metric from an Oracle System
The requirement was to try and get the “Buffer Quality”
value out of BASIS transaction ST04. The technique for this will vary from
system to system, as it depends on the underlying database.
The field behind this is DBUFF_QUALITY and ST04 is underpinned by program RSDB0004.
A quick look at this tells us that RSDB0004 is just a CASE
Statement that filters the Database System, and calls corresponding
transactions. The one I’m interested in is an Oracle Database:
Going into this, I did a search on “DBUFF_QUALITY” and
found that it was held in function module
C_ORA_MM_GET_DATA_V10.
I ran this in SE37, without any parameters populated, and
in the response, there was a structure called S_ST04N_DATA with parameter DBUFF_QUALITY
which is what I was after.
Nice and Simple!
Friday, 22 August 2014
CSV Files with Commas in the fields....
Okay, so the initial requirement was to load a CSV file
in to SAP and process the content. This all seemed fine, and I processed it as
follows:
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = v_filename_string
filetype = 'DAT'
TABLES
data_tab = itab.
loop
at itab.
split itab-text at ',' into w_input-matnr
w_input-desc w_input-date w_input-demand.
append w_input to t_input.
endloop.
But it became apparent that the users, saving data in
Excel CSV, wanted to include commas in the field values (specifically the
w_input-desc). Obviously the split just takes these as delimiters and messes up
the demand and date values.
To get round this, instead of “GUI Upload”, we’re going
to use
CALL FUNCTION
'KCD_CSV_FILE_TO_INTERN_CONVERT'
EXPORTING
i_filename = l_filename
i_separator =
','
TABLES
e_intern = lt_csv
EXCEPTIONS
upload_csv = 1
upload_filetype = 2
OTHERS = 3.
This results in a slightly awkward table :
Row Col Value
1 0001 0001 40253
2 0001 0002 Crispy Aromatic, Half Duck 20x32
3 0001 0003 14:32
4 0001 0004 40
5 0002 0001 40253
6 0002 0002 Crispy Aromatic, Half Duck 20x32
7 0002 0003 14:33
8 0002 0004 50
But I can loop round this, and create my t_input table
from there…
"Tee up the t_input table.
lt_csv_bluff[] = lt_csv[].
delete ADJACENT DUPLICATES FROM lt_csv_bluff COMPARING row.
loop at lt_csv_bluff into lv_csv_bluff.
append w_input to t_input.
endloop.
loop at lt_csv into lv_csv.
assign COMPONENT lv_csv-col of STRUCTURE w_input to <fs>.
<fs> = lv_csv-value.
MODIFY t_input from w_input INDEX lv_csv-row.
endloop.
"Tee up the t_input table.
lt_csv_bluff[] = lt_csv[].
delete ADJACENT DUPLICATES FROM lt_csv_bluff COMPARING row.
loop at lt_csv_bluff into lv_csv_bluff.
append w_input to t_input.
endloop.
loop at lt_csv into lv_csv.
assign COMPONENT lv_csv-col of STRUCTURE w_input to <fs>.
<fs> = lv_csv-value.
MODIFY t_input from w_input INDEX lv_csv-row.
endloop.
Thursday, 21 August 2014
Infinite Loop for background debugging
I use this all the time for debugging SAP ABAP "background" operations. Sometimes you can't debug straight to a bit of code, as it's operated on by the server, not the a frontend session.
So, what I do, is use this simple bit of code:
data: a, b.
a = 'X'.
do.
if a = b.
exit.
endif.
enddo.
Which, as you can probably tell, sets the program into an infinite loop, which can only be interrupted by debugging.
This is done in transaction SM51 - find the session with ( your / system ) name on it, and hit Program-Debug from the menu
At this point, you can then clear out the "a" variable, which then means that a = b, and so the loop gets exited. You can then debug through the background process.
There are other ways of doing it, but I've found this one to be the simplest and most reliable.
Don't forget to take it out again once you're finished, otherwise you'll paralyse the backend!
Subscribe to:
Posts (Atom)



