Wednesday, July 13, 2011

Editable ALV

A simple Editable ALV program

REPORT  ZJPM_MS.
TABLES: ZSCE_TRUCKS .
TYPE-POOLS: SLIS.
DATA: BEGIN OF WA_ITAB.
        INCLUDE STRUCTURE  ZSCE_TRUCKS.
DATA:   CELLTAB TYPE LVC_T_STYL,
        FLAG TYPE CHAR1,
     END OF WA_ITAB.
DATA: ITAB LIKE TABLE OF WA_ITAB.
DATA: G_CONTAINER TYPE SCRFNAME VALUE 'GRID_CONTROL',
      GRID  TYPE REF TO CL_GUI_ALV_GRID,
      G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      LAYOUT     TYPE LVC_S_LAYO,
      SAVE TYPE CHAR1.
DATA: FCAT TYPE LVC_T_FCAT,
      WA_FCAT TYPE LVC_S_FCAT.
DATA: OK_CODE LIKE SY-UCOMM,
      OKCODE LIKE SY-UCOMM,
      WA_TB   TYPE STB_BUTTON.
CLASS ER DEFINITION DEFERRED.
DATA: EVENT_RECEIVER TYPE REF TO ER,
      L_EVENT   TYPE CNTL_SIMPLE_EVENT.
DATA: LT_SELROW  TYPE  LVC_T_ROW,
      LS_SELROW TYPE LVC_S_ROW,
LS_CELLTAB TYPE LVC_S_STYL,
WA_CELLTAB TYPE LVC_S_STYL.
DATA:   LS_TOOLBAR_EXCLUDING TYPE UI_FUNC,
      LT_TOOLBAR_EXCLUDING TYPE UI_FUNCTIONS.
DATA: MODE TYPE CHAR1 VALUE 'D'.
*----------------------------------------------------------------------*
*       CLASS ER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS ER DEFINITION .
  PUBLIC SECTION.
    CLASS-METHODS:
    HANDLE_TB FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
    IMPORTING E_OBJECT E_INTERACTIVE,
*
      HANDLE_UC FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
    IMPORTING E_UCOMM.

  PRIVATE SECTION.
ENDCLASS.                    "er DEFINITION

PARAMETERS: P_VSTEL LIKE LIKP-VSTEL.
SELECT-OPTIONS: S_LIFNR FOR ZSCE_TRUCKS-ZLIFNR.

START-OF-SELECTION.
  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE  ITAB
    FROM  ZSCE_TRUCKS CLIENT SPECIFIED
  WHERE  MANDT   = SY-MANDT
  AND    ZVSTEL  = P_VSTEL
  AND    ZLIFNR  IN S_LIFNR.
  PERFORM OUTPUT_ALV.
  PERFORM ENABLE_DISABLE USING MODE.
  CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*&      Form  OUTPUT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM OUTPUT_ALV .
  CLEAR LAYOUT.
  LAYOUT-ZEBRA        = 'X'.
  LAYOUT-SEL_MODE     = 'A'.
  LAYOUT-STYLEFNAME = 'CELLTAB'.
  SAVE = 'A'.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
   EXPORTING
*   I_BUFFER_ACTIVE              = I_BUFFER_ACTIVE
     I_STRUCTURE_NAME             = 'ZSCE_TRUCKS'
*   I_CLIENT_NEVER_DISPLAY       = 'X'
*   I_BYPASSING_BUFFER           = I_BYPASSING_BUFFER
*   I_INTERNAL_TABNAME           = I_INTERNAL_TABNAME
    CHANGING
      CT_FIELDCAT                  = FCAT
   EXCEPTIONS
     INCONSISTENT_INTERFACE       = 1
     PROGRAM_ERROR                = 2
            .
  DELETE FCAT WHERE FIELDNAME = 'MANDT'.

  LOOP AT FCAT INTO WA_FCAT.
    WA_FCAT-EDIT = 'X'.
    MODIFY FCAT FROM WA_FCAT.
  ENDLOOP.
  REFRESH LT_TOOLBAR_EXCLUDING.
  APPEND '&LOCAL&DELETE_ROW'  TO LT_TOOLBAR_EXCLUDING.
  APPEND '&LOCAL&INSERT_ROW'  TO LT_TOOLBAR_EXCLUDING.

ENDFORM.                    " OUTPUT_ALV
*&---------------------------------------------------------------------*
*&      Module  PBO  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE PBO OUTPUT.
  SET PF-STATUS 'MAIN100'.
  IF G_CUSTOM_CONTAINER IS INITIAL.
    CREATE OBJECT G_CUSTOM_CONTAINER
      EXPORTING
        CONTAINER_NAME = G_CONTAINER.
  ENDIF.
  IF GRID IS INITIAL.
    CREATE OBJECT GRID
      EXPORTING
        I_PARENT = G_CUSTOM_CONTAINER.
    CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        IS_LAYOUT            = LAYOUT
        I_SAVE               = SAVE
        IT_TOOLBAR_EXCLUDING = LT_TOOLBAR_EXCLUDING
      CHANGING
        IT_FIELDCATALOG      = FCAT
        IT_OUTTAB            = ITAB.
    CREATE OBJECT EVENT_RECEIVER.
    SET HANDLER EVENT_RECEIVER->HANDLE_UC
*    EVENT_RECEIVER->HANDLE_MB
       EVENT_RECEIVER->HANDLE_TB FOR GRID.
    CALL METHOD GRID->SET_TOOLBAR_INTERACTIVE.
  ELSE.
    CALL METHOD GRID->REFRESH_TABLE_DISPLAY.
  ENDIF.
ENDMODULE.                 " PBO  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  PAI  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE PAI INPUT.
  OKCODE = OK_CODE.
  CLEAR OK_CODE.
  CASE OKCODE.
    WHEN 'EXIT' OR 'BACK' OR 'CANC'.
      LEAVE TO SCREEN 000.
    WHEN OTHERS.
*     do nothing
  ENDCASE.
ENDMODULE.                 " PAI  INPUT
*----------------------------------------------------------------------*
*       CLASS ER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS ER IMPLEMENTATION.
  METHOD HANDLE_TB.
    CLEAR WA_TB.
    MOVE 'ADD'       TO WA_TB-FUNCTION.
    MOVE ICON_INSERT_ROW TO WA_TB-ICON.
    MOVE 'Add Rows' TO WA_TB-QUICKINFO.
    MOVE 0             TO WA_TB-BUTN_TYPE.
    MOVE SPACE         TO WA_TB-DISABLED.
    MOVE ' '            TO WA_TB-TEXT.
    INSERT WA_TB      INTO E_OBJECT->MT_TOOLBAR INDEX 1.
    CLEAR WA_TB.
    MOVE 'DEL'       TO WA_TB-FUNCTION.
    MOVE ICON_DELETE_ROW TO WA_TB-ICON.
    MOVE 'Delete Rows' TO WA_TB-QUICKINFO.
    MOVE 0             TO WA_TB-BUTN_TYPE.
    MOVE SPACE         TO WA_TB-DISABLED.
    MOVE '' TO WA_TB-TEXT.
    INSERT WA_TB      INTO E_OBJECT->MT_TOOLBAR INDEX 2.

    CLEAR WA_TB.
    MOVE 'SAVE'       TO WA_TB-FUNCTION.
    MOVE ICON_SYSTEM_SAVE   TO WA_TB-ICON.
    MOVE 'Save' TO WA_TB-QUICKINFO.
    MOVE 0             TO WA_TB-BUTN_TYPE.
    MOVE SPACE         TO WA_TB-DISABLED.
*    MOVE 'Save Only' TO WA_TB-TEXT.
    INSERT WA_TB       INTO E_OBJECT->MT_TOOLBAR INDEX 3.
    CLEAR WA_TB.
    MOVE 'CHAN'       TO WA_TB-FUNCTION.
    MOVE ICON_TOGGLE_DISPLAY_CHANGE   TO WA_TB-ICON.
    MOVE 'Change<->Display' TO WA_TB-QUICKINFO.
    MOVE 0             TO WA_TB-BUTN_TYPE.
    MOVE SPACE         TO WA_TB-DISABLED.
*    MOVE 'Release' TO WA_TB-TEXT.
    INSERT WA_TB       INTO E_OBJECT->MT_TOOLBAR INDEX 4.

  ENDMETHOD.                    "HANDLE_TB
  METHOD HANDLE_UC.
    DATA: PVAL TYPE I.
    CALL METHOD CL_GUI_CFW=>FLUSH.
    CASE E_UCOMM.
      WHEN 'ADD'.
        IF MODE = 'D'.
          MESSAGE S398(00) WITH 'Cannot add in Display mode'.
          EXIT.
        ENDIF.
        CLEAR WA_ITAB.APPEND WA_ITAB TO ITAB.
      WHEN 'DEL'  .
        CALL METHOD GRID->GET_SELECTED_ROWS
          IMPORTING
            ET_INDEX_ROWS = LT_SELROW.
        LOOP AT LT_SELROW INTO LS_SELROW.
          WA_ITAB-FLAG = 'X'.
          MODIFY ITAB FROM WA_ITAB INDEX LS_SELROW-INDEX.
        ENDLOOP.
        IF SY-SUBRC = 0.
          DELETE ITAB WHERE FLAG = 'X'.
        ENDIF.
      WHEN 'CHAN'.
        IF MODE = 'D'.
          MODE = 'C'.
        ELSE.
          MODE = 'D'.
        ENDIF.
        PERFORM ENABLE_DISABLE USING MODE.
      WHEN 'SAVE'.
        DELETE ITAB WHERE ZVSTEL IS INITIAL
                       OR ZLIFNR IS INITIAL.
        LOOP AT ITAB INTO WA_ITAB.
          ZSCE_TRUCKS-ZVSTEL = WA_ITAB-ZVSTEL.
          ZSCE_TRUCKS-ZTRUCK = WA_ITAB-ZTRUCK.
          ZSCE_TRUCKS-ZLIFNR = WA_ITAB-ZLIFNR.
          ZSCE_TRUCKS-ZNAMTX = WA_ITAB-ZNAMTX.
          ZSCE_TRUCKS-ZCAPY = WA_ITAB-ZCAPY.
          MODIFY ZSCE_TRUCKS FROM ZSCE_TRUCKS.
        ENDLOOP.
        IF SY-SUBRC = 0.
          MESSAGE S398(00) WITH 'Records Saved'.
        ENDIF.
    ENDCASE.
    CALL METHOD GRID->REFRESH_TABLE_DISPLAY.
  ENDMETHOD.                    "HANDLE_UC
ENDCLASS.                    "ER IMPLEMENTATION
*&---------------------------------------------------------------------*
*&      Form  ENABLE_DISABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_MODE  text
*----------------------------------------------------------------------*
FORM ENABLE_DISABLE  USING    P_MODE.
  LOOP AT ITAB INTO WA_ITAB.
    REFRESH WA_ITAB-CELLTAB.
    LOOP AT FCAT INTO WA_FCAT.
      LS_CELLTAB-FIELDNAME = WA_FCAT-FIELDNAME.
      IF P_MODE = 'D'.
        LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
      ELSE.
        LS_CELLTAB-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
      ENDIF.
      INSERT LS_CELLTAB INTO TABLE WA_ITAB-CELLTAB.
    ENDLOOP.
    MODIFY ITAB FROM WA_ITAB.
  ENDLOOP.

ENDFORM.                    " ENABLE_DISABLE

Tuesday, July 12, 2011

Extending Standard BAPI’s to include extra fields which is not handled by BAPI

Extending Standard BAPI’s to include fields which is not available
Standard BAPI’s will contain only few fields that are most often used. Sometimes we need to include few fields which are not available in BAPI. For that SAP has given the provision to extend the standard fields available in the BAPI. One approach to do that is by having Business Transaction Events (BTE’s).
Example:  Posting FI Documents in FB01 transaction.
Extending BAPI BAPI_ACC_DOCUMENT_POST to include posting key.
Step by step procedure is given below:
1.To create Business Transaction Event, please follow the guideline below.
Step 1: Go to TCode "FIBF".
Step 2: Go to Settings -> Products -> ... of a customer
Step 3: Create a new entry and check the box to activate the product
Step 4: Create a new function module based on the sample(SAMPLE_INTERFACE_RWBAPI01) given by SAP.
Step 5: Go to Settings -> Process Modules -> ... of a customer
Step 6: Create a new entry by specifying the name of the function module name and product name created earlier.
2. Once the Function Module (FM) is created, You will have to code inside that FM.

               Inside that FM, code like this:
                  IF NOT extension IS INITIAL.
                         LOOP AT extension.
                                READ TABLE it_accit WITH KEY posnr = extension(3).
                                  IF sy-subrc IS INITIAL.
                                     it_accit-bschl = extension+3(2).
                                     MODIFY it_accit INDEX sy-tabix.
                                 ENDIF.
                         ENDLOOP.
                  ENDIF.
In all the BAPI’s, there will be one extension internal table which contains all the fields related to that transaction. All we have to do to extend the BAPI is to fill this internal table whichever value we need. In our above example, we have included posting key.
The reason to create a BTE is, we need to fill this extension internal table before the BAPI is called actually. The configuration of BTE is such that this will trigger the function module and the code inside will execute and fill the extension tables.
In the BAPI, we have fill the extension internal table like this,
FORM fill_pst_key .
    *first 3 character represent item number
    *next 2 character represent posting key
 CLEAR: gt_extension.
 gt_extension(3)    = sy-index(3).
 gt_extension+3(2) = 'New Posting Key'.
 APPEND gt_extension.
ENDFORM.



Adobe form scripting

In hierarchy view, the tree structure looks like this
data
|____body|
.........|_Tabe1|
................|_Footerrow[1]|
..............................|_leaf variables

----- data.Body.Table1.FooterRow[1].V_VAT_LOC_CUR::ready:layout - (JavaScript, client) -------------
// vat in local curr - design view
var zero = 0.00;
if (this.rawValue > zero)
{
    this.presence = "visible";
}

----- data.Head.Header.PagewiseTotal.empty::ready:layout - (JavaScript, client) --------------------
// prev. balance heading
var totalpage = xfa.layout.pageCount();
var currpage = xfa.layout.page(this);

if (currpage == 1)
{
}
else
{
    this.presence = "visible";
}
----- data.Head.Header.PagewiseTotal.TextField1::ready:layout - (JavaScript, client) ---------------
// prev balance currency field
var totalpage = xfa.layout.pageCount();
var currpage = xfa.layout.page(this);

if (currpage == 1)
{
}
else
{
    this.presence = "visible";
}
----- data.Head.Header.PagewiseTotal.CarryOver::ready:layout - (JavaScript, client) ----------------
//prev balance text field
var totalpage = xfa.layout.pageCount();
var currpage = xfa.layout.page(this);

if (currpage == 1)
{}
else
{
    var sum = 0;
    while (currpage > 1)
    {
        var total = 0;
        currpage = currpage-1;
        var fields = 0;
        fields = xfa.layout.pageContent(currpage-1, "field", 0);
        for ( i=0; i<= fields.length-1; i++ )
        {
            if (fields.item(i).name == "TOTAL")
            {
                total = total + fields.item(i).rawValue;
            }
        }
        sum = sum + total;
    }
    this.rawValue = sum;

}

----- data.Head.Header.PagewiseTotal.TotalExVAT::ready:layout - (JavaScript, client) ---------------
// total ex. vat text field
var totalpage = xfa.layout.pageCount();
var currpage = xfa.layout.page(this);

if (currpage == totalpage)
{  
//  this.presence = "visible";
    var total = 0.
// get the number of lines in the table and then loop through all the table lines searching for the field and add up
    var num = xfa.resolveNodes("data.Body.Table1.Row1[*]").length;
    for ( var i=0; i<num; i++ )
    {
        total = total + xfa.resolveNode("data.Body.Table1.Row1["+i+"].AMOUNT").rawValue;
    }
    this.rawValue = total;
}
else
{
// get the page content and check for your field and add the value
    var fields = xfa.layout.pageContent(xfa.layout.page(this)-1, "field", 0);
    var total = 0;
    for (var i=0; i <= fields.length-1; i++)
    {
        if (fields.item(i).name == "AMOUNT")
        {
            total = total + fields.item(i).rawValue;
        }
    }
    this.rawValue = total;
}

----- data.Head.Header.PagewiseTotal.VAT::ready:layout - (JavaScript, client) ----------------------
// vat
var totalpage = xfa.layout.pageCount();
var currpage = xfa.layout.page(this);

if (currpage == totalpage)
{  
    var sum = 0, amount, total;
// get the number of lines in the table and loop through all the table lines
// fetch Total and Amount. VAT = Total - Amount
    var num = xfa.resolveNodes("data.Body.Table1.Row1[*]").length;
    for ( var i=0; i<num; i++ )
    {
        total = xfa.resolveNode("data.Body.Table1.Row1["+i+"].TOTAL").rawValue;
        amount = xfa.resolveNode("data.Body.Table1.Row1["+i+"].AMOUNT").rawValue;
        sum = sum + (total - amount);
    }
    this.rawValue = sum;
}
else
{
// get the page content and check for your field and add the value
    var fields = xfa.layout.pageContent(xfa.layout.page(this)-1, "field", 0);
    var total = 0;
    for (var i=0; i <= fields.length-1; i++)
    {
        if (fields.item(i).name == "TOTAL")
        {
            total = total + fields.item(i).rawValue;
        }
    }
    var fields1 = xfa.layout.pageContent(xfa.layout.page(this)-1, "field", 0);
    var total1 = 0;
    for (var i=0; i <= fields1.length-1; i++)
    {
        if (fields1.item(i).name == "AMOUNT")
        {
            total1 = total1 + fields1.item(i).rawValue;
        }
    }
    this.rawValue = total - total1;
}

----- data.Head.Header.PagewiseTotal.Total::ready:layout - (JavaScript, client) --------------------
// total field
var totalpage = xfa.layout.pageCount();
var currpage = xfa.layout.page(this);

if (currpage == 1)
{  
    var pagefields=0;
    var pagefields = xfa.layout.pageContent(currpage-1, "field", 0);
    var pagetotal = 0;
    for (var i=0; i <= pagefields.length-1; i++)
    {
        if (pagefields.item(i).name == "TOTAL")
        {
            pagetotal = pagetotal + pagefields.item(i).rawValue;
        }
    }
    this.rawValue = pagetotal;
}
else
{
    var sum = 0;
    while (currpage>0)
    {
        var total = 0;
        var fields = 0;
        fields = xfa.layout.pageContent(currpage-1, "field", 0);
        for ( i=0; i<= fields.length-1; i++ )
        {
            if (fields.item(i).name == "TOTAL")
            {
                total = total + fields.item(i).rawValue;
            }
        }
   
        sum = sum + total;
        currpage = currpage-1;
    }
    this.rawValue = sum;

}