Also you can check standard documentation in here .
Friday, January 27, 2012
ABAP Dynamic Function Call
Code sample below shows how to implement a dynamic function call. Actually we need that kind of dynamic function call rarely instead of static call. However it helped me a lot in a complicated scenario. Less code , less effort and a clean code...
Also you can check standard documentation in here .
Also you can check standard documentation in here .
Tuesday, November 15, 2011
Open New Window Using ABAP
While displaying a report , generally user wants to drill down by clicking one of the lines or cells to get more detailed information about related topic. Most common behavior to accomplish this task is displaying detailed information at the same window. However in some cases users need to display the detailed report in another window. They need to see both reports inside different windows. For this purpose check sample below.
DATA : ls_params TYPE tpara,
lt_params TYPE TABLE OF tpara.
ls_params-paramid = 'BUK'.
ls_params-partext = '0001'.
APPEND ls_params TO lt_params.
ls_params-paramid = 'BLN'.
ls_params-partext = '00000101'.
APPEND ls_params TO lt_params.
ls_params-paramid = 'GJR'.
ls_params-partext = '2011'.
APPEND ls_params TO lt_params.
CALL FUNCTION 'CC_CALL_TRANSACTION_NEW_TASK'
STARTING NEW TASK 'FB03_TASK'
DESTINATION 'NONE'
EXPORTING
transaction = 'FB03'
skip_first_screen = 'X'
TABLES
paramtab = lt_params
EXCEPTIONS
communication_failure = 97
system_failure = 98
OTHERS = 99.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
lt_params TYPE TABLE OF tpara.
ls_params-paramid = 'BUK'.
ls_params-partext = '0001'.
APPEND ls_params TO lt_params.
ls_params-paramid = 'BLN'.
ls_params-partext = '00000101'.
APPEND ls_params TO lt_params.
ls_params-paramid = 'GJR'.
ls_params-partext = '2011'.
APPEND ls_params TO lt_params.
CALL FUNCTION 'CC_CALL_TRANSACTION_NEW_TASK'
STARTING NEW TASK 'FB03_TASK'
DESTINATION 'NONE'
EXPORTING
transaction = 'FB03'
skip_first_screen = 'X'
TABLES
paramtab = lt_params
EXCEPTIONS
communication_failure = 97
system_failure = 98
OTHERS = 99.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Read Handling Unit Items
If you need to get the contents of a handling unit , this function will help you.
*** Data
DATA : lf_highest_level_hu TYPE exidv ,
lt_hu_header TYPE hum_hu_header_t WITH HEADER LINE,
lt_hu_items TYPE hum_hu_item_t WITH HEADER LINE .
lt_hu_header TYPE hum_hu_header_t WITH HEADER LINE,
lt_hu_items TYPE hum_hu_item_t WITH HEADER LINE .
*** Get Items
CALL FUNCTION 'HU_GET_ONE_HU_DB'
EXPORTING
if_hu_number = i_handling_unit
if_all_levels = 'X' " Get All Levels
if_with_text = 'X'
IMPORTING
ef_highest_level_hu = lf_highest_level_hu
et_hu_header = lt_hu_header[]
et_hu_items = lt_hu_items[]
EXCEPTIONS
hu_not_found = 1
hu_locked = 2
fatal_error = 3
OTHERS = 4.
CALL FUNCTION 'HU_GET_ONE_HU_DB'
EXPORTING
if_hu_number = i_handling_unit
if_all_levels = 'X' " Get All Levels
if_with_text = 'X'
IMPORTING
ef_highest_level_hu = lf_highest_level_hu
et_hu_header = lt_hu_header[]
et_hu_items = lt_hu_items[]
EXCEPTIONS
hu_not_found = 1
hu_locked = 2
fatal_error = 3
OTHERS = 4.
*** Delete root HUs if you need only materials
DELETE lt_hu_items WHERE unvel IS NOT INITIAL.
DELETE lt_hu_items WHERE unvel IS NOT INITIAL.
Thursday, October 27, 2011
Goods Movement For Production Order
Sample Code to fulfill Goods Movement For Production Order.
*** PARAMETERS PASSED
i_aufnr , i_aufnr_itno , i_plant , i_lgort , i_quantity
*** PARAMETERS PASSED
*** DATA Decleration
DATA : gs_header TYPE bapi2017_gm_head_01 ,
gs_code TYPE bapi2017_gm_code ,
gt_headret TYPE TABLE OF bapi2017_gm_head_ret WITH HEADER LINE ,
gt_item TYPE TABLE OF bapi2017_gm_item_create WITH HEADER LINE,
gt_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
*** Find Order's UNIT
SELECT SINGLE sbmeh FROM afko INTO lv_sbmeh
WHERE aufnr = i_aufnr.
*GM_CODE TCODE Description
*01 MB01 Goods receipt for purchase order
*02 MB31 Goods receipt for production order
*03 MB1A Goods issue
*04 MB1B Transfer posting
*05 MB1C Other goods receipt
*06 MB11 Reversal of goods movements
*07 MB04 Subsequent adjustment with regard to a subcontract order
*** Set Code
gs_code-gm_code = '02' .
*** Header
gs_header-pstng_date = sy-datum .
gs_header-doc_date = sy-datum .
gs_header-pr_uname = sy-uname .
gs_header-header_txt = 'Text 123'.
gs_header-ref_doc_no = i_aufnr.
*** Item
CLEAR gt_item .
gt_item-move_type = '101' .
gt_item-spec_stock = 'E'.
gt_item-mvt_ind = 'F'.
gt_item-stck_type = 'X'.
*** Order Id
gt_item-orderid = i_aufnr.
gt_item-order_itno = i_aufnr_itno.
gt_item-plant = i_plant .
gt_item-stge_loc = i_lgort.
gt_item-entry_qnt = i_quantity .
gt_item-entry_uom = lv_sbmeh.
gt_item-entry_uom_iso = lv_sbmeh.
gt_item-item_text = i_aufnr .
APPEND gt_item .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = gs_header
goodsmvt_code = gs_code
IMPORTING
goodsmvt_headret = gt_headret
TABLES
goodsmvt_item = gt_item[]
return = gt_return[].
BREAK-POINT.
LOOP AT gt_return WHERE type = 'S'
AND id = 'L9'
AND number = '514'.
ENDLOOP.
IF sy-subrc EQ 0.
*** Delivery Created - OK
ELSE.
*** Problem Occured - Check gt_return[]
ENDIF.
Thursday, October 6, 2011
Full-screen ALV
ABAP developers face ALV full-screen problem most of the times as a part of designing issue. Displaying ALV in full-screen mode is just easy as below. You don't need to create a custom container.
if go_alv is initial.
create object go_alv
exporting
i_parent = cl_gui_container=>screen0
* i_appl_events = 'X'
exceptions
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
others = 5.
if sy-subrc ne 0.
message id sy-msgid type sy-msgty number
sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3
sy-msgv4.
endif.
endif.
Currency Conversion
Like unit conversion , currency conversion is also one of the most needed operation. And for this kind of common operations we should have code snippets. Here is a sample usage for currency conversion.
*** Currency Conversion
if lv_waers_from ne lv_waers_to.
perform convert_currency using lv_waers_from
lv_waers_to
changing lv_wrbtr.
endif.
*-------------------------------------------
* Form convert_currency
*-------------------------------------------
FORM convert_currency USING p_from_waers
p_to_waers
CHANGING p_wrbtr.
DATA : lv_ukurs TYPE tcurr-ukurs .
CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
date = gv_keydate
foreign_amount = p_wrbtr
foreign_currency = p_from_waers
local_currency = p_to_waers
IMPORTING
exchange_rate = lv_ukurs
EXCEPTIONS
no_rate_found = 1
overflow = 2
no_factors_found = 3
no_spread_found = 4
derived_2_times = 5
OTHERS = 6.
CHECK sy-subrc EQ 0.
p_wrbtr = p_wrbtr * lv_ukurs.
ENDFORM. " convert_currency
Monday, September 12, 2011
ABAP Field Symbol Usage
Here is a short sample which illustrates the usage of field symbols with work areas (headers) . Even though we don't know the number or the name of elements in a work area , we can dynamically get them like below. Field symbols are indispensable for complicated applications or reports. Also they could provide a solution for performance issues in some cases. Beside the usage below there are much more ways to use it. For more information just check out the documentation in sdn.
FIELD-SYMBOLS : TYPE ANY ,
DATA : t_table TYPE TABLE OF pa0001 WITH HEADER LINE.
SELECT * FROM pa0001 INTO TABLE t_table UP TO 100 ROWS.
LOOP AT t_table ASSIGNING .
WRITE : /.
" Loop at fields
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE TO .
IF sy-subrc EQ 0.
WRITE : .
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDLOOP.
Basic ABAP HTTP Request Sample
For various needs using http requests could be a life saver. Here is a simple code snippet.
data : lv_value type string.
data : http_client type ref to if_http_client .data : lv_url type string.data : return type string.
data : lv_err_string type string ,lv_ret_code type sy-subrc .
* Build Url
lv_valueinto lv_url.* Create Clientcall method cl_http_client=>create_by_urlexportingurl = lv_urlimportingclient = http_client.* Sendcall method http_client->sendexceptionshttp_communication_failure = 1http_invalid_state = 2.* Receivecall method http_client->receiveexceptionshttp_communication_failure = 1http_invalid_state = 2http_processing_failed = 3.if sy-subrc ne 0.http_client->response->get_status(importingcode = lv_ret_codereason = lv_err_string).message lv_err_string type 'I'.endif.* Now we have the response , parse , display* do what you likereturn = http_client->response->get_cdata( ).
Monday, August 22, 2011
UTILITY CLASS
Utility class is a kind of conceptual class that includes commonly used methods for the related operations. It groups methods together that have the similar purpose about a specific topic and these methods are static which makes them easy to use.
Examples of ABAP Utility Objects ;
CL_HTTP_UTILITY
CL_BSP_UTILITY
CL_ABAP_CHAR_UTILITIES
CL_ABAP_MEMORY_UTILITIES
CL_ABAP_STRING_UTILITIES
....
For Java ;
java.util.Collections
Wikipedia ;
http://en.wikipedia.org/wiki/Utility_class
Examples of ABAP Utility Objects ;
CL_HTTP_UTILITY
CL_BSP_UTILITY
CL_ABAP_CHAR_UTILITIES
CL_ABAP_MEMORY_UTILITIES
CL_ABAP_STRING_UTILITIES
....
For Java ;
java.util.Collections
Wikipedia ;
http://en.wikipedia.org/wiki/Utility_class
Friday, July 15, 2011
ABAP and Java Article
Here is a nice article about ABAP vs JAVA named "How to Get the Most Out of ABAP and Java in the Context of SAP Technology". It provides answers for some of the popular questions such as "What Is the Future of ABAP and Java at SAP?".
SDN link;
http://www.sdn.sap.com/irj/sdn/index?rid=/library/uuid/10f8e152-0da0-2e10-f9af-8eef7b9bbe15
SAP insider link;
ARTICLE : How to get Most Out of ABAP and Java in the Context of SAP Technology
Abstract :
Is ABAP better than Java? Or is Java better than ABAP? This first article in a two-part series shows you that it isn't a question of which development language is better; the choice depends on the situation, and sometimes the answer is both. This article offers three questions to consider at the start of your decision-making process, and then looks at a scenario in which ABAP is the best option.
SDN link;
http://www.sdn.sap.com/irj/sdn/index?rid=/library/uuid/10f8e152-0da0-2e10-f9af-8eef7b9bbe15
SAP insider link;
ARTICLE : How to get Most Out of ABAP and Java in the Context of SAP Technology
Abstract :
Is ABAP better than Java? Or is Java better than ABAP? This first article in a two-part series shows you that it isn't a question of which development language is better; the choice depends on the situation, and sometimes the answer is both. This article offers three questions to consider at the start of your decision-making process, and then looks at a scenario in which ABAP is the best option.
Wednesday, June 8, 2011
Displaying Custom HTML Content Using cl_gui_html_viewer
Here is my sample program that contains necessary operations to display custom html content using cl_gui_html_viewer. It is simple and would be very useful in some cases.
*&---------------------------------------------------------------------*
*& Report ZZTEST
*&---------------------------------------------------------------------*
REPORT zztest .
*** cl_gui_html_viewer instance
DATA : gr_browser TYPE REF TO cl_gui_html_viewer.
*** Data definitions HTML Content
DATA : gv_url(1024) TYPE c.
DATA : gt_html TYPE TABLE OF char255 WITH HEADER LINE.
DATA : gs_html LIKE LINE OF gt_html.
DATA : gv_color TYPE string VALUE '#e5f1f4'.
*** Internal Table That has main Data
DATA : BEGIN OF gt_main OCCURS 10,
persno TYPE i,
name TYPE string,
age TYPE i,
END OF gt_main.
*** Macro helps to create HTML content table
DEFINE add_to_html.
clear : gs_html.
gs_html = &1.
append gs_html to gt_html.
END-OF-DEFINITION.
*** START-OF-SELECTION
START-OF-SELECTION.
PERFORM add_data_for_demo.
CALL SCREEN 100.
*&---------------------------------------------------------------------*
*& Form add_data_for_demo
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM add_data_for_demo.
*** Demo table to show
DATA : lv_temp TYPE string.
DO 30 TIMES.
lv_temp = sy-index.
CONCATENATE 'Employee No ' lv_temp INTO lv_temp SEPARATED BY ' '.
CLEAR : gt_main.
gt_main-persno = sy-index + 60 .
gt_main-name = lv_temp.
gt_main-age = ( sy-index + 60 ) / 3.
APPEND gt_main.
ENDDO.
ENDFORM. "add_data_for_demo
*&---------------------------------------------------------------------*
*& Module pbo_100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE pbo_100 OUTPUT.
SET PF-STATUS 'PFST100'.
SET TITLEBAR 'TITLE100'.
*** Show Html Table
PERFORM html_viewer.
ENDMODULE. " pbo_100 OUTPUT
*&---------------------------------------------------------------------*
*& Module pai_100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE pai_100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE. " pai_100 INPUT
*&---------------------------------------------------------------------*
*& Form html_viewer
*&---------------------------------------------------------------------*
FORM html_viewer .
*** Create Instance Using Default Screen As Parent
CREATE OBJECT gr_browser
EXPORTING
parent = cl_gui_container=>screen0.
*** Create Your HTML Content
gv_url = 'test.htm'.
*** Html Head
add_to_html :
'<html><head><meta http-equiv=Content-Type content="text/html; ',
'charset=UTF-8"></head><body style="background:#b8fa84">' .
*** Html Body
*** Html Body
add_to_html : '<b> THANKS to cl_gui_html_viewer </b><br><br><br>'.
*** Html Table Properties : You can also use css
add_to_html :
'<table border=1 cellpadding=1 cellspacing=0',
'width=''500px''><thead>',
'<tr style="background:lightgray;text-align:center;">',
'<td colspan=3>',
'<b > Report Header </b>',
'</td></tr></thead>'.
*** Add header to html table
add_to_html :
'<tr align=center><td style="width:20%;background-color:azure">ID',
'</td><td style="width:40%;background-color:azure">Name',
'</td><td style="width:40%;background-color:azure;">Age</td></tr>'.
*** Show your internal table
LOOP AT gt_main.
PERFORM add_lines USING
gt_main-persno gt_main-name gt_main-age.
ENDLOOP.
*** Close HTML
add_to_html : '</table></body></html>'.
*** Load HTML Data
CALL METHOD gr_browser->load_data
EXPORTING
url = gv_url
IMPORTING
assigned_url = gv_url
CHANGING
data_table = gt_html[].
*** Show Url
CALL METHOD gr_browser->show_url
EXPORTING
url = gv_url.
CALL METHOD cl_gui_html_viewer=>set_focus
EXPORTING
control = gr_browser
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
OTHERS = 3.
ENDFORM. " html_viewer
*&---------------------------------------------------------------------*
*& Form add_lines
*&---------------------------------------------------------------------*
FORM add_lines USING p_1 p_2 p_3.
DATA : lv_str1 TYPE string.
DATA : lv_str2 TYPE string.
DATA : lv_str3 TYPE string.
*** Zebra Style
IF gv_color = '#e5f1f4'.
gv_color = '#f8fbfc'.
ELSE.
gv_color = '#e5f1f4'.
ENDIF.
lv_str1 = p_1.
lv_str2 = p_2.
lv_str3 = p_3.
CONCATENATE
'<tr align=center><td style="width:20%;background-color:'
gv_color '">' lv_str1 '</td>' INTO gs_html.
APPEND gs_html TO gt_html.
CONCATENATE
'<td style="width:40%;background-color:' gv_color '">' lv_str2
'</td><td style="width:40%;background-color:'
gv_color '">' lv_str3 '</td></tr>'
INTO gs_html.
APPEND gs_html TO gt_html.
ENDFORM. "add_linesTuesday, May 17, 2011
USING ALV EVENT HANDLER FOR ALL INSTANCES
We have more than one alv grid instance ( cl_gui_alv_grid ). Instead of defining separate event handlers for each instance , we could define one for an event using FOR ALL INSTANCES keyword.
When any of them fires related event, we could catch and take actions. But how do we know the instance ? Sender parameter is the key.
*** INSTANCES
DATA : go_alv01 TYPE REF TO cl_gui_alv_grid ,
go_alv02 TYPE REF TO cl_gui_alv_grid ,
go_alv03 TYPE REF TO cl_gui_alv_grid .
*** LINK EVENTS TO INSTANCE
SET HANDLER go_event_handler->handle_double_click FOR ALL INSTANCES.
*** DEFINITION OF EVENT
*---------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
class lcl_event_handler definition .
public section .
class-methods:
handle_double_click
for event double_click of cl_gui_alv_grid
importing e_row e_column sender.
private section.
endclass. "lcl_event_handler DEFINITION
*** IMPLEMENTATION OF EVENTS
*---------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
class lcl_event_handler implementation .
method handle_hotspot_click.
CASE sender.
WHEN go_alv01.
" go_alv01 has fired the event
WHEN go_alv02.
" go_alv02 has fired the event
WHEN go_alv03.
" go_alv03 has fired the event
ENDCASE.
endmethod. "handle_hospot_click
When any of them fires related event, we could catch and take actions. But how do we know the instance ? Sender parameter is the key.
*** INSTANCES
DATA : go_alv01 TYPE REF TO cl_gui_alv_grid ,
go_alv02 TYPE REF TO cl_gui_alv_grid ,
go_alv03 TYPE REF TO cl_gui_alv_grid .
*** LINK EVENTS TO INSTANCE
SET HANDLER go_event_handler->handle_double_click FOR ALL INSTANCES.
*** DEFINITION OF EVENT
*---------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
class lcl_event_handler definition .
public section .
class-methods:
handle_double_click
for event double_click of cl_gui_alv_grid
importing e_row e_column sender.
private section.
endclass. "lcl_event_handler DEFINITION
*** IMPLEMENTATION OF EVENTS
*---------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
class lcl_event_handler implementation .
method handle_hotspot_click.
CASE sender.
WHEN go_alv01.
" go_alv01 has fired the event
WHEN go_alv02.
" go_alv02 has fired the event
WHEN go_alv03.
" go_alv03 has fired the event
ENDCASE.
endmethod. "handle_hospot_click
Subscribe to:
Posts (Atom)