ABAP--一个实现Search Help Exits的完整样例(转)
Search help exit is used to modify the F4 values at run time before its being displayed as a list to the user for selection. This document explains step by step procedure for implementing a search help exit with an example.
2) PRESEL1 - in this step the maximum records, internal length, offset attributes of the fields are set
3) PRESEL - in this step a pop-up is displayed for the user to enter the selection criteria.
4) SELECT - in this step based on the selection criteria entered by the user data will be selected to RECORD_TAB internal table based on the selection method specified for the search help
5) DISP - this is the final step before the result list is displayed to the user.
The result list will be displayed as below,
Now say our requirement is to have customer number and customer name in the customer column as '121- BHARAT FORGES'.
We can achieve this by implementing a search help exit and modifying the internal length & output length attributes of the customer field at run time and concatenating customer name to customer number at run time.
For the purpose of this document I have copied search help VMVAC to ZVMVAC and implemented an exit for this search help.
3) Define the interface attributes for the function module as shown below
SHLP- Structure containing search help type, interface and field descriptions of the search help.
CALLCONTROL - Structure containing the current step of the search help process.
SHLP_TAB -contains search help field description, field properties and selection criteria.
RECORD_TAB - contains the search help result list.
In this example we will be modifying RECORD_TAB in control step DISP i.e. display as below
1) We need to increase the output length of field KUNNR, in addition to increasing the output length of KUNNR we also need to adjust the offset position of other fields in order to avoid overlapping of Customer data with other fields.
2) Customer numbers will be available in SHLP, build a range of all the customer numbers which will formulate WHERE condition for selecting customer name.
3) Select customer name from KNA1 for all the customers.
4) Modify RECORD_TAB to append customer name to customer number.
Once these steps are done output will appear as below,
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" SHLP_TAB TYPE SHLP_DESCT
*" RECORD_TAB STRUCTURE SEAHLPRES
*" CHANGING
*" VALUE(SHLP) TYPE SHLP_DESCR
*" VALUE(CALLCONTROL) TYPE DDSHF4CTRL
*"----------------------------------------------------------------------
DATA: ls_fielddescr TYPE dfies, "loc str for shlp-fielddescr
ls_selopt TYPE ddshselopt. "loc str for shlp-selopt
*Local structure for itab record_tab
DATA: BEGIN OF ls_record.
INCLUDE STRUCTURE seahlpres.
DATA: END OF ls_record.
DATA: ls_name1 TYPE name1_gp,
ls_start TYPE string,
ls_end TYPE string,
v_kunnr TYPE kunnr.
*Internal table to store Customer Name
DATA: BEGIN OF gt_kna1 OCCURS 0,
kunnr LIKE kna1-kunnr,
name1 LIKE kna1-name1,
END OF gt_kna1.
RANGES: lr_kunnr FOR kna1-kunnr. "Ranges for customer number
CHECK CALLCONTROL-STEP = 'DISP'.
LOOP AT shlp-fielddescr INTO ls_fielddescr.
CASE ls_fielddescr-fieldname.
WHEN 'KUNNR'.
ls_fielddescr-intlen = ls_fielddescr-outputlen = 45.
MODIFY shlp-fielddescr FROM ls_fielddescr INDEX sy-tabix.
WHEN 'ERNAM'.
ls_fielddescr-offset = 63.
MODIFY shlp-fielddescr FROM ls_fielddescr INDEX sy-tabix.
WHEN 'ERDAT'.
ls_fielddescr-offset = 75.
MODIFY shlp-fielddescr FROM ls_fielddescr INDEX sy-tabix.
WHEN 'VBELN'.
ls_fielddescr-offset = 83.
MODIFY shlp-fielddescr FROM ls_fielddescr INDEX sy-tabix.
ENDCASE.
ENDLOOP.
*Build range for customer number
LOOP AT shlp-selopt INTO ls_selopt WHERE shlpfield = 'KUNNR'.
lr_kunnr-sign = ls_selopt-sign.
lr_kunnr-option = ls_selopt-option.
lr_kunnr-low = ls_selopt-low.
lr_kunnr-high = ls_selopt-high.
APPEND lr_kunnr.
CLEAR: lr_kunnr.
ENDLOOP.
*Select Customer name
SELECT kunnr name1
FROM kna1
INTO TABLE gt_kna1
WHERE kunnr IN lr_kunnr.
*Modify record_tab to append Customer name to customer number
LOOP AT record_tab INTO ls_record.
v_kunnr = ls_record-string+18(10).
READ TABLE gt_kna1 WITH KEY kunnr = v_kunnr.
IF sy-subrc = 0.
ls_start = ls_record-string+0(28).
ls_end = ls_record-string+28( * ).
CLEAR: ls_record-string.
ls_record-string+0(28) = ls_start.
ls_record-string+29(1) = '-'.
ls_record-string+30(36) = gt_kna1-name1.
ls_record-string+66( * ) = ls_end.
MODIFY record_tab FROM ls_record.
CLEAR: ls_record,ls_start,ls_end,gt_kna1,v_kunnr.
ENDIF.
ENDLOOP.
ENDFUNCTION.
URL https://www.sdn.sap.com/irj/sdn/wiki?path=/display/Snippets/Implementing+Search+Help+Exits&
相关:http://blog.csdn.net/CompassButton/archive/2007/01/24/1492229.aspx
关于search help Exit和样例
1、 search help Exit是用于对标准帮助进行扩充,以提供更好的灵活性。
2、search help Exit何时被调用
Before Displaying the Dialog Box for Selecting the Required Search Path.
It is only called for collective search helps. Using the search help exit, the set of elementary search helps available can for example be restricted depending on the context.
Before Starting the F4 Process for the Elementary Search Help
The call is triggered independent of whether the dialog window for entering the search conditions appears or whether the selection is executed immediately (for example, because in the Hot key of the elementary search help Immediate value display is set).
Before Displaying the Dialog Box for Entering Search Conditions.
You can either influence the dialog for entering search conditions or skip it altogether here. You can also influence how the selection screen looks. The call is triggered only if there is no direct selection (that is, if in the Hot key of the elementary search help Immediate value display is not set).
Before Selecting Data.
The data selection can be partly or completely copied from the search help exit. This can become necessary if the data selection cannot be implemented with a SELECT statement for a table or a view.
Before Displaying the Hit List.
You can influence the display of the hit list in this step with the search help exit. You can reduce the number of values displayed here. For example, you can display only values for which the person calling the input help has authorization. You can also copy the complete hit list from the search help exit.
Before Returning the Values Selected by the User to the Input Template.
It could be advisable to intervene at this time if control of the further transaction flow should depend on the value selected. A typical example is setting set/get parameters.
3、search help Exit的函数模板(F4IF_SHLP_EXIT_EXAMPLE )程序员就可以拷贝该函数进行改编,已达到适合自己企业的应用。
function f4if_shlp_exit_example.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*" TABLES
*" SHLP_TAB TYPE SHLP_DESCT
*" RECORD_TAB STRUCTURE SEAHLPRES
*" CHANGING
*" VALUE(SHLP) TYPE SHLP_DESCR
*" VALUE(CALLCONTROL) LIKE DDSHF4CTRL STRUCTURE DDSHF4CTRL
*"----------------------------------------------------------------------
* EXIT immediately, if you do not want to handle this step
if callcontrol-step <> 'SELONE' and
callcontrol-step <> 'SELECT' and
" AND SO ON
callcontrol-step <> 'DISP'.
exit.
endif.
*"----------------------------------------------------------------------
* STEP SELONE (Select one of the elementary searchhelps)
*"----------------------------------------------------------------------
* This step is only called for collective searchhelps. It may be used
* to reduce the amount of elementary searchhelps given in SHLP_TAB.
* The compound searchhelp is given in SHLP.
* If you do not change CALLCONTROL-STEP, the next step is the
* dialog, to select one of the elementary searchhelps.
* If you want to skip this dialog, you have to return the selected
* elementary searchhelp in SHLP and to change CALLCONTROL-STEP to
* either to 'PRESEL' or to 'SELECT'.
if callcontrol-step = 'SELONE'.
* PERFORM SELONE .........
exit.
endif.
*"----------------------------------------------------------------------
* STEP PRESEL (Enter selection conditions)
*"----------------------------------------------------------------------
* This step allows you, to influence the selection conditions either
* before they are displayed or in order to skip the dialog completely.
* If you want to skip the dialog, you should change CALLCONTROL-STEP
* to 'SELECT'.
* Normaly only SHLP-SELOPT should be changed in this step.
if callcontrol-step = 'PRESEL'.
* PERFORM PRESEL ..........
exit.
endif.
*"----------------------------------------------------------------------
* STEP SELECT (Select values)
*"----------------------------------------------------------------------
* This step may be used to overtake the data selection completely.
* To skip the standard seletion, you should return 'DISP' as following
* step in CALLCONTROL-STEP.
* Normally RECORD_TAB should be filled after this step.
* Standard function module F4UT_RESULTS_MAP may be very helpfull in this
* step.
if callcontrol-step = 'SELECT'.
* PERFORM STEP_SELECT TABLES RECORD_TAB SHLP_TAB
* CHANGING SHLP CALLCONTROL RC.
* IF RC = 0.
* CALLCONTROL-STEP = 'DISP'.
* ELSE.
* CALLCONTROL-STEP = 'EXIT'.
* ENDIF.
exit. "Don't process STEP DISP additionally in this call.
endif.
*"----------------------------------------------------------------------
* STEP DISP (Display values)
*"----------------------------------------------------------------------
* This step is called, before the selected data is displayed.
* You can e.g. modify or reduce the data in RECORD_TAB
* according to the users authority.
* If you want to get the standard display dialog afterwards, you
* should not change CALLCONTROL-STEP.
* If you want to overtake the dialog on you own, you must return
* the following values in CALLCONTROL-STEP:
* - "RETURN" if one line was selected. The selected line must be
* the only record left in RECORD_TAB. The corresponding fields of
* this line are entered into the screen.
* - "EXIT" if the values request should be aborted
* - "PRESEL" if you want to return to the selection dialog
* Standard function modules F4UT_PARAMETER_VALUE_GET and
* F4UT_PARAMETER_RESULTS_PUT may be very helpfull in this step.
if callcontrol-step = 'DISP'.
* PERFORM AUTHORITY_CHECK TABLES RECORD_TAB SHLP_TAB
* CHANGING SHLP CALLCONTROL.
exit.
endif.
endfunction.
4、样例代码(参考sap的实例SFLIGHT)
SFLIGHT(SE11)中设置了search help exit SAPBC_GLOBAL_F4_SFLIGHT 。代码如下:
function sapbc_global_f4_sflight.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*" TABLES
*" SHLP_TAB TYPE SHLP_DESCT
*" RECORD_TAB STRUCTURE SEAHLPRES
*" CHANGING
*" VALUE(SHLP) TYPE SHLP_DESCR
*" VALUE(CALLCONTROL) LIKE DDSHF4CTRL STRUCTURE DDSHF4CTRL
*"----------------------------------------------------------------------
* The scope of this search help exit is decribed in the documentation.
* Note that SAPBC_GLOBAL_F4_SFLIGHT_MIN_FR provides a more complex
* example of a serch help exit.
data: begin of seatinfo occurs 0, " parallel table to
seatsmax type sflight-seatsmax, " record_tab containing
seatsocc type sflight-seatsocc, " the information about
seatsfre type s_seatsfre, " the seats
end of seatinfo.
if callcontrol-step = 'SELECT'.
* The search help parameters SEATSMAX and SEATSOCC are not displayed on
* the list of possible entries. Hence, provided they are not connected
* to dynpro fields, the F4 processor might consider it not to be
* necessary to select the contents of these fields.
* But this exit needs these contents in order to compute the number of
* free seats. Thus, the space for this contents is allocated by the
* following two calls. This automatically forces the F4 processor to
* fill this space with the contents of these fields.
call function 'F4UT_PARAMETER_ALLOCATE'
exporting
parameter = 'SEATSMAX'
tables
shlp_tab = shlp_tab
record_tab = record_tab
changing
shlp = shlp
callcontrol = callcontrol.
call function 'F4UT_PARAMETER_ALLOCATE'
exporting
parameter = 'SEATSOCC'
tables
shlp_tab = shlp_tab
record_tab = record_tab
changing
shlp = shlp
callcontrol = callcontrol.
endif.
check callcontrol-step = 'DISP'.
* This Exit only has to do something before the list of possible values
* is displayed. At that moment it has to compute the number of free
* seats for each row and attach it to the result of the selection
* process
* First fill the seatsmax-Info from the selected data. Note that there
* are two ways of using F4UT_PARAMETER_VALUE_GET described in the
* documetation of that function module. Here the second one is used.
call function 'F4UT_PARAMETER_VALUE_GET'
exporting
parameter = 'SEATSMAX'
" Reference to search help parameter
fieldname = 'SEATSMAX'
" Reference to field of Seatinfo
tables
shlp_tab = shlp_tab
record_tab = record_tab
results_tab = seatinfo
changing
shlp = shlp
callcontrol = callcontrol.
* Now do the same with the seatsocc-Info:
call function 'F4UT_PARAMETER_VALUE_GET'
exporting
parameter = 'SEATSOCC'
" Reference to search help parameter
fieldname = 'SEATSOCC'
" Reference to field of Seatinfo
tables
shlp_tab = shlp_tab
record_tab = record_tab
results_tab = seatinfo
changing
shlp = shlp
callcontrol = callcontrol.
* Now compute the number of free seats:
loop at seatinfo.
if seatinfo-seatsocc < seatinfo-seatsmax.
seatinfo-seatsfre = seatinfo-seatsmax - seatinfo-seatsocc.
else.
clear seatinfo-seatsfre.
endif.
modify seatinfo transporting seatsfre.
endloop.
* Finally transport the computed numbers into the search help data.
call function 'F4UT_PARAMETER_RESULTS_PUT'
exporting
parameter = 'SEATSFRE'
" Reference to search help parameter
fieldname = 'SEATSFRE'
" Reference to field of Seatinfo
tables
shlp_tab = shlp_tab
record_tab = record_tab
source_tab = seatinfo
changing
shlp = shlp
callcontrol = callcontrol.
endfunction.
具体处理流程如图: