Friday, 27 June 2014

Creating Class Interface in SAP Classes

Creating Class Interface in SAP Classes

+ -
As per SAP Standard documentation all the standard interfaces in SAP starts with IF_ and all the custom interfaces starts with ZIF_ , in the below example we will be learning how to create a use interfaces in SAP.
All the custom classes is SAP starts with ZCL_ and all the custom interfaces will start with ZIF_, based on the provided name (at the time of creation), SAP will automatically determine and creates (interface or class).
Go to SE24, provide name as ZIF_SAPN_MATERIAL_EXAMPL, create.
Creating interfaces in SAP Classes
Provide description, save, save it in a local object.
Creating interfaces in SAP Classes
Go to methods tab, add method name as GET_MATERIAL_DETAILS-INSTANCE, click on parameters button and add below parameters.
Creating interfaces in SAP Classes
Save, Go back to methods and add one more method GET_MATERIAL_DESCRIPTIONS.
Creating interfaces in SAP Classes
Click on parameters and provide parameters as below.
Creating interfaces in SAP Classes
Save and activate.
We have created an interface with two methods (which dosen`t have implementation).
No Go to SE24 and create a class ZCL_SAPN_CLASS_EX
Creating interfaces in SAP Classes
Provide description, save it in a local object.
Creating interfaces in SAP Classes
Go to interfaces tab and add interface name asCreating interfaces in SAP ClassesZIF_SAPN_MATERIAL_EXAMPLE, enter
Creating interfaces in SAP Classes
Now go to methods tab, you can see all the methods in the interface are automatically copied.
Creating interfaces in SAP Classes
Save, double click on each method and create your own implementation.
Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS, and add below code.
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.
Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS, add below code
    SELECT * FROM MAKT INTO EX_MAKT
      WHERE MATNR = IM_MATNR
       AND SPRAS = 'E'. "English description only
    ENDSELECT.
Save and activate the class.

Using class in program

Create a SE38 program and follow the steps below. The interface method will be clalled by using INTERFACENAME~METHODNAME.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration
DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the class

START-OF-SELECTION.
  CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MAKT  = WA_MAKT.
  WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MBRSH, WA_MARA-MATKL.
  WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX, WA_MAKT-SPRAS.

1 comment: