You are visitor number


View My Stats

Tuesday, November 18, 2008

Making ALV Grid Editable

Actually this subject is included in BCALV_EDIT* in SAP/R3. I just want to make several conclusions, and make some notes on how to implement an ALV Editable Grid with the minimum method required to make it work. In this example I use a dynamic ALV Grid, so here are the steps:
  1. You must create a screen (e.g. 9000) and create a CUSTOM_CTRL on that screen.
  2. Add these variables to your program:

  3. DATA: GRID TYPE REF TO CL_GUI_ALV_GRID,

    CUST_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

    G_LAYOUT TYPE LVC_S_LAYO,

    G_FIELDCAT TYPE LVC_S_FCAT OCCURS 0,

    GS_FIELDCAT TYPE LVC_S_FCAT.

  4. You must define a class derived from CL_GUI_ALV_GRID to make custom methods that you can edit according to your needs.
    To see the methods (and parameters needed) available for CL_GUI_ALV_GRID, you can double click on the class.
    The code will look like this:

    CLASS LCL_9000_GRIDEVENT DEFINITION.
    PUBLIC SECTION.
    METHODS:
    HANDLE_DATACHANGE FOR EVENT DATA_CHANGED OF
    CL_GUI_ALV_GRID IMPORTING
    ER_DATA_CHANGED
    E_ONF4
    E_ONF4_BEFORE
    E_ONF4_AFTER,

    HANDLE_USERCOMMAND FOR EVENT USER_COMMAND OF
    CL_GUI_ALV_GRID IMPORTING E_UCOMM.

    ENDCLASS.


    CLASS LCL_9000_GRIDEVENT IMPLEMENTATION.

    METHOD HANDLE_DATACHANGE.
    perform f_datachange using er_data_changed.
    ENDMETHOD.


    METHOD HANDLE_USERCOMMAND.
    perform f_usercommand using e_ucomm.
    ENDMETHOD.

    ENDCLASS.


  5. Create a variable that refers to the class created.

    DATA: LCL_9000_GRIDEVENT TYPE REF TO LCL_9000_GRIDEVENT.

  6. Create PBO for your screen. For example your PBO method name is PBO_9000. The code will look like this:

    MODULE PBO_9000 OUTPUT.

    IF CUST_CONTAINER IS INITIAL.

    CREATE OBJECT CUST_CONTAINER
    EXPORTING
    CONTAINER_NAME = 'CS_CTRL'.

    CREATE OBJECT GRID
    EXPORTING
    I_PARENT = CUST_CONTAINER.

    GS_REPID = SY-REPID.


    *---This part will costumize the display of your grid
    G_LAYOUT-GRID_TITLE = 'Put your title here'.
    G_LAYOUT-CWIDTH_OPT = 'X'.
    G_LAYOUT-no_rowins = 'X'.
    *----------------------------------------------------

    *---Build your field catalog for your internal table displayed
    PERFORM F_9000_BUILD.
    *----------------------------------------------------


    CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY
    EXPORTING
    IS_LAYOUT = G_LAYOUT
    CHANGING
    IT_OUTTAB = T_DATA
    IT_FIELDCATALOG = G_FIELDCAT.


    *-----These are the key methods called for editable ALV Grid to *-----work properly
    CALL METHOD GRID->SET_READY_FOR_INPUT
    EXPORTING
    I_READY_FOR_INPUT = 1.

    call method grid->register_edit_event
    exporting
    i_event_id = cl_gui_alv_grid=>mc_evt_enter.
    *-----------------------------------------------------------------------

    *------------------attach your grid to the EVENT HANDLER
    CREATE OBJECT LCL_9000_GRIDEVENT.
    set handler LCL_9000_GRIDEVENT->handle_datachange for grid.
    set handler LCL_9000_GRIDEVENT->handle_usercommand for grid.
    *---------------------------------------------------------


    ENDIF.

    CALL METHOD GRID->SET_FRONTEND_FIELDCATALOG
    EXPORTING
    IT_FIELDCATALOG = G_FIELDCAT.
    CALL METHOD GRID->REFRESH_TABLE_DISPLAY.


    CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = GRID.

    ENDMODULE. " PBO_9000 OUTPUT


  7. You need to fill in G_FIELDCAT variable. For example, you want to make an internal table contains material id and text. Material ID will be editable but not the text because material text will be obtained from MAKT table. This is the implementation looks like:

    FORM F_9000_BUILD.
    clear GS_FIELDCAT.
    GS_FIELDCAT-fieldname = 'MATNR'.
    GS_FIELDCAT-ref_table = 'MARA'.
    GS_FIELDCAT-ref_field = 'MATNR'.
    GS_FIELDCAT-EDIT = 'X'.
    append GS_FIELDCAT TO G_FIELDCAT.

    CLEAR GS_FIELDCAT.
    GS_FIELDCAT-fieldname = 'MAKTX'.
    GS_FIELDCAT-ref_table = 'MAKT'.
    GS_FIELDCAT-ref_field = 'MAKTX'.
    append GS_FIELDCAT TO G_FIELDCAT.
    ENDFORM



Done! You're Editable ALV Grid is ready to be used. Enjoy!

Tuesday, October 7, 2008

Edit program in PROD / QA

I got this code 6 years ago from some site. I don't remember where I got this from, but it's really handy when you need to make small changes of ABAP Programs in PROD/QA. You won't need to make unnecessary transports thus you will also save CR numbers.

Just copy paste the next code, let's name it ZEDIT. Transport it and you will be able to change your programs in PROD/QA as many times as you like.

IMPORTANT NOTES:
1. This program can only change the program that is already exist in PROD/QA, so you will need to transport the program you'd like to change to PROD/QA.

2. To maintain the integrity between DEV and PROD/QA, change the program as you need in DEV. After it's successfully activated, run the ZEDIT program in PROD/QA, copy paste your whole program there, then try to execute your changed program again.

Enjoy!



REPORT ZEDIT .


* global data test data
DATA: BEGIN OF t_report OCCURS 9,
line(72),
END OF t_report.

* macro
define mac_check_uname.
*this program can only be used by you
check sy-uname = 'IT-VERA'.
end-of-definition.

* user interface
PARAMETERS: p_cprog LIKE sy-cprog.


* main program
START-OF-SELECTION.
mac_check_uname.
SELECT SINGLE name FROM trdir INTO p_cprog
WHERE name = p_cprog.
IF sy-subrc <> 0.
MESSAGE e000(zz) WITH 'Program name is not valid!'.
ENDIF.

READ REPORT p_cprog INTO t_report.
EDITOR-CALL FOR t_report. "display-mode title 'Test'.
* EDITOR-CALL FOR REPORT p_cprog.
* IF sy-ucomm = 'TTU'.
IF sy-ucomm = 'WB_SAVE'.
INSERT REPORT p_cprog FROM t_report.
IF sy-subrc = 0.
MESSAGE s000(zz) WITH 'Program is saved!'.
ENDIF.
ENDIF.
END-OF-SELECTION.

Thursday, January 10, 2008

SAP Script Tutorial

I forgot where I got this tutorial, I got it more than 5 years ago he he. Anyway, I want to thank whoever sent me this tutorial cause I found it very useful and handy ;)



- Introduction -

Often there are instances where an output from a SAP program is required on a physical paper in a pre-designed format. Using normal ABAP code this is not possible. Instead SAP provides an object called SAPSCRIPT to generate such kind of documents which can contain logos, tables and other objects and which can look like pre-printed documents.

This article focuses on the design and use of Layout sets in ABAP programs to generate beautified output in SAP.

A layout set is a template designed in SAP to place the stream of data coming from a SAP program on different parts of a physical page. The designer needs to lay out the various elements that need to be printed on the page and store it as an object in the SAP system. An ABAP program will subsequently call this object to generate an instance of the template – thus generating an output document from the program.

In this 7 article tutorial we will cover subjects such as:

· Main elements of SAPscript

· Commands that can be used in a layout set

· How to include a graphical image

· Commands that can be used in SAPscript

· Step-by-step walkthrough of example

· Example layout set

· Example SAPscript code



- Main elements -

Language

Language in which the data coming on to the layout set will be printed. Generally, this will be the language that has been set up as default in the SAP system

Header

Section to define the various attributes of the layout set on a global level. Changing these attributes will affect all the components of the layout set.

The various components of the header are explained below

  • Administration Information

This shows the information about the layout set – details of the designer, details of changes occurring to the design, development class of the layout set and the language details for the layout set

  • Standard Attributes

1. Description - Brief description or title of the layout set

2. Default paragraph - The base paragraph that is globally applicable to the document. This can be overridden at lower level of the layout set by using other paragraphs

3. Tab Stop - The base tab-stop that is globally applicable to the document. These can be overridden at lower level of the layout set by using other tab stops

4. First Page - The start page of the layout set

5. Page Format

6. Orientation - The direction of printing the data on a page – P for portrait (vertical) and L for landscape (horizontal)

7. Lines per inch

8. Characters/inch

  • Font Attributes

Here the various attributes and the base font applicable to the document can be defined. This font setting can be overridden at a lower level using the character strings

Paragraphs

Used to define the start and end positions for the different texts and objects that need to be printed on the output document.

Character Strings

Used to define the fonts and the formatting and printing styles for each and every character that needs to be printed on the output document. The start of the character string is indicated by , while the end of the character string is indicated by

Pages

The designer needs to organise the template as a series of pages. When an actual output document is printed, it will refer to each page for putting the data coming from the ABAP program. The order of pages is also taken from the template i.e the layout set defined.

Windows

Various parts of the output document can be conveniently organised on the pages using windows. Thus the data stream coming from the ABAP program can be logically grouped into various parts and can be placed on different locations on a page

There are 2 main types of windows that can be used in a layout set:

MAIN - A layout set can have only one MAIN window which is created by default. This window can flow over multiple pages.

CONSTANT - A layout set can have any number of constant windows. A constant window can be used once per page

Text Elements

Any text that needs to be written on the output document should be placed within a text element. This includes constant text as well as variable data like internal table data coming from the ABAP program.

It is advisable to group logically related data within one text element.

The fields of various tables defined in the ABAP program will be included under these text elements. These fields are carriers of data. Every field should be included in a pair of & characters. (e.g. &aufk-aufnr&)

Page Windows

All the windows that form a page of the layout set.

Choose the window and click the Text Elements button to go to the Layout Set Editor. This consists of 2 parts

The small space on the left is for specifying the type of command, while the window adjacent to it is for writing the command or the text that needs to go under a text element.


- Including Graphical Images -

The various types of commands that can be used within a layout set are tabulated below

Command

Purpose

*

Default paragraph

Blank

Continuous text

=

Extended Line

(

Raw Line

/

Line Feed

/=

Line feed and extended line

/(

Line Feed and Raw Line

/:

Command Line

/*

Comment Line

/E

Text Element

This is either the name of the paragraph that should be applicable from that line of the layout set



- Layout Set Commands -

The steps for including graphical elements in the layout set are as follows

  • The graphical element (like company logo) should be in valid graphic file format like .bmp or .jpg
  • Use appropriate software to convert the above file into a .TIFF file
  • Use report RSTXLDMC to upload this file as a text module in SAP
  • Execute the above program from the ABAP /4 editor
  • Enter the location of the .TIFF file on the PC
  • Specify BMON or BCOL as the raster image type
  • The SAP system suggests a name for the file ( like ZHEX-MARCO-* ). The * indicates the type of file. For e.g. if the file contains a logo then the name can be ZHEX-MACRO-LOGO
  • The ID should be ‘ST’ and give the logon language
  • Running the program will convert this .TIFF file into a text element
  • Incorporate this converted logo in the appropriate window under the appropriate text element by giving

INCLUDE ZHEX-MACRO-LOGO OBJECT TEXT ID ST in the first line




- Commands -

The commands that are commonly used in a sap script are as follows -:

Command

Use

New-page

Prints the text following this command on a new page (when a page name is specified then that page is taken as the next page)

Protect ….. Endprotect

This acts like a conditional page break. Putting the text within this command prevents the breaking of the text across multiple pages. If there is not enough space for the entire paragraph to be printed in the space remaining on the page, then the entire paragraph is printed on the next page

Box

Position

Size

The BOX command draws a box as per the specifications. The x y co-ordinates are for the upper left corner relative to the values in the position command.

POSITION command is used to set the x y co-ordinates with respect to the start position of the window.

SIZE command is used to specify the size of the box that we need to draw.

Varying these parameters also helps to draw a line instead of a box.

IF ….. END IF

This allows the conditional printing of the text on the output document. The various conditional operators that can be used are as follows

= EQ Equal to

<>

> GT Greater than

<= LE Less than or equal to

>= GE greater than or equal to

<> NE not equal to

The logical operators that can be used are as follows

NOT, AND, OR

- Invoice Example Walkthrough -

The best way to explain the various steps in designing a SAP script is to visualize the creation of a document. The entire process is explained as a series of steps.

Let us assume that we need to design the document Invoice.doc for the ABC Company Limited.

  1. Understand the structure of the document that needs to be generated page by page.
  2. Find out the different pages that form the document.
  3. Decide the FIRST page of the document and the pages that are going to follow.
  4. Find out the various fonts and styles (bold, italics, etc) that are used in the document.
  5. Also try to group the data printed on the document into logical parts.
  6. Create all the character strings that have been used in the document
  7. Create all the paragraphs that have been used in the document
  8. Create all CONSTANT the windows that have been uniquely identified in the document
  9. Identify the MAIN window of the every page of the document
  10. Define the pages that form the parts of the document
  11. Assign the windows to each page
  12. Define the text elements within each window
  13. Use function module OPEN_FORM to open the layout set.
  14. Use function module WRITE_FORM to write the text elements in various windows
  15. Use function module CLOSE_FORM to close the layout set

Always remember to check and activate the layout set when any change is done to it, otherwise the change will not appear on the output document that is printed!!!


- Example Layout Set -

Layout set           Z_TESTSCRIPT
Description          Test SAP script
 
Standard attributes
  First page          FIRST
  Default paragraph   P1
  Tab-stop            1.00 CH
  Page format         DINA4
  Orientation         Landscape
  Lines/inch            6.00
  Characters/inch      10.00
 
Font attributes
  Font family         COURIER
  Font size           12.0 Point
  Bold                No
  Italic              No
  Underlined          No
 
Characters    Attributes
  B           Character String Bold
              Standard attributes
              Marker            No
              Font attributes
              Bold              Yes
 
Paragraphs    Attributes
  P1          Default Paragraph
              Standard attributes
              Line spacing      1.00 LN
              Left margin       1.00 CM
              Alignment         Left-aligned
              Font attributes
              Font family       TIMES
              Font size         12.0 Point
 
  P2          Header Paragraph
              Standard attributes
              Line spacing      1.00 LN
              Left margin       4.50 CM
              Alignment         Left-aligned
              Font attributes
              Font family       TIMES
              Font size         18.0 Point
              Bold              Yes
 
  P3          Undelined paragraph
              Standard attributes
              Line spacing      1.00 LN
              Alignment         Left-aligned
              Font attributes
              Font family       TIMES
              Font size         12.0 Point
              Underlined        Yes
 
Windows       Attributes
  MAIN        Main window
              Window type       MAIN
  HEADER      Main window
              Window type       CONSTANT
  FOOTER      Main window
              Window type       CONSTANT
 
Pages         Attributes
  FIRST       First Page
              Standard attributes
              Next page         FIRST
              Page counter
              Mode              START
              Numbering type    Arabic numerals
              Page window
              HEADER               Left margin          00.00 CM
                                   Upper margin         00.00 CM
                                   Window width         20.00 CM
                                   Window height        04.00 CM
              MAIN                 Left margin          00.00 CM
                                   Upper margin         05.00 CM
                                   Window width         20.00 CM
                                   Window height        20.00 CM
              FOOTER               Left margin          00.00 CM
                                   Upper margin         25.00 CM
                                   Window width         20.00 CM
                                   Window height        04.00 CM
 
Text elements for following windows:
HEADER
 
Element HEADER
/: POSITION XORIGIN 2 CM YORIGIN '-0.5 CM'
/: BOX XPOS 1 CM YPOS 1 CM WIDTH 18 CM HEIGHT 1 CM FRAME 10 TW INTENSITY 10
/
/
/
P2     ,,TEST PURCHASE ORDER
/
/
 
MAIN
 
Element MAIN
P1  Customer/Supplier:,,&KNA1-NAME1&
/
P1  PO No:,,&EKPO-EBELN&
/
P1  Part No:,,&MAKT-MATNR&
/
P1  Description:,,&MAKT-MAKTX&
/
P1  Quantity:,,&EKPO-MENGE&
/
P1  Sign:&uline(81)&
/
P1  Date:&EKKO-AEDAT&
 
FOOTER
 
Element FOOTER
/: POSITION XORIGIN 2 CM YORIGIN '-0.5 CM'
/: BOX XPOS 1 CM YPOS 1 CM WIDTH 18 CM HEIGHT 1 CM FRAME 10 TW INTENSITY 10
/
/
/
P2     ,,PLEASE SIGN THE PO BEFORE DISPATCH
/
/

- Example Code -

REPORT ZPSAPSCRIPT.
TABLES : EKKO,                        
         EKPO,                        
         KNA1,                        
         USR01,                       
         MARA,                        
         MAKT.
 
DATA : BEGIN OF ZOPTION.
        INCLUDE STRUCTURE ITCPO.
DATA : END OF ZOPTION.
 
PARAMETERS: P_EBELN LIKE EKKO-EBELN,
            P_EBELP LIKE EKPO-EBELP.
 
CLEAR EKPO.
SELECT SINGLE * FROM EKPO
       WHERE EBELN = P_EBELN AND
             EBELP = P_EBELP.
 
CLEAR KNA1.
SELECT SINGLE NAME1 FROM KNA1
              INTO KNA1-NAME1
              WHERE KUNNR = EKPO-KUNNR.
 
CLEAR MAKT.
SELECT SINGLE MAKTX FROM MAKT
              INTO MAKT-MAKTX
              WHERE MATNR = EKPO-MATNR AND
                    SPRAS = SY-LANGU.
 
CLEAR USR01.
SELECT SINGLE * FROM USR01 WHERE BNAME = SY-UNAME.
 
ZOPTION-TDDEST    = USR01-SPLD.        "Output device (printer)
ZOPTION-TDIMMED   = 'X'.               "Print immediately
ZOPTION-TDDELETE  = 'X'.               "Delete after printing
ZOPTION-TDPROGRAM = 'ZPQRPRNT'.        "Program Name
 
CALL FUNCTION 'OPEN_FORM'
     EXPORTING
         APPLICATION        = 'TX'
*         ARCHIVE_INDEX      = ' '
*         ARCHIVE_PARAMS     = ' '
         DEVICE             = 'PRINTER'
         DIALOG             = ' '
         FORM               = 'Z_TESTSCRIPT'
         LANGUAGE           = SY-LANGU
         OPTIONS            = ZOPTION
     IMPORTING
          LANGUAGE           = SY-LANGU
       EXCEPTIONS
         OTHERS     = 1.
 
CALL FUNCTION 'WRITE_FORM'
     EXPORTING
         ELEMENT       = 'HEADER'
*         FUNCTION      = 'SET'
*         TYPE          = 'BODY'
         WINDOW        = 'HEADER'
     EXCEPTIONS
          ELEMENT       = 1.
 
CALL FUNCTION 'WRITE_FORM'
     EXPORTING
         ELEMENT       = 'MAIN'
*         FUNCTION      = 'SET'
*         TYPE          = 'BODY'
         WINDOW        = 'MAIN'
     EXCEPTIONS
          ELEMENT       = 1.
 
CALL FUNCTION 'WRITE_FORM'
     EXPORTING
         ELEMENT       = 'FOOTER'
*         FUNCTION      = 'SET'
*         TYPE          = 'BODY'
         WINDOW        = 'FOOTER'
     EXCEPTIONS
          ELEMENT       = 1.
 
CALL FUNCTION 'CLOSE_FORM'
     EXCEPTIONS
          UNOPENED = 1
          OTHERS   = 2.