SAPScript is a tool used to create and maintain forms for sending across and mass printing. The tool can develop the forms that supports printing, faxing, e-mailing or XML generated outputs.

Scripts are older version of SAP print forms. SAP Scripts are client dependent and not visible to other clients. Advanced version of script is client independent and they will be visible in all clients.

SE71 is the transaction code for creating SAPScripts. The SAP system has a standard SAPscript forms that are used as a template for form creation. The SAPScript form structure contains of 2 key components and those are −

  • Content − This can be either text or graphics.
  • Layout − The form content appears.

Function Modules used in SAPScripts -

Some function modules used to develop SAPScripts listed below -

Function moduleDescription
OPEN_FORMUsed to open a form for execution by loading it into memory.
WRITE_FORMUsed to write Some information on the SAP Script form using Text Element.
CLOSE_FORMUsed to close the form which is opened by open form.
START_FORMUsed to call another SAP Script into current SAP Script (Nested Scripts).
END_FORMUsed to end the form which started by START_FORM.

SAPScript Components -

SAPScript components are specified below -

ComponentDescription
HeaderContains header information of the script.
Administrative and basic settings of the script
PagesPage is group of windows.
Only 99 pages can be created.
WindowsIs a container that contains the information to display.
Four types of windows are - main window, constant window, variable window, graphical window.
Page windowsWindows assigned to a page called page windows
Paragraph formatUsed to specify the format for all the characters in paragraph.
The format can be font size/family/Bold/italic/underlined.
TABS can also be specified.
Character formatThe format used by a group of characters.
LayoutSpecifies the place to design the page with windows.

Example -

Simple example to implement SAP scripts in writing a Report from the table.

There is two steps in using the SAP script.

  • Creating Script
  • Including script in SAP Program

Create Script -

Below are the step by step process for creating the script.

Step-1: Go to SE71 transaction. It opens the 'Form painter: Request' screen. Enter the Form name with starting letter is Z or Y and select 'page layout' as subobjects. In this case, the form name is ZPRODUCT1 and click on Create button.

Creating Script

Step-2: It opens information dialog to create ZPRODUCT1 and click on Continue.

Creating Script

Step-3: Enter the meaning and click on save.

Creating Script

Step-4: It opens the 'Form: Change Header: ZPRODUCT1'. Click on Setting Form Painter. In SAPscript tab, uncheck both options and click on Continue icon.

Creating Script

Step-5: Click on Pages button to create a new page like below.

Creating Script

Step-6: Click on Character Formats to create custom character format like below.

Creating Script

Step-7: Click on Paragraph Formats to create a new paragraph like below.

Creating Script

Step-8: Click on Windows button to create the new window like below.

Creating Script

Step-9: Click on Page windows button to configure the windows under the page. Click on Edit Create Element to add the window.

Creating Script

Step-10: Add main page as first one, HEADER page as second one and their alignments like below.

Creating Script

Step-11: go to edit and click on text element to enter some content like below.

Creating Script

Step-12: Click on back to exit from Window header to go to 'Form: Change Header: ZPRODUCT1'. Creating Script

Step-12: Go to settings Form painter.

Creating Script

Step-13: In SAPScript tab, check both options and click on Continue icon.

Creating Script

Step-14: It opens the page layout like below. Review the layout.

Creating Script

Step-15: To close this layout go to setting form painter and uncheck the graphical form painter.

Creating Script

Step-16: Enter the default paragraph and First page name in the below screen on 'basic settings'.

Creating Script

Step-17: Activate the form.

Creating Script

Include Script in SAP Program -

Step-18: Now Write the program to include the script.

 
*&---------------------------------------------------------------------*
*& Report  Z_SCRIPT_PROG
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_SCRIPT_PROG.

* Specifying table name
TABLES ZTC_TPRODUCT.

* Declaring cursor and work area
DATA: head(60) TYPE C VALUE '      PRODUCT INFORMATION          ',
      heading(60) TYPE C VALUE 'PRODUCTID  | PRODUCT            '
      ' |    PAMOUNT    |',
      WA  LIKE ZTC_TPRODUCT,
      it  TYPE TABLE OF ZTC_TPRODUCT.

START-OF-SELECTION.
  PERFORM display_script.

FORM display_script.

CALL FUNCTION 'OPEN_FORM'
    EXPORTING
      form                        = 'ZPRODUCT1'
    EXCEPTIONS
      canceled                    = 1
      device                      = 2
      form                        = 3
      OPTIONS                     = 4
      unclosed                    = 5
      mail_options                = 6
      archive_error               = 7
      invalid_fax_number          = 8
      more_params_needed_in_batch = 9
      spool_error                 = 10
      codepage                    = 11
      OTHERS                      = 12.

  IF sy-subrc <> 0.
    MESSAGE 'Form is not opened successfully' TYPE 'I'.
  ENDIF.

  "Starting the Form
  CALL FUNCTION 'START_FORM'
    EXPORTING
      form        = 'ZPRODUCT1'
      program     = 'Z_SCRIPT_PROG'
    EXCEPTIONS
      form        = 1
      format      = 2
      unended     = 3
      unopened    = 4
      unused      = 5
      spool_error = 6
      codepage    = 7
      OTHERS      = 8.

  IF sy-subrc <> 0.
    MESSAGE 'Form is not started successfully' TYPE 'I'.
  ENDIF.

  "Writing the Heading of PO Item
  CALL FUNCTION 'WRITE_FORM'
    EXPORTING
      element                  = 'head'
      window                   = 'HEADER'
    EXCEPTIONS
      element                  = 1
      function                 = 2
      type                     = 3
      unopened                 = 4
      unstarted                = 5
      window                   = 6
      bad_pageformat_for_print = 7
      spool_error              = 8
      codepage                 = 9
      OTHERS                   = 10.

 IF sy-subrc <> 0.
    WRITE: 'Head not written', sy-subrc.
 ENDIF.

  "Writing the Heading of PO Item
  CALL FUNCTION 'WRITE_FORM'
    EXPORTING
      element                  = 'heading'
      window                   = 'HEADER'
    EXCEPTIONS
      element                  = 1
      function                 = 2
      type                     = 3
      unopened                 = 4
      unstarted                = 5
      window                   = 6
      bad_pageformat_for_print = 7
      spool_error              = 8
      codepage                 = 9
      OTHERS                   = 10.

 IF sy-subrc <> 0.
    MESSAGE 'Heading not written' TYPE 'I'.
 ENDIF.

* Retrieving data from the table with product ID
SELECT *
  INTO WA
  FROM ZTC_TPRODUCT ORDER BY PRODUCTID.
        "Writing the line Items one by one
      CALL FUNCTION 'WRITE_FORM'
        EXPORTING
          element                  = 'wa'
          window                   = 'MAIN'
        EXCEPTIONS
          element                  = 1
          function                 = 2
          type                     = 3
          unopened                 = 4
          unstarted                = 5
          window                   = 6
          bad_pageformat_for_print = 7
          spool_error              = 8
          codepage                 = 9
          OTHERS                   = 10.
     WRITE: / WA-PRODUCTID, '|', WA-PRODUCT,  '|',WA-PAMOUNT,  '|'.
ENDSELECT.


  "Ending the Form
  CALL FUNCTION 'END_FORM'

            .
  IF sy-subrc <> 0.
    MESSAGE 'Form is not ended' TYPE 'I'.
  ENDIF.

  "Closing the Form
  CALL FUNCTION 'CLOSE_FORM'

            .
  IF sy-subrc <> 0.
    MESSAGE 'Form is not closed' TYPE 'I'.
  ENDIF.
WRITE: 'Report completed..'.
ENDFORM.

Output -

SAPScripts output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.