Wednesday, 26 December 2012

Constructor in Classes

Usage of Constructors in Classes

There are 2 types of constructors in an ABAP CLASS.
  1.  CONSTRUCTOR: The constructor is a method which is used to initialize the class members or assign value to them. It is called once for each instance of an class or more precisely it is called for each object of a class.
  2. CLASS CONSTRUCTOR: The class constructor is called once for each class irrespective of the number of objects created or instantiated for that class.

Let us look into this concept with the help of an example:

EXAMPLE:

REPORT z_constructor.

*--Class Definition
CLASS l_test DEFINITION.
     PUBLIC SECTION.
         METHODS:  constructor.
         CLASS-METHODS: class_constructor.
ENDCLASS.

*--Class Implementation.
CLASS l_test IMPLEMENTATION.
     METHOD constructor.
        WRITE : / ' inside constructor '.
     ENDMETHOD.
     METHOD class_constructor.
        WRITE : / 'inside class constructor'.
     ENDMETHOD.
ENDCLASS.

*--Report.
*--Declare the reference variables.
DATA  : ob1 TYPE REF TO  l_test,
              ob2 TYPE REF TO  l_test.

START-OF-SELECTION.
CREATE OBJECT ob1.
CREATE OBJECT ob2.

------------------------------------------------------------------------------------------------------------
OUTPUT:

inside class constructor                          <-- from class constructor

inside constructor                                  <-- from constructor for object ob1

inside constructor                                  <-- from constructor for object ob2
------------------------------------------------------------------------------------------------------------

Thursday, 13 December 2012

Declaring Local Class in ABAP Report

In this tutorial we will learn about how to declare local class and use it in a report program.

KEY POINTS:
  • The first step is to give a class definition.
  • Next implement the class and its methods.
  • Declare a reference to the class.
  • Create an object of the class and use it to call the methods of the class.
EXAMPLE:
REPORT  zlocal_class.
*--Class Definition
CLASS l_test DEFINITION.
   PUBLIC SECTION.
*--data declaration
     DATA: info TYPE string.
*--methods
     METHODS:
     constructor     IMPORTING p_balance TYPE dmbtr,
     set_balance    IMPORTNG  p_set TYPE dmbtr,
                           EXPORTING p_amount TYPE dmbtr,
     display.
  PRIVATE SECTION.
    DATA : balance TYPE dmbtr.
ENDCLASS.      " end of class definition
*--Class Implementation
 CLASS l_test IMPLEMENTATION.
    METHOD constructor.
      WRITE: / 'inside constructor'.
       balance  = p_balance.
      WRITE: /  'value'  ,  balance.
    ENDMETHOD.
    METHOD set_balance.
       balance = p_set.
    ENDMETHOD.
   METHOD display.
      WRITE : / ' set the balance'.
      WRITE :  balance.
   ENDMETHOD.
ENDCLASS.

*--Report 
*-- Declare the reference.
 DATA:  test TYPE REF TO  l_test.

*declare a parameter on selection screen.
PARAMETERS: p_val TYPE dmbtr.

START-OF-SELECTION.
*--create object
CREATE OBJECT test 
    EXPORTING 
     p_balance =  40.                          <-- This will be passed to the constructor.

*-- call method set_balance.
CALL METHOD test->set_balance  <-- call the method to set balance.
    EXPORTING
      p_set = p_val.
*-- call method display.
CALL METHOD test->display.        <-- call method display.

  ----------------------------------------------------------------------------------------------------------

INPUT :
 P_VAL  = 500.

OUTPUT:
inside constructor
value 40

set the balance 500
-----------------------------------------------------------------------------------------------------------
Notes :  
  • The method constructor is called when the object is created using CREATE OBJECT statement.
  • The method set_balance s used to set the value or assign the value of parameter to the class attribute balance.
  • The attribute 'balance' is private and hence can only be accessed through class methods.
  • The method display will print the value of balance attribute.

Table Maintenance Generation Events

INTRODUCTION

Table maintenance generation events can help us to perform validations while creating new entries in a table or hiding the fields or assigning default values to table fields.
There are large number of events present which can be used for such purposes.Some of them are :


01  Before saving the data in the database
02  After saving the data in the database
03  Before deleting the data displayed
04  After deleting the data displayed
05  Creating a new entry


15  Before retrieving deleted entries
16  After retrieving deleted entries

21  Fill hidden fields 

In total there are 39 events provided by SAP.

HOW TO USE THESE EVENTS?

Now we will learn how to use these events in some simple steps:

1. Create a table in SE11.
2. Go to Utilities ->Table Maintenance Generator -> Fill Required data of this Screen -> Environment -> Modification -> Events.
3. Select the event.
4. Give the form name and click on the editor icon.
5. Select the include to place your form definition.
6. Within the form write your code . Save and Activate.
7. Now just go to SM35 to maintain your table to witness the events trigger.

EXAMPLE 

Table : Let us take a simple table having fields as employee id  ,  employee name and date on which  entry created.

Requirement : The employee name and date should get automatically populated while creating a new entry.

Table Name : ZTEST_EVENTS

Fields :                           Type 
ZEMPID                        NUMC
ZNAME                        CHAR
ZDATE                          DATS

Now create the Table Maintenance Generator.

Select Event :  05  Creating a new entry.

Give the form name as : CREATE_ENTRY and write the following code.

FORM create_entry.
ztest_events-zname = sy-uname.
ztest-events-zdate   = sy-datum.
ENDFORM.

Save and Activate.

Now if we go to maintain the table and leave name and date as blank , we will see that they are automatically created.

Hence , In this way events can be used.



Saturday, 17 November 2012

CRM to ECC Debugging



In the case of CRM to ECC integration there may be scenarios when you need to debug from CRM to ECC. Like in case of client replication from CRM to ECC , if the replication is not successful ,than you may need to check if some data error is there are some mandatory fields is missing. For that you need to debug the system from CRM to ECC.

To debug you can follow following approach:

1.  DEBUGGING FROM SMW01:

·         Put a break-point in
o    FM CRM_UPLOAD_AFTER_MAP_HANDLER  in CRM System
o   In class cl_exithandler in ECC system.
·         Reprocess the BDOC.
·         In debugging mode change the value of variable lv_synchron  to ‘X’.
·         Press F8 , control will reach to class in ECC.
·         Now you can put a break-point in your Z FM if you have created any to populate missing fields.
·         Put a break-point for ABAP command ‘CALL TRANSACTION’.
·         Change the transaction mode to ‘A’ and press F8. Control will go to screens where you can see what all values are populating and which are missing.
2. DEBUGGING THROUGH QUEUE SMQ1.
·         De register the queue from from CRM system by transaction SMQS for ECC system.
·         Put a break point on class cl_exithandler in ECC.
·         Create the BP and it will stop in queue SMQ1.
·         Go to SMQ1, double click on the BDOC. Double click on the queue name. You will get the FM name ‘CRM_UPLOAD_TRIGGER’.
·         Put the cursor on this FM and click on the debugging button.
·         Press f8 , control will reach to class in ECC.
·         Now you can put a break-point in your Z FM if you have created any to populate missing fields.
·         Put a break-point for ABAP command ‘CALL TRANSACTION’.
·         Change the transaction mode to ‘A’ and press F8. Control will go to screens where you can see what all values are populating and which are missing.

Thursday, 1 November 2012

VARIANTS in SAP - Part III


INTRODUCTION: In this post we will learn about the different standard SAP Function Modules which can be used in reports / programs to achieve certain requirements.


Function Module Description
RS_CREATE_VARIANT
To create variants for reports
RS_VARIANT_EXISTS To check whether a variant exists for a report
RS_VARIANT_CATALOG Returns the variant list for a report in an internal table
RS_VARIANT_FETCH To get the variant attributes of an existing variant
RS_VARIANT_COPY To copy an existing variant
RS_CHANGE_CREATED
_VARIANT
To change the contents of an existing variant
RS_VARIANT_CONTENTS To get the variant contents
RS_VARIANT_ATTR_SAVE To save the variant attributes. Helpful for dynamic variants
RS_VARIANT_VALUE_SAVE   To save the variant values.

Wednesday, 24 October 2012

VARIANTS in SAP - Part II

INTRODUCTION: In this post well will learn about tables associated with variant values and variant attributes.

Variants are client dependent.

VARIANT VALUES: The variant values are stored in a cluster table named VARI.

VARIANT DIRECTORY: The table VARID is a directory of  variants present in system.
This table stores values such as : Variant Name , Report name for which variant is created , Information about when and by whom the variant was created ,version of variant , protected , transport and environment information.
  1. Protected: This tells whether the variant is protected or not.If a variant is a protected variant then it can be modified only by the user who has created it.
  2. Transport: This gives the information whether a variant can be transported to other systems or not.
  3. Environment: Gives information about the variants usage in foreground or only in background processing.
How to change protected variants ?: To change protected variants we need to remove the protected flag from VARID table for a given variant. SAP has provided a standard program for this purpose.
Program : RSVARENT can be used to unprotect variants. It takes report and variant name as input.

How to use table TVARVC for dynamic variants ?: The table TVARVC can be used for creating variants where input data needs to be changed frequently.

We have to first maintain high and low values for a field in the table TVARVC .Transaction STVARVC can be used to maintain  these values in client 000. In other clients the table can de directly maintained in SM30.

This values can be then used in variants while saving the variant attributes.

To create dynamic value for any field, Click on Selection variable and then press F4.

The F4 for the selection variable shows the following options.
T – Table Variable for TVARVC
D – Dynamic data calculation.

When T is selected values will be  fetched from TVARVC table. Press F4 on 'Name of Variable' for the field and select the desired entry.

When D is selected  it corresponds to dynamic date calculations and hence is suitable for date fields.




 


Sunday, 14 October 2012

VARIANTS in SAP - Part I

What are variants?

In SAP ABAP reports have a mechanism for entering values for database selection or for performing different calculations through SELECTION SCREENS.

Selection screen provides a user an option to execute the reports or transactions as per his requirements and thus provide a range of values as an input.

Whenever there is a requirement to execute a report with the same set of input values again and again, for large selection screens entering the same values can be a tiresome and boring task. Thus SAP has provided a functionality to save these values in a set called as VARIANTS.

Variants can prove to be useful in various ways:
  1. As mentioned above, when there is a requirement to execute a report with a same set of values every time.
  2.  When a report needs to be executed in background mode, variants are the only way to pass values to the report.
  3.  Variants can ensure the integrity of the data being passed as an input and thus minimize the risk of incorrect data entry.

TYPES OF VARIANTS:  Variants values can be Static or Dynamic.

STATIC: Static values are fixed values and do not change over the course of time. 

Example: Let us suppose there is an organization with 10,000 employees and the HR executes a  monthly report having the personnel number as input ,each month. Then in this case we can use the static value giving the range as 1 to 10,000 for this.

DYNAMIC: Dynamic values in a variant can change over time.

Example: The HR wants to execute the report daily and gives the current date as the input. The requirement is that next day when the report is executed the date should be changed to current date.
In such case we can use dynamic values. If the dynamic value is a date , we can use the different date calculation options provided by SAP.
In other cases we can use the values stored in table TVARVC (ECC 6.0 Version) and TVARV (SAP 4.6 ).

PROTECTION OF VARIANTS:
Variants can be saved as protected variants so that only the creator has the authority to change the variant at a later point of time. This can be done by selecting the checkbox for variant protection at the time time of saving variant attributes.