Monday, June 3, 2013

Create Dynamic structure in ABAP

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,
      is_fcat LIKE LINE OF it_fcat.
DATA: it_fieldcat TYPE lvc_t_fcat,
      is_fieldcat LIKE LINE OF it_fieldcat.
DATA: new_table TYPE REF TO data.
DATA: new_line  TYPE REF TO data.
FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,
               <l_line>  TYPE ANY,
               <l_field> TYPE ANY.

* Build fieldcat
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
           i_structure_name = 'SYST'
       CHANGING
           ct_fieldcat      = it_fcat[].
   LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.
        MOVE-CORRESPONDING is_fcat TO is_fieldcat.
        is_fieldcat-fieldname = is_fcat-fieldname.
        is_fieldcat-ref_field = is_fcat-fieldname.
        is_fieldcat-ref_table = is_fcat-ref_tabname.
        APPEND is_fieldcat TO it_fieldcat.
   ENDLOOP.

* Create a new Table
CALL METHOD cl_alv_table_create=>create_dynamic_table
       EXPORTING
        it_fieldcatalog = it_fieldcat
       IMPORTING
        ep_table        = new_table.

* Create a new Line with the same structure of the table.
ASSIGN new_table->* TO <l_table>.
CREATE DATA new_line LIKE LINE OF <l_table>.
ASSIGN new_line->* TO <l_line>.

* Test it...
   DO 30 TIMES.
      ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
      <l_field> = sy-index.
      INSERT <l_line> INTO TABLE <l_table>.
   ENDDO.

   LOOP AT <l_table> ASSIGNING <l_line>.
      ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.
      WRITE <l_field>.
   ENDLOOP. 

Lock/Unlock the ABAP Editor Lock Field

REPORT ZEDITOR. 
 
TABLES: TRDIR. "System table TRDIR 
 
PARAMETERS: PROGRAM LIKE TRDIR-NAME. 
PARAMETERS: EDITOR LIKE TRDIR-EDTX. 
 
SELECT SINGLE * FROM TRDIR WHERE NAME = PROGRAM. 
 
TRDIR-EDTX = EDITOR. 
MODIFY TRDIR. 
IF SY-SUBRC EQ 0. 
   WRITE: / 'Editor Lock update Successful ', TRDIR-NAME. 
   IF TRDIR-EDTX = 'X'. 
      WRITE: ' Lock'. 
   ELSE. 
      WRITE: ' UnLock'. 
   ENDIF. 
ELSE. 
   WRITE: / 'Editor Lock update Unsuccessful ', TRDIR-NAME. 
ENDIF. 



ABAP Program to check for a holiday using factory calendar

REPORT Z_HOLIDAY.
 
* ABAP Program to check for holidays using the factory calendar
* include zday .
* substitute tdate = 'yyyymmdd'.
* tholiday_found   = 'X'   -> Holiday
TABLES THOCS.
 
DATA: BEGIN OF INT_THOCS OCCURS 100,
      THOCS LIKE THOCS.
DATA: END OF INT_THOCS.
 
DATA: TDAY(1),
*      TDATE LIKE SY-DATUM,
      THOLIDAY_ATTRIBUTES,
      THOLIDAY_FOUND(1).
 
PARAMETERS: P_DATE LIKE SY-DATUM DEFAULT SY-DATUM.
 
*&------- Selection ------------------------
START-OF-SELECTION.
 
  PERFORM HOLIDAY.
 
FORM HOLIDAY.
CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'
     EXPORTING
          DATE                         = P_DATE
          HOLIDAY_CALENDAR_ID          = 'XX'
*         WITH_HOLIDAY_ATTRIBUTES      = ' '
     IMPORTING
          HOLIDAY_FOUND                = THOLIDAY_FOUND
     TABLES
          HOLIDAY_ATTRIBUTES           = INT_THOCS
     EXCEPTIONS
          CALENDAR_BUFFER_NOT_LOADABLE = 1
          DATE_AFTER_RANGE             = 2
          DATE_BEFORE_RANGE            = 3
          DATE_INVALID                 = 4
          HOLIDAY_CALENDAR_ID_MISSING  = 5
          HOLIDAY_CALENDAR_NOT_FOUND   = 6
          OTHERS                       = 7.
 
CALL FUNCTION 'DATE_COMPUTE_DAY'
     EXPORTING
          DATE    = P_DATE
     IMPORTING
          DAY     = TDAY
     EXCEPTIONS
          OTHERS  = 1.
 
* For checking.
if tholiday_found = 'X'.
   write: /1 'Holiday ', P_DATE.
else.
   write: /1 'Not Holiday ', P_DATE.
endif.
 
case sy-subrc.
   when 0.
     write: /1 P_DATE, 'is', tday, 'day of the week.'.
   when others.
     write: /1 'Unknown day ', P_DATE.
endcase.
 
ENDFORM.


How to call one report to another?

Here ZREP2 CALLS report ZREP1 when you click on output of report ZREP2.

REPORT ZREP1.
parameter : paramet(18) type c.
write : paramet. 

REPORT ZREP2 NO STANDARD PAGE HEADING.
tables: qals.
RANGES seltab for qals-prueflos.
WRITE: 'Select a Selection!',
/ '--------------------'.
SKIP.
FORMAT HOTSPOT COLOR 5 INVERSE ON.
WRITE: 'Selection 1',
/ 'Selection 2'.
AT LINE-SELECTION.
CASE SY-LILLI.
WHEN 4.
SUBMIT zrep1 VIA SELECTION-SCREEN
WITH PARAMET EQ 'Selection 1'
WITH SELECTO IN SELTAB
WITH SELECTO NE 3
AND RETURN.
WHEN 5.
 
SUBMIT zrep1 VIA SELECTION-SCREEN
WITH PARAMET EQ 'Selection 1'

AND RETURN.
ENDCASE. 

Inserting Website Links in ABAP

REPORT ZURL NO STANDARD PAGE HEADING.

DATA: BEGIN OF URL_TABLE OCCURS 10,
L(25),
END OF URL_TABLE.

URL_TABLE-L = 'http://www.lycos.com'.APPEND URL_TABLE.
URL_TABLE-L = 'http://www.hotbot.com'.APPEND URL_TABLE.
URL_TABLE-L = 'http://www.sap.com'.APPEND URL_TABLE.

LOOP AT URL_TABLE.
  SKIP. FORMAT INTENSIFIED OFF.
  WRITE: / 'Single click on '.
  FORMAT HOTSPOT ON.FORMAT INTENSIFIED ON.
  WRITE: URL_TABLE. HIDE URL_TABLE.
  FORMAT HOTSPOT OFF.FORMAT INTENSIFIED OFF.
  WRITE: 'to go to', URL_TABLE.
ENDLOOP.
CLEAR URL_TABLE.

AT LINE-SELECTION.

IF NOT URL_TABLE IS INITIAL.

  CALL FUNCTION 'WS_EXECUTE'
       EXPORTING
            program = 'C:\Program Files\Internet Explorer\IEXPLORE.EXE'
            commandline     = URL_TABLE
            INFORM         = ''
          EXCEPTIONS
            PROG_NOT_FOUND = 1.
  IF SY-SUBRC <> 0.
     WRITE:/ 'Cannot find program to open Internet'.
  ENDIF.

ENDIF.


How to call any html page into SAP report

Used this function module: 
    
  CALL FUNCTION 'GUI_RUN' 
  EXPORTING 
  command ='IEXPLORE.EXE' 
  PARAMETER ='WWW.YAHOOMAIL.COM'. 
  * CD = 
  * IMPORTING 
  * RETURNCODE = 

Transferring data to a file (presentation server)

REPORT ZTRANSFERDATA.

* Data declarations for later use
PARAMETERS FILENAME(128) DEFAULT 'c:\users\default\testfile.dat'
                         LOWER CASE.
TABLES CUSTOMERS.
DATA ALL_CUSTOMERS LIKE CUSTOMERS OCCURS 100
                   WITH HEADER LINE.
* Get data for file transfer
SELECT * FROM CUSTOMERS INTO TABLE ALL_CUSTOMERS.
SORT ALL_CUSTOMERS BY CITY.
LOOP AT ALL_CUSTOMERS.
  WRITE: / ALL_CUSTOMERS-CITY,
           ALL_CUSTOMERS-NAME.
ENDLOOP.
* Transferring Data
CALL FUNCTION 'WS_DOWNLOAD'
     EXPORTING
          FILENAME            = FILENAME
     TABLES
          DATA_TAB            = ALL_CUSTOMERS
     EXCEPTIONS
          FILE_OPEN_ERROR     = 1
          OTHERS              = 2.
CASE SY-SUBRC.
  WHEN 1.
    WRITE 'Error when file opened'.
    EXIT.
  WHEN 2.
    WRITE 'Error during data transfer'.
    EXIT.
ENDCASE.

Reading data from a file

REPORT ZREADDATA.

* Data declarations for later use
TABLES CUSTOMERS.
PARAMETERS FILENAME(128) DEFAULT '/usr/tmp/testfile.dat'
                         LOWER CASE.
DATA: MSG_TEXT(50),
      ALL_CUSTOMER_NAMES LIKE CUSTOMERS-NAME OCCURS 100
                        WITH HEADER LINE.
* Opening the File
OPEN DATASET FILENAME FOR INPUT IN TEXT MODE
                      MESSAGE MSG_TEXT.
IF SY-SUBRC NE 0.
  WRITE: 'File cannot be opened. Reason:', MSG_TEXT.
  EXIT.
ENDIF.
* Reading Data
DO.
  READ DATASET FILENAME INTO ALL_CUSTOMER_NAMES.
  IF SY-SUBRC NE 0.
    EXIT.
  ENDIF.
  APPEND ALL_CUSTOMER_NAMES.
ENDDO.
* Closing the file
CLOSE DATASET FILENAME.
* Display the result
LOOP AT ALL_CUSTOMER_NAMES.
  WRITE / ALL_CUSTOMER_NAMES.
ENDLOOP.


Create local directory on your PC via ABAP



REPORT ZGUI_DIR.
 
call function 'GUI_CREATE_DIRECTORY'
  exporting
    dirname       = 'C:\TEST\'
  EXCEPTIONS
    FAILED        = 1
    OTHERS        = 2.
 
if sy-subrc <> 0.
  WRITE: / 'Error Code ',SY-SUBRC.
else.
  WRITE: / 'Directory Created...'.
endif.

Sunday, June 2, 2013

Easy Way To Remember Table In SAP

The easiest way to remember the table in sap is:

Just try to remember the sap terminology of representing the business or buisness objects... such as
all vendor tables start with L such as lfa1, etc..
all customer tables start with K ... Kna1, konv...
all sales tables start with V... vbak vbap
all master data tables start with T... T001, t001w
all bank tables start with B.. Bknf, bkpf....
all purchasing tables start with E.. ekko, ekpo etc..
all material tables start with M... mara, makt, marc...

and

in sales, delivery, billing, purchasing...
if the tables name contains K init it is Header data
  such as vbak, likp, vbrk, ekko...
if the tables name contains P init it is Item data
  such as vbap, lips, vbrp, ekpo ....

Coming to FI tables... there are only 6 important tables in whole Finance module which are mostly used by abapers.

They can be remember by:
if the table name contains I, that is open item
if the table name contains A, that is closed item
if the table name ends with S, that is GL account
exp BSIS- GL master open items
    BSAS- GL master closed items
like wise...
if the table name ends with D, that is customer
if the table name ends with K, that is vendor

Finally, table TSTC contains the list of all transaction codes.

How to find user exits

User exits :
1. Introduction
2. How to find user exits
3. Using Project management of SAP Enhancements 
Content Author: Abhishek
1. Introduction:
User exits (Function module exits) are exits developed by SAP. The exit is implementerd as a call to a functionmodule. The code for the function module is writeen by the developer. You are not writing the code directly in the function module, but in the include that is implemented in the function module.
The naming standard of function modules for functionmodule exits is: 
EXIT_<program name><3 digit suffix>
The call to a functionmodule exit is implemented as:
CALL CUSTOMER.-FUNCTION <3 digit suffix>
Example:
The program for transaction VA01 Create salesorder is SAPMV45A
If you search for CALL CUSTOMER-FUNCTION i program
SAPMV45A you will find ( Among other user exits):
CALL CUSTOMER-FUNCTION '003'
  exporting
    xvbak   = vbak
    xvbuk   = vbuk
    xkomk   = tkomk
  importing
    lvf_subrc = lvf_subrc
  tables
    xvbfa = xvbfa
    xvbap = xvbap
    xvbup = xvbup.
The exit calls function module EXIT_SAPMV45A_003
2. How to find user exits?
Display the program where you are searching for and exit and search for CALL CUSTOMER-EXIT
If you know the Exit name, go to transaction CMOD.
Choose menu Utillities->SAP Enhancements. Enter the exit name and press enter.
You will now come to a screen that shows the function module exits for the exit.
3. Using Project management of SAP Enhancements, we want to create a project to enahance trasnaction VA01 .
- Go to transaction CMOD
- Create a project called ZVA01
- Choose the Enhancement assign radio button and press the Change button
In the first column enter V45A0002 Predefine sold-to party in sales document.
Note that an enhancement can only be used in 1 project. If the enhancement is already in use, and error message will be displayed
Press Save
Press Components. You can now see that enhancement uses user exit EXIT_SAPMV45A_002. Double click on the exit.
Now the function module is displayed. Double click on include ZXVVAU04 in the function module
Insert the following code into the include: E_KUNNR = '2155'.
Activate the include program. Go back to CMOD and activate the project.
Goto transaction VA01 and craete a salesorder.
Note that Sold-to-party now automatically is "2155"

List Box in ABAP Report

REPORT ZLISTBOX.

TYPE-POOLS: VRM.

DATA: NAME  TYPE VRM_ID,
      LIST  TYPE VRM_VALUES,
      VALUE LIKE LINE OF LIST.

PARAMETERS: PS_PARM(10) AS LISTBOX VISIBLE LENGTH 10.

AT SELECTION-SCREEN OUTPUT.

NAME = 'PS_PARM'.
VALUE-KEY = '1'.

VALUE-TEXT = 'LINE 1'.
APPEND VALUE TO LIST. VALUE-KEY = '2'.

VALUE-TEXT = 'LINE 2'.
APPEND VALUE TO LIST.

CALL FUNCTION 'VRM_SET_VALUES' EXPORTING ID = NAME VALUES = LIST.

START-OF-SELECTION.
WRITE: / 'PARAMETER:', PS_PARM.

Step by Step Installation Guide for SAP NetWeaver AS ABAP 7.02 SP6 32-bit Trial Version


Please click the below link for getting step by step Installation-----------