Search This Blog

Monday, March 8, 2010

Difference Between BADI and User Exits/Customer Exits :


  1. In user exits we go by general method for enhancements while BADIs we use objects (oops concepts) methods for enhancement. 
  2. User exits are used for adding additional functionality to the existing SAP standard transactions. Using User exits we can add additional functionality standard SAP functionality without making any changes to the original code. BADI is a standardized interface for ABAP sources that enables partners and customers to enhance SAP-delivered programs in their namespace. 
  3. Business Add-Ins are a new SAP enhancement technique based on ABAP Objects. They can be inserted into the SAP System to accommodate user requirements too specific to be included in the standard delivery. Since specific industries often require special functions, SAP allows you to predefine these points in your software. 
  4. As with customer exits two different views are available: 
    In the definition view, an application programmer predefines exit points in a source that allow specific industry sectors, partners, and customers to attach additional software to standard SAP source code without having to modify the original object.
    In the implementation view, the users of Business Add-Ins can customize the logic they need or use a standard logic if one is available.
  5. In contrast to customer exits, Business Add-Ins no longer assume a two-level infrastructure (SAP and customer solutions), but instead allow for a multi-level system landscape (SAP, partner, and customer solutions, as well as country versions, industry solutions, and the like). Definitions and implementations of Business Add-Ins can be created at each level within such a system infrastructure.
  6. SAP guarantees the upward compatibility of all Business Add-In interfaces. Release upgrades do not affect enhancement calls from within the standard software nor do they affect the validity of call interfaces. You do not have to register Business Add-Ins in SSCR.
  7. The Business Add-In enhancement technique differentiates between enhancements that can only be implemented once and enhancements that can be used actively by any number of customers at the same time. In addition, Business Add-Ins can be defined according to filter values. This allows you to control add-in implementation and make it dependent on specific criteria (on a specific Country value, for example).
  8. All ABAP sources, screens, GUIs, and table interfaces created using this enhancement technique are defined in a manner that allows customers to include their own enhancements in the standard. A single Business Add-In contains all of the interfaces necessary to implement a specific task.

Difference between User exits & Customer exits :

  1.  A user exit is considered a modification, since technically objects in SAP namespace are modified. Customer exits do not affect standard SAP source code.
  2. User Exits are forms (in the SAP Standard Includes) and are called by SAP standard programs using perform. Customer Exits are functions so they are called using call function (or more exactly Call customer function). 
  3. Inside the form (user exit) you can read and change almost any global data from host program. Inside a function (customer exit) you can only access your import/export/changing/tables parameters. 
  4. User exits are more flexible because you have more information to use in your code but on the other hand, it is very easy to manipulate erroneously global data and lead the standard program to a dump or even to make database inconsistent. Customer exits are more restrictive but you are sure any change you can make to any parameters will never lead to inconsistency. 
  5. User-exit doesn't have any classification. In customer-exit we have function-module exit, screen exit, menu exit. 
  6. User exits are basically designed For SD module. Customer exits are available for MM, SD, FI, HR... basically designed for all modules. 
  7. While changing User-exit, Access Key is required, whereas in Customer-exit no access key is needed.
  8. User exits are flexible and inconsistent and customer exits are restrictive and consistent. 
  9. User-Exit affects modifies the standard code and this change will be effected every time. Whereas Customer-Exit will be included (or called) into the main program only when the corresponding Project is active. Thus preserving us lot of resources time.

Sunday, March 7, 2010

Different Types of Internal Tables in SAP


Internal table: Allocating extra memory for a structure is called as Internal table. An internal table will have a header area and a body area. The Header area can store only one record and the body area can store more no. of records depending upon the size of the table defined during declaration.

Types of Internal tables: 
STRUCTURED INTERNAL TABLES:
Syntax for Internal table based on LIKE option.
DATA <ITAB> LIKE <DB TABLE> OCCURS <SIZE> [WITH HEADER LINE]
  • Any variable defined for a table in ABAP using LIKE statement is by default a structure. It can hold only one record.
  • If OCCURS <size> specification is added, SAP allocates Body area memory with out header line.
  • If <size> is specified as '0', it adds 8 KB of data to the body area and if any other integer is given, memory equal to those no. of records is added.
  • The memory size is not fixed with the size specification. It increases in the same width as specified in the OCCURS <size> specification.
  • For defining Internal table with header line, we need to use the option WITH HEADER LINE.
     
NOTE: Internal table without header line is required in LOV concept and ALV reports.
  • Internal tables with header line is used all SAP applications to program DML operations. These concepts are used in MPP and BDC also.
  • If we need to populate data into internal table without header line, we need to define an explicit structure and populate data into that structure and append data from that into the body area of the internal table using APPEND or COLLECT statements.
  • There are two statements for loading data into internal tables.
           1) APPEND        2) COLLECT
  • Append statement is used to add header data always a new record. Whereas Collect statement verifies whether the current header data is available in the body area. If found, Integer fields are added up with the current header data value. Else, adds a new record. If there are no internal fields, no records are added.
  • For displaying Internal Table data we can use the Data Base loops.
        1) Select - End Select 2) Loop - End Loop.
  • Select - End select statement is used to fetch data from DB tables into Application Server memory. Memory created by ABAPer in form of structure or internal tables is called as Explicit memory.
  • Loop - End loop is used to display data in internal tables.
  • Example for using Loop - End loop and Append statements:
        Data WA like KNA1 occurs 0 with header line.
        Wa–kunnr = 'customer 1'.
        Wa–land1 = 'in'.
        Wa-name1 = 'Indian'.
        Append wa.
        Wa–kunnr = 'customer 2'.       Wa–land1 = 'in'.       Wa-name1 = 'indian2'.       Append wa.            Loop at wa.         Write: / wa-kunnr, wa-land1, wa-name1.        Endloop. 
  • In the above example, if the internal table doesn't have the WITH HEADER LINE option, it would have generated an error.
  • For such internal tables, we need populate using an work area also display using an work area.
  • In the above example if header line is not specified, define another wa (wa2) of the same type, add the values into that wa2 instead of wa. and in place of append statement, use "Append WA2 to WA".
  • For displaying, in Loop statement, use "Loop at wa into wa2". Then in Write statement, use the values in wa2.
  • When we give Select - End Select statement, the data is retrieved from the data base table record by record. For displaying all the records in one hit to the data base, use the option INTO TABLE <INT_TAB> in the SELECT statement. In this case, there is no need of any ENDSELECT.
  • By using the above method, the performance of the program increases.
NOTE:Begin of  - End of  is best useful in Reprots. Like Syntaxes are best useful in DML Operation.
EXIT:This statement is used for coming out of the loop with out checking for any condition. SORT: This statement is used to sort data of the internal table based on the field selected for sorting in the manner required.
        Syntax: SORT <INTAB> [BY <FIELD[S]>]    [ASCENDING / DESCENDING]
  • If sorting is not specified, the sorting is done in Ascending order based on Key field.
  • If no sorting mode is specified, then also sorting is done in the Ascending order based on the fields selected. 
Different type of internal table definitions using BEGIN OF … END OF:
First syntax for declaring internal tables.
Data:begin of itab occurs 0,
             mandt like kna1-mandt,
             kunnr like kna1-kunnr,
             .
             .
         end of itab.
 
Second syntax for declaring internal tables.
Data begin of <itab> occurs 0.
           include structure <dbtab>.
data end of <itab>. 
This syntax is used to add the same structure of data base table into current internal table or workarea with help of include statement.This syntax is used to insert or modify the data of data base table based on internal table values.
 
3rd Syntax a of Internal table using like and occurs options.
data <var> like <db_tab>.
ex: data wa like kna1.
wa is called as structure, number of fields in wa is same as kna1 field fields. wa can hold only one record memory.
 
3RD SYNTAX b
    DATA : ITAB LIKE <DBTAB> [OCCURS <N>] [WITH HEADER LINE].
OCCURS <N> IS THE OPTION WHICH MAKES THE VARIABLE AS INTERNAL TABLE, BY DEFAULT THIS STATEMENT CAN ALLOCATE MEMORY FOR ONLY BODY AREA. IF ABAPER HAS TO GET BACK HEADER LINE FOR THE INTERNAL TABLE USE "WITH HEADER LINE" OPTION. IF A INTERNAL TABLE IS DECLARED WITHOUT HEADER LINE, THEN HOW TO POPULATE DATA, AFTER POPULATING HOW TO SEE THE DATA..?
IF THE INTERNAL TABLE IS NOT DECLARED WITH HEADER LINE , THEN DECLARE ANOTHER STRUCTURE FOR WITH SAME FIELDS, USING "APPEND <WA> TO <ITAB>" SYNTAX POPULATE ONE ONE RECORD INTO INTERNAL TABLE BODY AREA.
NOTE:
LOOP AT ITAB IS THE SYNTAX TO DISPLAY THE CONTENTS OF INTENRAL TABLE. BUT THIS STATEMENT CAN ALLWAYS CHECKS FOR THE INTERNAL TABLE HAS HEADER LINE, IF ITAB HAS NO HEADER LINE THEN ABAPER CAN USE "INTO" OPTION TO REDIRECT THE DATA TO DESTINATION WORKAREA.

EXAMPLE PROGRAM.
DATA ITAB LIKE MARA OCCURS 0."HAS NO HEADER
DATA WA LIKE MARA. "HAS HEADER LINE.

WA-MATNR = '1780'.
WA-MBRSH = 'P'.
WA-MTART = 'FERT'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
WA-MATNR = '1781'.
WA-MBRSH = 'P'.
WA-MTART = 'COUP'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
WA-MATNR = '1782'.
WA-MBRSH = 'M'.
WA-MTART = 'BEVR'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
WA-MATNR = '1783'.
WA-MBRSH = 'C'.
WA-MTART = 'DRIN'.
WA-MEINS = 'KG'.
APPEND WA TO ITAB.
LOOP AT ITAB INTO WA.
  WRITE :/ WA-MATNR , WA-MTART, WA-MBRSH, WA-MEINS.
ENDLOOP.

EXAMPLE2.
DATA ITAB LIKE MARA OCCURS 0 WITH HEADER LINE.
ITAB-MATNR = '1780'.
ITAB-MBRSH = 'P'.
ITAB-MTART = 'FERT'.
ITAB-MEINS = 'KG'.
APPEND ITAB.
ITAB-MATNR = '1781'.
ITAB-MBRSH = 'P'.
ITAB-MTART = 'COUP'.
ITAB-MEINS = 'KG'.
APPEND ITAB.

LOOP AT ITAB.
   WRITE : ITAB-MATNR , ITAB-MBRSH , ITAB-MEINS , ITAB-NTART.
ENDLOOP.

SORTING THE DATA OF INTERNAL TABLE.

DATA : ITAB LIKE KNA1 OCCURS 0 WITH HEADER LINE.
SELECT * FROM KNA1 INTO TABLE ITAB.
SORT ITAB DESCENDING.
LOOP AT ITAB.
  WRITE : / ITAB-KUNNR , ITAB-LAND1 , ITAB-NAME1.
ENDLOOP.

SORTING IS PERFORMED BASED ON ASCCI CHARACTERS

Notes:
data itab like kna1 occurs 0 with header line. itab key field and dababase key fields are same, is not possible to change the key fields.

Syntax 4.
if any case if the data base key field and internal table key field should be changed for report generations then use "standard" internal table.
data : itab like standard table of <dbtab>
            [with unique/non-uniqe key <fld>]
                [with header line]
                     [initial size <n>].
example :data: itab like standard table of kna1 with non-unique key name1 with header line initial size 0. 
select * FROM KNA1 INTO TABLE ITAB.
SORT ITAB.
LOOP AT ITAB.
  WRITE : / ITAB-KUNNR , ITAB-LAND1 , ITAB-NAME1.
ENDLOOP.

STANDARD: This internal table is used in ABAP to change Key field priority in the Internal table. Standard tables have a linear index. You can access them using either the index or the key. If you use the key, the response time is in linear relationship to the number of table entries. The key of a standard table is always non-unique, and you may not include any specification for the uniqueness in the table definition. This table type is particularly appropriate if you want to address individual table entries using the index. This is the quickest way to access table entries. To fill a standard table, append lines using the (APPEND) statement. You should read, modify and delete lines by referring to the index (INDEX option with the relevant ABAP command). The response time for accessing a standard table is in linear relation to the number of table entries. If you need to use key access, standard tables are appropriate if you can fill and process the table in separate steps.
Syntax:
data : <itab> like standard table of <db_table>
with <default/non-unique> key <fld>
with header line
initial size <n>.
example:
data : itab like standard table of kna1
with NON-UNIQUE key name1
with header line
initial size 0.
select * from kna1 into table itab.
sort itab.
loop at itab.
write : / itab-kunnr , itab-land1 , itab-name1.
endloop.


SORTED: Data is sorted into these internal tables at the time of Data population itself. These internal tables can't be resorted by ABAPer using "SORT" statement. They also have a linear key, and, like standard tables, you can access them using either the table index or the key. When you use the key, the response time is in logarithmic relationship to the number of table entries, since the system uses a binary search. The key of a sorted table can be either unique, or non-unique, and you must specify either UNIQUE or NON-UNIQUE in the table definition. Standard tables and sorted tables both belong to the generic group index tables. This table type is particularly suitable if you want the table to be sorted while you are still adding entries to it.
syntax:
data : <itab> like sorted table of <db_table>
with <default/non-unique> key <fld>
with header line
initial size <n>.
example code.
data : itab like sorted table of kna1
with non-unique key land1
with header line
initial size 0.
select * from kna1 into table itab.
loop at itab.
write : / itab-kunnr , itab-land1 , itab-name1.
endloop.

 
Note: For all Above Internal tables Indexing is generated automatically by sap as Row ID. Based on This index number abaper can read the requied data from Internal table body area using "read" statement.
READ STATEMENT SYNTAX.
READ TABLE <ITAB> INDEX <NUM>.
This statement for Internal tables reads Data of Internal table for Specified Index Number and Populates the data into Header area of the Internal table.
example code.
DATA : BEGIN OF ITAB OCCURS 1,
F1(10),
F2(20),
END OF ITAB.
PARAMETERS : NUM TYPE I DEFAULT 1.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'AP'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'TN'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'MH'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'UP'.
APPEND ITAB.
ITAB-F1 = 'INDIA'.
ITAB-F2 = 'KA'.
APPEND ITAB.
READ TABLE ITAB INDEX NUM.
WRITE : ITAB-F1 COLOR 5 , ITAB-F2 COLOR 7.


Note: IF THE INDEX NUMBER IS SPECIFIED MORE THAN SIZE OF INTERNAL TABLE, THEN READ STATEMENT GETS DATA INTO HEADER OF THE LAST RECORD IN THE INTERNAL TABLE.
HASHED: These internal tables are called as Non index internal tables. As this internal body is created in Hashed format, indexing can't be generated for the records. You can only access hashed tables by specifying the key. The response time is constant, regardless of the number of table entries, since the search uses a hash algorithm. The key of a hashed table must be unique, and you must specify UNIQUE in the table definition. This table type is particularly suitable if you want mainly to use key access for table entries. Hashed tables are a useful way of constructing and using internal tables that are similar to database tables.
Syntax:
DATA : ITAB LIKE HASHED TABLE OF <DB_TAB>
WITH UNIQUE KEY <FLD_NAME>
WITH HEADER LINE
INITIAL SIZE <N>.

 
EXAMPLE:
DATA : ITAB LIKE HASHED TABLE OF VBAK
WITH UNIQUE KEY VBELN
WITH HEADER LINE
INITIAL SIZE 0.
SELECT * FROM VBAK INTO TABLE ITAB.
LOOP AT ITAB.
WRITE : / 'VBELN' , ITAB-VBELN.
WRITE : / 'ERDAT' , ITAB-ERDAT.
WRITE : / 'ERNAM' , ITAB-ERNAM.
ULINE.
ENDLOOP.

Tuesday, March 2, 2010

SELECT Statements in SAP ABAP


There are different types of Select Statements in ABAP. Below are some select statements for frequently used cases.
<FLD_LIST> -> List of Fields from Data Base table for selection
<DB_TABLE> -> Source Data Base Table
<WA> -> Target Work Area/Structure
<ITAB> -> Target Internal Table
<CONDITION> -> Where condition in the select statement based on DB Table Fields.



  1. SELECT *|<FLD_LIST>                             (We can select all fields or the fields in given order of the internal table)
    FROM <DB_TABLE>
        INTO <WA>
            WHERE <CONDITION>.
        ENDSELECT.
    Ex: SELECT * FROM KNA1
        INTO W_KNA1
WHERE KUNNR GT '0000100000'. (Where W_KNA1 is a STRUCTURE similar to KNA1)
        WRITE: / W_KNA1-KUNNR, W_KNA1-NAME1.
ENDSELECT.

PS: This is a loop on data base table. This Select-EndSelect loop will retrieve records one after the other from data base and places it in the structure/Work Area. Programmer can use the structure data for their programming purpose within the Select – EndSelect Statements.


     2.  SELECT *|<FLD_LIST>
    FROM <DB_TABLE>
        INTO TABLE <ITAB>
            WHERE <CONDITION>.

        Ex: SELECT * FROM KNA1
             INTO TABLE IT_KNA1
       WHERE KUNNR GT '0000100000'. (Where IT_KNA1 is an internal table with structure similar to KNA1)
              LOOP AT IT_KNA1.
                  WRITE: / IT_KNA1-KUNNR, IT_KNA1-NAME1.
              ENDLOOP.
            
PS: When INTO TABLE is used, it will no more be a Loop. ENDSELECT is not needed here. This is a single statement to get all the required records from the Data Base table into our internal table in a single step, which will be very faster as compared with earlier process as well as reduces number of hits to the DB and thus optimizing our programs performance.
Also, here we will have all the records for processing at a later stage in our internal table.


     3.  SELECT SINGLE *|<FLD_LIST>    (We can select all fields or the fields 
                                                                                in given order of the internal table)
     FROM <DB_TABLE>
          INTO <WA>
              WHERE <CONDITION>.

          Ex: SELECT SINGLE * FROM KNA1
                     INTO W_KNA1
          WHERE KUNNR GT '0000100000'. (Where W_KNA1 is a 
                                                                   STRUCTURE similar to KNA1)

PS: In this case also we don't need ENDSELECT. This is a statement which will bring the first selected record from the Data Base Table and place it in our structure/WA. This will be useful if we need only one record from the DB table thus not looping over the DB table number of times.

NOTE: One important thing to remember for all the above cases (when using the <FLD_LIST> in the Select statement) is that, the order of the fields in the <FLD_LIST> should be same as the order of the fields defined in the Work Area/Internal Table.
System will try to place the selected fields of each record in the order (i.e., first field into first field etc.). So, if the fields defined in the internal table and fields selected in Select statement are not in same order, there will be errors in the data selected (sometimes running into Run-Time errors when there is a data type conflict). The solution to this scenario is below.


          4. SELECT <FLD_LIST>
         FROM <DB_TABLE>
              INTO CORRESPONDING FIELDS OF < ITAB>
                  WHERE <CONDITION>.
By Using the INTO CORRESPONDING FIELDS OF addition, the select statement will try to identify the corresponding field of the Internal Table which has the same name as the Selected field from the Data Base table and updates it in that field. Thus even if the order of selection for the fields in the Select statement and in the Internal table do not match, the data is populated into the corresponding fields.
    The same functionality applies to all the three scenarios explained above.
    NOTE: One Limitation here however is that, the Names of the Fields in the Internal Table should be the same as the names of the Data Base Table. Or else, system cannot find any such field and so the fields will not be updated.
Below is a detailed description of the Generic Select Statement Syntax and its different parts.
Syntax:
SELECT <result>
INTO <target>
FROM <source>
[WHERE <condition>]
[GROUP BY <fields>]
[HAVING <cond>]
[ORDER BY <fields>].

The SELECT statement consists of a series of clauses, each of which fulfils a certain task:
SELECT clause: Defines the structure of the selection.

Syntax
SELECT [SINGLE]|[DISTINCT]
* | <si>...
<agg>( [DISTINCT] <sj>) [AS <aj>]...
The selection can be a single line SINGLE or a series of lines. You can eliminate duplicate lines using the DISTINCT addition. To select the entire line, use *, otherwise, you can specify individual columns <si>. For individual columns, you can use aggregate functions <agg>, and assign alternative column names <ai>.

 INTO clause: Defines the target area into which the selection from the SELECT clause is to be placed.
Syntax
... INTO [CORRESPONDING FIELDS OF] <wa>
| INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE <itab>
[PACKAGE SIZE <n>]
| INTO (<f1>, <f2>,...)
The target area can be a flat work area <wa>, an internal table <itab>, or a list of fields <fi>. If you use the CORRESPONDING FIELDS addition, data is only selected if there is an identicallynamed field in the target area. If you use APPENDING instead of INTO, the data is appended to an internal table instead of overwriting the existing contents. PACKAGE SIZE allows you to overwrite or extend the internal table in a series of packages. The data type of the target area must be appropriate for the selection in the SELECT clause.

 FROM clause: Specifies the database tables from which the data in the selection in the SELECT clause is to be read.
Syntax
... FROM [<tab> [INNER]|LEFT [OUTER] JOIN] <dbtab> [AS <alias>]
[ON <cond>]
[CLIENT SPECIFIED]
[BYPASSING BUFFER]
[UP TO <n> ROWS]
You can read a single table <dbtab> or more than one table, using inner and outer joins to link tables with conditions <cond>, where <tab> is a single table or itself a join condition. You can specify individual database tables either statically or dynamically, and you can replace their names with an alternative <alias>. You can bypass automatic client handling with the CLIENT SPECIFIED addition, and SAP buffering with BYPASSING BUFFER. You can also restrict the number of lines read from the table using the UP TO <n> ROWS addition.

WHERE clause: Restricts the number of lines selected.
Syntax
... [FOR ALL ENTRIES IN <itab>] WHERE <cond>
The condition <cond> may contain one or more comparisons, tests for belonging to intervals, value list checks, subqueries, selection table queries or null value checks, all linked with AND, OR, and NOT. If you use the FOR ALL ENTRIES addition, the condition <cond> is checked for each line of the internal table <itab> as long as <cond> contains a field of the internal table as an operand. For each line of the internal table, the lines from the database table meeting the condition are selected. The result set is the union of the individual selections resulting from each line.

 GROUP BY clause: Groups lines in the selection
Syntax
... GROUP BY <s1> <s2>
Groups lines with the same contents in the specified columns. Uses aggregate functions for all other columns in each group. All columns listed in the SELECT clause that do not appear in the GROUP BY addition must be specified in aggregate expressions.

 HAVING clause: Restricts the number of line groups.
Syntax
... HAVING <cond>
Like the WHERE clause, but can only be used in conjunction with a GROUP BY clause. Applies conditions to aggregate expressions to reduce the number of groups selected.

 ORDER BY clause: Sorts the lines in the selection.
Syntax
... ORDER BY PRIMARY KEY |... <si> [ASCENDING|DESCENDING]...
Sorts the selection in ascending or descending order according to the primary key or the contents of the fields listed.

Sunday, February 28, 2010

What is the Value of 0/0 in SAP


What is the Value of 0/0?
If the same question is asked in a Mathematics class, what will the answer?
Here are some possible answers that I got from some of the students.
  1. Anything divided by zero is Infinity. So, first answer -> INFINITY
  2. If numerator of a ratio is zero (or we can say if Zero multiplied by anything –even a ratio) then the answer is ZERO.

  3. Now read this interesting answer –> 0/0 = 1.
    Didn't understood how, considering algebraic mathematics… I will convert the expression as X / X which will be equal to ONE.
    There is no end to this. Even some will say X/Y where X = 2Y resulting in TWO. There is no end to these somehow "Innovative" answers.
But the answer to all these arguments is the actual fact as per Mathematics Principles is that, 0/0 is UNDEFINED.

Now let us see what the value of 0/0 as per SAP. Write a small code as below:

DATA: VAR1 TYPE I,
            VAR2 TYPE I,
            VAR3 TYPE I.

    VAR3 = VAR1 / VAR2.
    WRITE: / VAR1, VAR2, VAR3.

You can see that there won't be any Run Time error and instead the answer will be ZERO.Now before the statement     VAR3 = VAR1 / VAR2, set VAR1 to a non-zero value. We can see that it will generate a Run Time Error of "Divide by Zero".

Monday, February 22, 2010

Maintenance View


Maintenance views: It can be created for One or More tables, if more, only related tables. This view is created will all key fields of table automatically and joins are auto generated by SAP. We cannot change it. This Object can't be used as Dictionary Object for SE38 programs, with help table maintenance generator we should convert maintenance view as repository object. Unlike Database views, we can modify data from multiple tables also.

Navigations to create maintenance view.
1. SE11 -> SELECT OBJEC TYPE AS "VIEW" -> NAME THE VIEW ( ZMAINVIEW1 ) -> CLICK ON CREATE -> SELECT VIEW TYPE AS "MAINTENANCE VIEW" -> CLICK ON COPY -> ENTER SHORT DESCRIPTION FOR THE VIEW ( ANY ) -> SPECIFY PRIMARY TABLE ( MARA ) -> PRESS ENTER -> CLICK ON RELATIONSHIPS TO ADD OTHER TABLES FOR THE VIEW -> SELECT ONLY DEPENDENT TABLES ( MAKT ) -> CLICK ON COPY , BY DEFAULT SAP GENERATES A JOIN FOR THE TABLES , AND ADDS ALL KEY FIELDS FROM THE TABLES -> CLICK ON VIEW FIELDS TAB BUTTON TO SELECT REQUIRED OPTIONAL FIELDS -> CLICK ON TABLE FIELDS , TO SELECT REQUIRED FIELDS FROM BOTH TABLES -> CLICK ON MAINTENANCE STATUS -> SET THE ACCESS AS "READ/CHANGE/DELETE AND INSERT" ( DEFAULT SELECTED ) -> SAVE THE VIEW UNDER A PACKAGE -> ACTIVATE THE VIEW.
2. To generate Repository Object, Generate Table maintenance using
utilities menu -> table maintenance generator -> opens an interface -> Specify Authorization Group as "ATES" -> Name the Function Group to be created ( ZFGMAINVIEW1 ) -> select maintenance type as "one step" screen -> set the screen number as "123" -> click on create from application toolbar -> save the objects under a package -> SAP Generates Function Group with Code for maintenance view.
3. Test maintenance view code execution.
SE54 (TCODE ) -> Name the VIEW (ZMAINVIEW1 ) -> click on "TEST" PUSHBUTTON -> Opens another interface -> Click on Maintain Pushbutton -> Opens another screen with the contents of the view -> For creating new entries, click on "New Entries" from ATB. For changing existing contents, modify the values -> SAVE -> Changes will be updated in the DB tables.

Projection View

A view doesn't contain any data physically. It's only a logical way of grouping the required fields from one/more tables with required Joins and selection conditions. View gets records from respective database tables only in runtime and if any updates are done using the View, the data will be actually saved/modified in the database tables. If we try to create any new records using Views, all fields of the database tables which don't lie in the view are maintained either as Blank or Zero(depending on the data type).

Projection View can be created on One table. We can insert records into the actual database table using these Views. The main use of Projection View lies in giving the feasibility of projecting only the required fields out of the number of fields of the table, without all the fields literally in the program, making our programming easier. (for ex: create a Projection View with 30 fields out of 128 fields of VBAK table).

Navigations to create a Database view for.

SE11 -> Select "VIEW" -> Name the view ( ZPROJVIEW1 ) -> Click on Create -> Opens an interface , select view type as "Projection View" -> click on Continue -> Opens an interface -> Enter Description for the projection view ( any ) -> Name the table to be assigned for the View ( VBAK ) -> Press Enter -> Click on Table Fields to select the List of Fields from table ( here select all key fields and optional required fields ) -> select required fields and click on copy -> Click on Maintenance status tab button -> select Access as "read and change" -> Save -> Activate..


 

* use the view in SE38 to fetch data into Reports.

REPORT ZVIEWTEST2 LINE-SIZE 150 NO STANDARD PAGE HEADING.

DATA : ITAB LIKE ZPROJVIEW1 OCCURS 0 WITH HEADER LINE.

SELECT * FROM ZPROJVIEW1 INTO TABLE ITAB.

LOOP AT ITAB.

WRITE : / ITAB-VBELN , ITAB-KUNNR , ITAB-NETWR , ITAB-AUDAT , ITAB-VBTYP.

ENDLOOP.

Database View

A view doesn't contain any data physically. It's only a logical way of grouping the required fields from one/more tables with required Joins and selection conditions. View gets records from respective database tables only in runtime and if any updates are done using the View, the data will be actually saved/modified in the database tables. If we try to create any new records using Views, all fields of the database tables which don't lie in the view are maintained either as Blank or Zero(depending on the data type).

Database View can be created on One table as well as multiple tables. Only when it is created on top of one table, we can insert records into the actual database tables using the Views. When it's created with combination of multiple tables, we can only read the data from Database tables.

Navigations to create a Database view for.

se11 -> select Object type as "view" -> Name the view ( ZDBVIEW1 ) -> Click on Create -> Opens an interface with type of views , select "Data base view" -> click on continue -> opens another Interface -> Enter Description ( any ) -> name some table say, "SPFLI" -> press enter -> Click on Relationship pushbutton on screen to get The List of related tables ->select the required tables from the list (say SBOOK , SFLIGHT ) -> Click on Copy , internally SAP Generates Auto Joins Conditions -> Click on View Fields tab button -> Click on table fields to select required fields from tables -> go to "Selection Conditions" tab -> Give the selection conditions if required, with AND/OR for multiple selection -> save the view under a package , assign with request number ->Activate the view (ignore any warnings).


 

* use the view in SE38 to fetch data into Reports.

REPORT ZVIEWTEST1 LINE-SIZE 150 NO STANDARD PAGE HEADING.

DATA : ITAB LIKE ZDBVIEW1 OCCURS 0 WITH HEADER LINE.

SELECT * FROM ZDBVIEW1 INTO TABLE ITAB.

LOOP AT ITAB.

WRITE : / ITAB-CARRID , ITAB-CITYFROM , ITAB-CITYTO , ITAB-FLDATE , ITAB-PRICE.

ENDLOOP.


 

Saturday, February 20, 2010

Session Method


Session Method is one of the BDC methods which is best in Maintenance.
We use Three FM's in Session Method:
BDC_OPEN_GROUP for creating SESSION OBJECT.
BDC_INSERT to insert data of the internal table.
BDC_CLOSE_GROUP to close SESSION OBJECT MEMORY from the program.

Sample code:
DATA : BEGIN OF ITAB OCCURS 0,
                 STR(255) TYPE C,
              END OF ITAB.

DATA : JTAB LIKE BDCDATA OCCURS 0 WITH HEADER LINE,
             ITAB1 LIKE MARA OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'UPLOAD'
     EXPORTING
          FILENAME = 'D:\930AM\MATDATA.TXT'
          FILETYPE = 'ASC'
     TABLES
          DATA_TAB = ITAB.


LOOP AT ITAB.
      SPLIT ITAB-STR AT ',' INTO ITAB1-MATNR ITAB1-MTART ITAB1-MBRSH ITAB1-MEINS.
      APPEND ITAB1.
ENDLOOP.


CALL FUNCTION 'BDC_OPEN_GROUP'
     EXPORTING
          CLIENT = SY-MANDT
          GROUP = 'SESS9301'
          KEEP = 'X'
          USER = SY-UNAME.

LOOP AT ITAB1.
     PERFORM PRGINFO USING 'SAPMZMPPSCR' '123'.
     PERFORM FLDINFO USING 'ITAB-MATNR' ITAB1-MATNR.
     PERFORM FLDINFO USING 'ITAB-MTART' ITAB1-MTART.
     PERFORM FLDINFO USING 'ITAB-MBRSH' ITAB1-MBRSH.
     PERFORM FLDINFO USING 'ITAB-MEINS' ITAB1-MEINS.

     CALL FUNCTION 'BDC_INSERT'
          EXPORTING
              TCODE = 'ZCT930'
          TABLES   
              DYNPROTAB = JTAB.
  ENDLOOP.

 CALL FUNCTION 'BDC_CLOSE_GROUP'.

FORM PRGINFO USING PROGRAMNAME SCRNUM.
     REFRESH JTAB.
     JTAB-PROGRAM = PROGRAMNAME.
     JTAB-DYNPRO = SCRNUM.
     JTAB-DYNBEGIN = 'X'.
     APPEND JTAB.
ENDFORM.


FORM FLDINFO USING FLDNAME FLDVAL.
     CLEAR JTAB.
     JTAB-FNAM = FLDNAME.
     JTAB-FVAL = FLDVAL.
     APPEND JTAB.
ENDFORM.

LSMW


LSMW IS NAVIGATION FORMAT TO PERFORM BDC'S
LSMW IS A COLLECTION OF PROJECTS, PROJECT IS A COLLECTION OF SUB PROJECTS AND SUB PROJECT IS A COLLECTION OF OBJECTS.
LSMW -> NAME THE PROJECT , SUB PROJECT AND OBJECT -> CLICK ON CREATE FROM APPLICATION TOOLBAR -> OPENS ANOTHER INTERFACE ACCEPT SHORT DESCRIPTION -> OK -> TO LSMW PROCESS CLICK ON EXECUTE ( PRESS F8).
1) Maintain Object Attributes
IN THIS STEP WE CAN SPECIFY THE TARGET TCODE, WHERE DATA TO BE POPULATED FOR LSMW, WITH TCODE FIELD IDENTITIES WITH HELP OF RECORDING METHOD.
SELECT -> EXECUTE -> CLICK ON RECORDING OVERVIEW ( PB ON APPLICATION ) -> OPENS LPS SCREEN -> CLICK ON CREATE NEW RECORDING FROM APPLICATION TOOLBAR -> OPENS AN INTERFACE -> ENTER RECORDING METHOD NAME ( ZREC1) -> SHORT TEXT -> CONTINUE -> ENTER THE TCODE( MK01 ) -> Enter sample data -> Save the entries from tcode -> Generates a report in LPS with recording method fields, but in this step abaper has to assign recording methods fields to LSMW process , to do this -> click on Default all from application toolbar, assigns all fields of recording method into LSMW process-> save the entries -> come back -> come back -> change mode of screen -> select batch input recording -> name the recording (zrec1) -> save the entries -> come back.
2) Maintain Source Structures
In this process we are creating a structure where can be splitted from flat file, to populate data into corresponding fields of screen select -> execute -> change the mode of screen -> click on create structure from application toolbar -> name the structure ( ktab) -> short text -> ok -> Save the entries and come back.

3) Maintain Source Fields
In this process we are adding Fields for the structure with help tcode fields.
select -> execute -> change mode -> select name of structure -> click on create field -> specify field name -> description -> size of the field -> data type -> continue -> repeat the same step for all fiels of tcode where data to be populated -> save entries -> come back.

4) Maintain Structure Relations
In this step the relation between structure and Recording object should be assigined, this will be done automatically, else process manually,
select -> execute -> chane the mode -> save -> come back ( as relations are maintained by sap by default [zrec <<<< ktab]).

5) Maintain Field Mapping and Conversion Rules
In this process we are performing field mappings of recording ( lsmw ) to structure fields. To do this
select -> execute -> change the mode -> select field of Recording method (LIFNR) -> click on source field from application toolbar -> displays list of fields in KTAB ( structure ) -> double click on related field name ( field is mapped ) -> Repeat the same step for all fields-> save the entries and come back.

6) Maintain Fixed Values, Translations, User-Defined Routines
This step is require if we need to assign any default values, is a optional step [ we can avoid this step ].

select -> execute -> come back.

7) Specify Files
we can specify the file is from Presentation layer or application server to be extracted for data population.
select -> execute -> change the mode -> select the location ( legacy system PC ) -> click on add entry from application toolbar -> opens an interface -> enter path of source file in File ( field ) [c: \LFA1.txt] -> description in name field -> specify field separator (comma) -> continue -> save the entries -> come back ->

8) Assign FilesIn this process we are assigning File to the LSMW process.
select -> execute -> change the mode -> save -> come back.

9) Import Data
in this process the data from legacy system is populated into LSMW process, generates ".lsmw.read" file
select -> execute -> execute -> generates report -> come back -> come back.

10) Display Imported Data
In this process we are display the data of .read file in presentation layer.
select -> execute -> continue -> display report ( yellow color ) -> come back ->

 11) Convert Datain this process generates ".conv" with help of ".read" file.
select -> execute -> execute -> generates a report -> come back -> come back ->

12) Display Converted Data
displays the converted data from application created by sap.
select -> execute -> continue -> generates the report -> come back.

 13) Create Batch Input Sessionin this process creates a session object with name as object name defined in LSMW.
select -> execute -> execute.

 14) Run Batch Input Sessionexecutes the SM35 tcode to process LSMW into sap r/3.
opens sm35 tcode -> select object name -> process.

Logical Data Base


LDB is a special Programming in ABAP to increase Performance of the Reports. It is a collection of nodes assigned with GUI and Select Statement. It is a Repository Object.
Before learning about creation of LDB, let us know about GET and PUT statements.

PUT : It is the statement used in source code of LDB in between SELECT-ENDSELECT to add selected data information in the Node.

GET: We can invoke the node using "GET" statement. It gets the data from NODE into the Presentation layer.

NOTE: Do not perform DML operations as it effects performance of Server. Inconsistent code in LDB causes lot of delay for all applications in SAP Data Base and even Application Server.

Navigations to create Logical Data Base:
*GUI Creation.
SLDB -> name the Logical Database( zmyldb_1) -> create -> Short text -> save -> Specify name of Root Node ( KNA1) -> short text -> Data base table ( KNA1) -> continue-> opens Logical Data base Editor -> Select the node -> click on Selections from Application toolbar -> opens a dialog box -> click on yes -> prompts another dialog box to create search helps ( no ) -> select all checkboxs prompted in LPS [ is used to generate GUI for Dynamic Selections from the table and Dynamic Field Selects from the table -> Click on transfer -> opens SE38 code -> code is generate by SAP -> Replace ? with Suitable name , remove comments for GUI statement if required -> save -> activate the source code -> come back.
(Remember that Node name and table name must be same.)
Adding Select Statement for the Node:
select the node from LDB editor -> click on Source Code From application toolbar -> opens ABAP editor with two include files as "Top Include file " AND "nxxx" FILE . Double click on top include file -> declare internal table. Double click on file -> opens another ABAP editor -> Double Click in "Node" code include statement -> activate the select statement as follows .
SELECT * INTO CORRESPONDING FIELDS OF ITAB
FROM MARA WHERE MATNR IN MATNUM.
PUT MARA.
APPEND ITAB.
ENDSELECT.
Don't forget to use PUT Statement between Select - Endselect , as it adds all data information in the Node -> save and Activate the code.
Once If the Logical Data Base is created with Nodes can be invoked from the program using SE38 -> name the program -> create -> short text -> type of program -> Logical Database (zmyldb_1) -> save -> opens ABAP editor -> use the code as follows.
NODES : MARA.
GET MARA.

Friday, February 19, 2010

Selection-Screen Statements in SAP ABAP


--> There are 11 statements used in ABAP under this category.
    1) PARAMETERS.
    2) SELECTION-SCREEN COMMENT.
    3) SELECTION-SCREEN BEGIN OF LINE –
        END OF LINE.
    4) SELECTION-SCREEN SKIP.
    5) SELECTION-SCREEN ULINE.
    6) SELECTION-SCREEN PUSH BUTTON.
    7) SELECTION-SCREEN BEGIN OF BLOCK –
        END OF BLOCK.
    8) SELECTION-SCREEN BEGIN OF SCREEN –
        END OF SCREEN.
    9) SELECTION-SCREEN FUNCTION KEY.
  10) SELECT-OPTIONS.
  11) SELECTION-SCREEN POSITION.

1) PARAMETERS:
    --> Used to create I/O fields, check boxes and radio buttons in ABAP.
syntax:
    PARAMETERS <VAR_NAME>
[<TYPE/LIKE> <DATATYPE/EXISTING VARIABLE>]
[DEFAULT <INITIAL_VALUES>] [LOWER CASE] [OBLIGATORY]
[MATCHCODE_OBJECT <SHLD>].

By default, name of the variable itself is displayed as label on GUI. To change label for GUI, activate the code in SE38
  • click on 'GOTO' menu
  • Text Elements
  • Selection-text
  • Opens an interface with list of GUI components in program, set the required text to be displayed on GUI as label
 à save
    à activate
    à comeback.

b) Syntax for generating checkboxes using parameters statement.:
    syntax: PARAMETERS <VAR_NAME> AS CHECKBOX. 
  • Whenever a field is defined for checkbox, by default, variable is assigned to type 'C' data type.

  • If the checkbox is selected, value of the variable will be 'X'. Else, space.

       c) Syntax for generating radio buttons using parameters statement:

    syntax: PARAMETERS <VAR_NAME> RADIO BUTTON
                    GROUP <GROUP_NAME>.

 à Radio buttons are defined under one group category.
  • Under one group at least two components must be added.
  • Only one of the radio button in a group can be selected.  
2) SELECTION-SCREEN COMMENT statement:

Syntax: SELECTION-SCREEN COMMENT [/]<POS>(width)
        <Label variable>
  • It is a statement used in ABAP to add a label on selection-screen GUI. 
Ex:     Parameters : A type I default 300.
          selection-screen comment /20(15) LB1.
          write: A.
          INITIALIZATION.    "-> Event

             A = 900.
             LB1 = 'A value'.

INITIALIZATION EVENT:
  • It is an event in ABAP used to set default values for all GUI variables in SAP scrn.
  • This is executed before loading the screen.
à The need of this event is that there are some GUI variables like 'LABEL', 'PUSH BUTTONS', 'FUNTIONS KEYS', 'BLOCK NAMES', 'SCREEN TITLES' etc which cannot be initialized at the time of declaration using DEFAULT addition.
  • For initializing such GUI variables, we need INITIALIZATION EVENT.

  • Before loading the screen, initialization event is executed and even before it, declaration part is executed.  
FAQ: What is the need of initialization event when there is default addition in SAP?
Ans: (Explained above).

 3) Selection-screen begin of line – end of line:
    à Any input field created in SAP is created in new line. For displaying more than one input field in same row, the statements must be specified between Selection-screen Begin of line = = = = = Selection-screen End of line.
    à But here, the labels of the input fields will not be displayed. For displaying them we need to add manually using Selection-screen comments stmt.

Ex:       Selection-screen begin of line.
            Selection-screen comment 1(10) LB1.
Parameters: Name(20).
Selection-screen comment 40(10) LB2.
Parameters: No.(10).
Selection-screen end of line.

        Initialization.    "-> Event
           LB1 = 'Name'. LB2 = 'No:'.

4) Selection-screen skip [N]:

    à Used for generating Blank lines on GUI for selection screen.
    à N specifies the no. of lines to be skipped off.

5) Selection-screen ULINE:
    à This is a statement used for generating Horizontal lines on GUI.
    Syntax: SELECTION-SCREEN ULINE [/] [<POS>] [(WIDTH)].  

6) Selection-screen Push Button:
    à This is a statement used to add Push Buttons on GUI.
    Syntax: Selection-screen pushbutton [/] [<POS>] [(WIDTH)] <LB1> User-command <PB1>.
        where <LB1> is the variable on Pushbutton and <PB1> is the name of it.


Ex:     Parameters: Kunnr like kna1-kunnr,
                            Land1 like kna1-land1,
                            Name1 like kna1-name1,
                            Ort01 like Kna1-ort01.
Selection-screen Pushbutton /10(10) LB1 User command PB1.
Selection-screen Pushbutton 30(10) LB2 User command PB2.

Initialization.    "Event
    LB1 = 'Abap Editor'.
    LB2 = 'Abap Dictionary'.
At Selection screen.    "Event
Case sy-ucomm.
    When PB1.
        Call Transaction 'se38'.
    When PB2.
        Call Transaction 'se11'.
End Case.  
7) Selection-screen Begin of Block ===== End of Block:

à This is a statement used to create Block in the selection-screen, which will be easier for the users to understand the related/grouped Selection screen fields.
Syntax:     SELECTION-SCREEN BEGIN OF BLOCK <B> WITH FRAME TITLE <T>.
     SELECTION-SCREEN END OF BLOCK <B>.
8) Selection-screen Begin of Screen ===== End of Screen:

à This is a statement used to create our own screens and can be understood in conjuction with Screen Painter. (Explained in Screen Painter/MPP concepts)
9) Selection-screen Function key:
    à This is a stmt used to create application tool bar components in selection screen.
    Syntax: SELECTION-SCREEN FUNCTION KEY <Num>.
        where Num is ranging from 1 to 5.

SSCR FIELDS: It is a structure in SAP for selection screens. Only 5 application tool bar components can be crated in a screen.
    à Whenever application tool bar components are added using above stmt, for these components labels are defined under a structure called " SSCRFIELDS" as FC01, FC02, FC03, FC04 and FC05 related to respective function key number.  
 à For this structure we need to allocate implicit memory using "TABLES" statement.
    Ex:     TABLES SSCRFIELDS.
              PARAMETERS: ID(10).
              SELECTION-SCREEN FUNCTION KEY 1.
  SELECTION-SCREEN FUNCTION KEY 2.
  SELECTION-SCREEN FUNCTION KEY 3.
             
              INITIALIZATION:                  
                   SSCRFIELDS-FUNCTXT_01 = 'FIRST ATC'.
                   SSCRFIELDS-FUNCTXT_02 = 'SECOND ATC'.
                   SSCRFIELDS-FUNCTXT_03 = 'THIRD ATC'.

AT SELECTION-SCREEN.     CASE SY-UCOMM.
         WHEN 'FC01'.
            MESSAGE I000(ZMSG_11AM).
         WHEN 'FC02'.
            MESSAGE I001(ZMSG_11AM).
         WHEN 'FC03'.
            LEAVE PROGRAM.
         ENDCASE.
10) SELECT-OPTIONS:
    à This statement is used for creating range of input fields for selection screen. Using this option, we can fetch required selected data from the DB table.
        Syntax: SELECT-OPTION <Var> FOR <Existing variable>.
            <Var> becomes a GUI variable for existing variable.   
        Ex:     DATA CNO(5).
                 SELECT-OPTIONS CNUM FOR CNO.

    à Internally, when SELECT-OPTIONS is used, a structure is created as below:
DATA:  BEGIN OF CNUM,
                SIGN(1),
                OPTION(2),
                LOW(5),
                HIGH(5),
            END OF CNUM.
This is predefined structure for select-options.  
à By default the content of sign is 'I' and option will be BW (between) when range is selected or EQ (Equal to) when only LOW value is given. It means IN BETWEEN. i.e., fields in between the high and low values specified are selected or IS EQUAL to the value in LOW.
à If we set SIGN as 'E' (Exclusive), data other than the range/Value will be selected.

11) SELECTION-SCREEN POSITION:
    àThis is the statement used to control position of GUI fields at lower position or at higher position.                          
 à This must be used only between SELECTION-SCREEN BEGIN OF LINE ==== END OF LINE.   

Syntax: SELECTION-SCREEN POSITION <Pos.>.
        Where <Pos> is POS_LOW or POS_HIGH.
Ex:     DATA:    KUNNR LIKE KNA1-KUNNR,
          SELECT-OPTIONS CNO FOR KUNNR.
          SELECTION-SCREEN BEGIN OF LINE.
              SELECTION-SCREEN COMMENT 1(20) LB1.
              SELECTION-SCREEN POSITION POS_HIGH.
              PARAMETERS A TYPE I.
          SELECTION-SCREEN END OF LINE.  
  
         INITIALIZATION.
             LB1 = 'Customer name'.