Wednesday, September 16, 2009

when and where we can use parallel cursor


Loop AT i_primary into wa_primary.
Loop AT i_secondary into wa_secondary from lv_index.
If ( wa_primary-field1 NE wa_secondary-field1 ) or
( wa_primary-field2 NE wa_secondary-field2 ).
Lv_index = sy-tabix.
Endif.
“ your code comes here “
Endloop.
Endloop.
To use this following should be true
1)      Both tables should be sorted with the same key ( in the above case field1 and field2 )
2)      The mapping between primary to secondary tables should be
a.       one to many ( for one record in primary there are multiple records in secondary )
b.      one to one (for one record in primary there is only one in secondary )
Please note that other than the above 2 other 2 scenarios (many to many & many to one) will not work with the above logic and make sure you don’t use in such case.
3)      The secondary table should not contain a record which does not map to the primary table ( this will not happen if the secondary is fetched using for all entries from primary )
4)      Please make sure you do not delete any record from primary before using the logic which will make the point 3 false  
5)      If you were to use a 'AND' in where statement of the inner loop you should use or in the inner loop if condition in the above logic. 
 

Friday, September 11, 2009

Copy to Clipborad SAP

copy to clipborad

Bellow code is how to copy data to clipboard. This feature helps you when there is a BDC recording and you want to add selection extended options

    DATA: v_rc TYPE I,
       it_data TYPE STANDARD TABLE OF CHAR128,
       wa_data TYPE char128.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>CLIPBOARD_EXPORT
    IMPORTING DATA = it_data
    CHANGING RC = v_rc      .

Monday, August 31, 2009

How to add a button on selection screen


one small thing I came across on a customer development was to add a button on the selection screen. I have not done such a requirement before for any customer so i didn't any idea on how to do it.

After doing some searching this is what I found out was :).

For a selection screen you can put up to 5 buttons this is a default feature which is available to use if you want.
For that these are the steps you have to do

I have added the button in two ways one is just the button with the text next one button with a icon.

The data declarations for these are.
TABLES: sscrfields.
DATA: functxt TYPE smp_dyntxt.
SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME
TITLE TEXT-001.
SELECT-OPTIONS: p_ktopl FOR t030-ktopl.

SELECTION-SCREEN END OF BLOCK block1.


SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN FUNCTION KEY 2.


functxt-icon_id = ICON_HISTORY.
functxt-quickinfo = 'log'(001).
sscrfields-functxt_01 = functxt.

MOVE 'Maintain Item Cat.' TO sscrfields-functxt_02.






Thursday, July 16, 2009

how to add tooltip to icon


its very easy to add tooltip to icon's

1) have a icon field in output table ( this can be a char field with 25 characters)

2) in case if you cant to add the lock icon for that icon field put
-icon = '@06\QIn process@'.

in this @06@ is the icon when you give the tooltip with '\QXXXtooltipXXX'
insert this part beore last @.

Note: if you want to find the icons check icon table :)

3) field catalog make the icon field 'X'.


there is a SAP example on program: BCALV_DEMO_TOOLTIP



Tuesday, June 23, 2009

How to Debug any screen on SAP

its very simple just put this code on a text file and save it with what even name you want e.g debug

[FUNCTION]
Command=/H
Title=Debugger
Type=SystemCommand



when ever you want to debug you dont have to use /h but just drag and drop this on the SAP screen this enables you to debug on popup screens

Hope this will be useful

nafran

Wednesday, June 10, 2009

how to get the first day and last day SAP

Most of the time when we need the first day and the last day of a given month we always run to get a function. why use a function when its so easy to code it

*get first and last date of month and year
CONCATENATE s_bdatj-low s_mnr-low '01' INTO gv_date1.

gv_month = s_mnr-low .
gv_month = gv_month + 01.

CONCATENATE s_bdatj-low gv_month '01' INTO gv_date2.

gv_date2 = gv_date2 - 1.

Monday, June 8, 2009

only way of parallel or Multi Threading on SAP

Sometimes you get programs where you might have done almost all the programming best practices and Indexing and other stuff but still the program will run very slow.On a time like this sometimes we might wonder is there anyway to do parallel processing or something like multi treading on SAP.
Hmm.... there is only one way that I can think about in SAP and we can get all most all features on Parallel processing from this way.


CALL FUNCTION 'Fetch_Data'
STARTING NEW TASK 'Fetch'
performing RETURN_FROM_WAIT ON END OF TASK
     EXPORTING
input            = input
TABLES
output             = output
EXCEPTIONS
COMMUNICATION_FAILURE = 1
SYSTEM_FAILURE        = 2.





Important : you cant use any import parameters on the function no this
method the only option is to using the RETURN_FROM_WAIT which is a
perform.


For Eg when you want to fetch data parallel do it using 2 functions and using the perform
you can have  a flag to make sure that fetching is completed.
  
This is just a over view I got this document from Daniel Perecky
Multi Threading

this has lots of information no how to program this way.
I would love to see some comments about this :)

Friday, May 22, 2009

create custom menu exits

I got this spacial requirement when i was working with a project.

where a generic menu bar is required which can add items according to the customer.

the first thing to do in such requirement is to add a fcode in menu painter starting with +XXX ( replace X).

once this is done handle the fcode using a function

CALL CUSTOMER-FUNCTION '010'.

with in this function you can do what ever you what to do for when that item is clicked.


then on a standard SAP enhancement(smod) add your program name and the fcode and add your function exit on it.

create a project on cmod and add the enhancement you will find the exit and the menu exit.

if this is not clear pls post a comment ( coz i just wrote it in a way how i will understand)

Thursday, May 7, 2009

best way to code nested loops SAP


REPORT  ZPARALLEL_CURSOR.
TABLES:
  likp,
  lips.

DATA:
      t_likp  
TYPE TABLE OF likp,
      t_lips  
TYPE TABLE OF lips,
      t_lips1 
TYPE SORTED TABLE OF lips
      
WITH NON-UNIQUE KEY vbeln.




DATA:
      w_runtime1 
TYPE I,
      w_runtime2 
TYPE I,
      w_counter 
TYPE I,
      w_index 
LIKE sy-INDEX.

DATA:
      likp_l 
TYPE I,
      lips_l 
TYPE I.

PARAMETERSrows TYPE i OBLIGATORY.

START-
OF-SELECTION.
SELECT *
FROM likp
INTO TABLE t_likp up to rows ROWS.



DESCRIBE TABLE t_likp LINES likp_l.

WRITE:/ 'records in likp: ' .
WRITE likp_l.

SELECT *
INTO TABLE t_lips
FROM lips
  
FOR ALL ENTRIES IN t_likp
  
WHERE vbeln = t_likp-vbeln.


DESCRIBE TABLE t_lips LINES lips_l.


WRITE:/ 'records in lips: ' .
WRITE lips_l.


**********************************************************************
**********************************************************************
**********************************************************************

GET RUN TIME FIELD w_runtime1.

LOOP AT t_likp INTO likp.
  
LOOP AT t_lips INTO lips WHERE vbeln EQ likp-vbeln.

  
ENDLOOP.
ENDLOOP.

GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

WRITE:/.
WRITE:/'Time taken on normal where condition:'.
WRITE: w_runtime2.

**********************************************************************
**********************************************************************
**********************************************************************
GET RUN TIME FIELD w_runtime1.
SORT t_likp BY vbeln.
SORT t_lips BY vbeln.

LOOP AT t_likp INTO likp.

  
LOOP AT t_lips INTO lips FROM w_index.
    
IF likp-vbeln NE lips-vbeln.
      w_index = sy-tabix.
      
EXIT.
    
ENDIF.

  
ENDLOOP.
ENDLOOP.


GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.


WRITE:/.
WRITE:/.
WRITE:/'Time taken with parallel cursor:' , w_runtime2.



**********************************************************************
**********************************************************************
**********************************************************************

GET RUN TIME FIELD w_runtime1.

SORT t_lips BY vbeln.
t_lips1[] = t_lips[].




LOOP AT t_likp INTO likp.
  
LOOP AT t_lips1 INTO lips WHERE vbeln EQ likp-vbeln.
  
ENDLOOP.

ENDLOOP.

GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

WRITE:/.
WRITE:/.
WRITE:/'Time taken for a sorted table with where condition:' , w_runtime2.





Thursday, April 23, 2009

Use of binary search option

When a programmer uses the read command, the table is sequentially searched. This slows down the processing. Instead of this, use the binary search addition. The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.

Not Recommended

Read table int_fligh with key airln = ‘LF’.

Recommended

Read table int_fligh with key airln = ‘LF’ binary search.


source: http://www.thespot4sap.com/Articles/SAPABAPPerformanceTuning_BinarySearch.asp

Modifying a group of lines of an internal table

Use the variations of the modify command to speed up this kind of processing.

Not recommended

Loop at int_fligh.

If int_fligh-flag is initial.

Int_fligh-flag = ‘X’.

Endif.

Modify int_fligh.

Endloop.

Recommended

Int_fligh-flag = ‘X’.

Modify int_fligh transporting flag where flag is initial.


source: http://www.thespot4sap.com/Articles/SAPABAPPerformanceTuning_ModifyingGroupOfLines.asp


Wednesday, April 15, 2009

copy ALV layouts from a one program to another

*&---------------------------------------------------------------------*
*& Report  Z_COPY_ALV_VARNTS_PROG_TO_PROG
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  Z_COPY_ALV_VARNTS_PROG_TO_PROG.


* =====================================================================
* Data Declarations Section
* =====================================================================

* ---------------------------------------------------------------------
DATA : BEGIN OF MYLTDX OCCURS 0 .
INCLUDE STRUCTURE LTDX .
DATA : END OF MYLTDX .
* ---------------------------------------------------------------------
DATA : BEGIN OF MYLTDXT OCCURS 0 .
INCLUDE STRUCTURE LTDXT .
DATA : END OF MYLTDXT .
* ---------------------------------------------------------------------
DATA : BEGIN OF MYLTDXD OCCURS 0 .
INCLUDE STRUCTURE LTDXD .
DATA : END OF MYLTDXD .
* ---------------------------------------------------------------------

DATA : MANS(1TYPE C .

DATA : PROGRAMM LIKE RS38M-PROGRAMM .
DATA : BEGIN OF MDYNPFIELDS OCCURS 1 .
INCLUDE STRUCTURE DYNPREAD .
DATA : END OF MDYNPFIELDS .
CONSTANTS BUTTONSELECTED(1TYPE C VALUE 'X' .

* =====================================================================
* Macro for Inputing Filenames
* =====================================================================
DEFINE GET_FILENAME .
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
* DEF_FILENAME = ' '
DEF_PATH = &1
MASK = ',*.*,*.*.'
MODE = '0'
* TITLE = ' '
IMPORTING
FILENAME = &2
* RC =
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5.

END-OF-DEFINITION .

* =====================================================================
* Macro for Downloading to ASCII Files
* =====================================================================
DEFINE DOWNLOAD_TO_ASCII .
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
* BIN_FILESIZE = ' '
* CODEPAGE = ' '
FILENAME = &1
FILETYPE = 'DAT'
* MODE = ' '
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* COL_SELECT = ' '
* COL_SELECTMASK = ' '
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = &2
* FIELDNAMES =
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TABLE_WIDTH = 4
INVALID_TYPE = 5
NO_BATCH = 6
UNKNOWN_ERROR = 7
GUI_REFUSE_FILETRANSFER = 8
OTHERS = 9.

END-OF-DEFINITION .

* =====================================================================
* Macro for uploading Data from ASCII files
* =====================================================================
DEFINE UPLOAD_FROM_ASCII .
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
* CODEPAGE = ' '
FILENAME = &1
FILETYPE = 'DAT'
* HEADLEN = ' '
* LINE_EXIT = ' '
* TRUNCLEN = ' '
* USER_FORM = ' '
* USER_PROG = ' '
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = &2
EXCEPTIONS
CONVERSION_ERROR = 1
FILE_OPEN_ERROR = 2
FILE_READ_ERROR = 3
INVALID_TABLE_WIDTH = 4
INVALID_TYPE = 5
NO_BATCH = 6
UNKNOWN_ERROR = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10.
END-OF-DEFINITION .

* =====================================================================
* Selection Screen Default
* =====================================================================
PARAMETERS : P_FROM_P LIKE RS38M-PROGRAMM OBLIGATORY .
PARAMETERS : P_TO_P LIKE RS38M-PROGRAMM OBLIGATORY .
PARAMETERS : P_SAME_S RADIOBUTTON GROUP GRP1 DEFAULT 'X' .
PARAMETERS : P_DOWNLD RADIOBUTTON GROUP GRP1 .
PARAMETERS : P_UPLOAD RADIOBUTTON GROUP GRP1 .
PARAMETERS : P_FILE_x LIKE RLGRAP-FILENAME DEFAULT 'c:\LTDX.txt' .
PARAMETERS : P_FILE_t LIKE RLGRAP-FILENAME DEFAULT 'c:\LTDXT.txt' .
PARAMETERS : P_FILE_d LIKE RLGRAP-FILENAME DEFAULT 'c:\LTDXD.txt' .

* =====================================================================
* At Selection Screen Events
* =====================================================================
AT SELECTION-SCREEN .
PROGRAMM = P_FROM_P .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE_x .
GET_FILENAME 'c:\LTDX.txt' P_FILE_x .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE_t .
GET_FILENAME 'c:\LTDXT.txt' P_FILE_t .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE_d .
GET_FILENAME 'c:\LTDXD.txt' P_FILE_d .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FROM_P .
CLEAR MDYNPFIELDS . REFRESH MDYNPFIELDS .
MDYNPFIELDS-FIELDNAME = 'P_FROM_P' .
APPEND MDYNPFIELDS .
CALL FUNCTION 'DYNP_VALUES_READ'
EXPORTING
DYNAME = SY-CPROG
DYNUMB = SY-DYNNR
TABLES
DYNPFIELDS = MDYNPFIELDS
EXCEPTIONS
INVALID_ABAPWORKAREA = 1
INVALID_DYNPROFIELD = 2
INVALID_DYNPRONAME = 3
INVALID_DYNPRONUMMER = 4
INVALID_REQUEST = 5
NO_FIELDDESCRIPTION = 6
INVALID_PARAMETER = 7
UNDEFIND_ERROR = 8
DOUBLE_CONVERSION = 9
STEPL_NOT_FOUND = 10
OTHERS = 11.

READ TABLE MDYNPFIELDS INDEX 1 .
PROGRAMM = MDYNPFIELDS-FIELDVALUE .
CALL FUNCTION 'REPOSITORY_INFO_SYSTEM_F4'
EXPORTING
OBJECT_TYPE = 'PROG'
OBJECT_NAME = PROGRAMM
IMPORTING
OBJECT_NAME_SELECTED = PROGRAMM
EXCEPTIONS
CANCEL = 1
WRONG_TYPE = 2
OTHERS = 3.
P_FROM_P = PROGRAMM .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_TO_P .
CLEAR MDYNPFIELDS . REFRESH MDYNPFIELDS .
MDYNPFIELDS-FIELDNAME = 'P_TO_P' .
APPEND MDYNPFIELDS .
CALL FUNCTION 'DYNP_VALUES_READ'
EXPORTING
DYNAME = SY-CPROG
DYNUMB = SY-DYNNR
TABLES
DYNPFIELDS = MDYNPFIELDS
EXCEPTIONS
INVALID_ABAPWORKAREA = 1
INVALID_DYNPROFIELD = 2
INVALID_DYNPRONAME = 3
INVALID_DYNPRONUMMER = 4
INVALID_REQUEST = 5
NO_FIELDDESCRIPTION = 6
INVALID_PARAMETER = 7
UNDEFIND_ERROR = 8
DOUBLE_CONVERSION = 9
STEPL_NOT_FOUND = 10
OTHERS = 11.

READ TABLE MDYNPFIELDS INDEX 1 .
PROGRAMM = MDYNPFIELDS-FIELDVALUE .
CALL FUNCTION 'REPOSITORY_INFO_SYSTEM_F4'
EXPORTING
OBJECT_TYPE = 'PROG'
OBJECT_NAME = PROGRAMM
IMPORTING
OBJECT_NAME_SELECTED = PROGRAMM
EXCEPTIONS
CANCEL = 1
WRONG_TYPE = 2
OTHERS = 3.
P_TO_P = PROGRAMM .

* =====================================================================
* Start of Selection
* =====================================================================
START-OF-SELECTION .
CASE BUTTONSELECTED.
WHEN P_SAME_S .
PERFORM COPY_FROM_PROG_TO_PROG .
WHEN P_DOWNLD .
PERFORM VDOWNLOAD .
WHEN P_UPLOAD .
PERFORM VUPLOAD .
ENDCASE .

*&---------------------------------------------------------------------*
*& Form COPY_FROM_PROG_TO_PROG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM COPY_FROM_PROG_TO_PROG.

CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
* DEFAULTOPTION = 'Y'
TEXTLINE1 = 'Are you sure you want to copy Variants ? '
* TEXTLINE2 = ' '
TITEL = 'Confirmation '
* START_COLUMN = 25
* START_ROW = 6
* CANCEL_DISPLAY = 'X'
IMPORTING
ANSWER = MANS
EXCEPTIONS
OTHERS = 1.

IF MANS = 'J' .
* ---------------------------------------------------------------------
REFRESH MYLTDX . CLEAR MYLTDX .
SELECT * FROM LTDX INTO TABLE MYLTDX
WHERE REPORT = P_FROM_P.
LOOP AT MYLTDX .
MYLTDX-REPORT = P_TO_P .
MODIFY MYLTDX .
ENDLOOP .
IF SY-SUBRC = 0 .
DELETE FROM LTDX WHERE REPORT = P_TO_P .
INSERT LTDX FROM TABLE MYLTDX .
ENDIF .
* ---------------------------------------------------------------------
REFRESH MYLTDXT . CLEAR MYLTDXT .
SELECT * FROM LTDXT INTO TABLE MYLTDXT
WHERE REPORT = P_FROM_P.
LOOP AT MYLTDXT .
MYLTDXT-REPORT = P_TO_P .
MODIFY MYLTDXT .
ENDLOOP .
IF SY-SUBRC = 0 .
DELETE FROM LTDXT WHERE REPORT = P_TO_P .
INSERT LTDXT FROM TABLE MYLTDXT .
ENDIF .
* ---------------------------------------------------------------------
REFRESH MYLTDXT . CLEAR MYLTDXT .
SELECT * FROM LTDXT INTO TABLE MYLTDXT
WHERE REPORT = P_FROM_P.
LOOP AT MYLTDXT .
MYLTDXT-REPORT = P_TO_P .
MODIFY MYLTDXT .
ENDLOOP .
IF SY-SUBRC = 0 .
DELETE FROM LTDXT WHERE REPORT = P_TO_P .
INSERT LTDXT FROM TABLE MYLTDXT .
ENDIF .
* ---------------------------------------------------------------------
ENDIF .

ENDFORM" COPY_FROM_PROG_TO_PROG

*&---------------------------------------------------------------------*
*& Form VDOWNLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM VDOWNLOAD.
REFRESH MYLTDX . CLEAR MYLTDX .
SELECT * FROM LTDX INTO TABLE MYLTDX
WHERE REPORT = P_FROM_p.
DOWNLOAD_TO_ASCII P_FILE_x MYLTDX .

* ---------------------------------------------------------------------
REFRESH MYLTDXT . CLEAR MYLTDXT .
SELECT * FROM LTDXT INTO TABLE MYLTDXT
WHERE REPORT = P_FROM_P.
DOWNLOAD_TO_ASCII P_FILE_t MYLTDXT .

* ---------------------------------------------------------------------
REFRESH MYLTDXT . CLEAR MYLTDXT .
SELECT * FROM LTDXD INTO TABLE MYLTDXT
WHERE REPORT = P_FROM_P.
DOWNLOAD_TO_ASCII P_FILE_d MYLTDXT .

* ---------------------------------------------------------------------

ENDFORM" VDOWNLOAD

*&---------------------------------------------------------------------*
*& Form VUPLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM VUPLOAD.
CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'
EXPORTING
* DEFAULTOPTION = 'Y'
TEXTLINE1 =
'Are you sure you want to upload Variants ? '
* TEXTLINE2 = ' '
TITEL = 'Confirmation '
* START_COLUMN = 25
* START_ROW = 6
* CANCEL_DISPLAY = 'X'
IMPORTING
ANSWER = MANS
EXCEPTIONS
OTHERS = 1.

IF MANS = 'J' .
* ---------------------------------------------------------------------
REFRESH MYLTDX . CLEAR MYLTDX .
UPLOAD_FROM_ASCII P_FILE_x MYLTDX .
LOOP AT MYLTDX .
MYLTDX-REPORT = P_TO_P .
MODIFY MYLTDX .
ENDLOOP .
IF SY-SUBRC = 0 .
DELETE FROM LTDX WHERE REPORT = P_TO_P .
INSERT LTDX FROM TABLE MYLTDX .
ENDIF .
* ---------------------------------------------------------------------
REFRESH MYLTDXT . CLEAR MYLTDXT .
UPLOAD_FROM_ASCII P_FILE_t MYLTDXT .
LOOP AT MYLTDXT .
MYLTDXT-REPORT = P_TO_P .
MODIFY MYLTDXT .
ENDLOOP .
IF SY-SUBRC = 0 .
DELETE FROM LTDXT WHERE REPORT = P_TO_P .
INSERT LTDXT FROM TABLE MYLTDXT .
ENDIF .
* ---------------------------------------------------------------------
REFRESH MYLTDXD . CLEAR MYLTDXD .
UPLOAD_FROM_ASCII P_FILE_d MYLTDXT .
LOOP AT MYLTDXT .
MYLTDXT-REPORT = P_TO_P .
MODIFY MYLTDXT .
ENDLOOP .
IF SY-SUBRC = 0 .
DELETE FROM LTDXT WHERE REPORT = P_TO_P .
INSERT LTDXT FROM TABLE MYLTDXT .
ENDIF .
* ---------------------------------------------------------------------
ENDIF .

ENDFORM" VUPLOAD
Google