Saturday, February 6, 2010

Smartform - Lable printing

http://wiki.sdn.sap.com/wiki/display/Snippets/Mass+Address+or+Label+Printing+in+SMARTFORMS



Added by NagaSwapna Thota , last edited by NagaSwapna Thota on Sep 25, 2008  (view change)
Labels: 
Author:  Naga Swapna Thota
Submitted:  Sep 02, 2008
Related Links:  N/A
"Please create a Smartform for printing address as a label". When I received this short email from our client, I explained them that it is not possible in SMARTFORM. However, it is possible in SAP Script. And, I got teasing reply from client saying that - "I thought developer can do anything...". I accepted the challenge, and I would like to share my experience with my fellow community.
Layout
Let us design a Smart Form as shown below :
Example
Smartforms:
Go to Transaction 'SMARTFORMS' from the SAP Easy Access screen.
Give the Smart Form name which starts with Y or Z and click on Create.
Rename Page1 as FIRST by expanding the node Pages and Windows, double click on the node %PAGE1.
Now coming to the Declarations. As required, the data into the form comes from the Table ADRC
ADRC   --   Addresses (Business Address Services)
The Data Retrieval and printing the Form is done through a Driver Program called ZSF_PRINTADDRESS.
In the Form we need to declare some Import parameters and also define some Global Definitions.
-          For declaring Import parameters double click on Form Interface and include the lines as shown below :
In the above screen shot Y_TABTYP_ADRC is the table type which is created for the line type ADRC.
-          For Global Definitions, double click on the Global Definitions and include the lines as follows :
Let us create a Header Window for printing the Headings:
For this Right Click on the node First Page ->  Create -> Window
Name the window as HEADER and give some Description. Ensure that the Window Type is Secondary Window.
In order to write the Details in this window, we have to create TEXT. For this Right click on the node Header -> Create -> Text.
Give some Name and Description to the TEXT. In the Editor give the Details as below.
In the Screen shot below an arrow is placed on a button in application toolbar. Click on it to get the names of the fields. Expand the node Import Parameters. You will encounter the fields IM_S_ADDRNUM_LOW  &  IM_S_ADDRNUM_HIGH. Drag and Drop them from this list.
Create a style to make the fields appear in the way you require.
Now let us create program lines in the Main Window for reading the number of records retrieved from the database table ADRC according to the user input. For this Right click on Main Window -> Create -> Flow Logic -> Program Lines.
Give a Name and Description to the Program Lines. In the Input Parameters give the Table name as IM_T_ADRC ( The Table in which we have ADRC details ). In the output Parameters give the field name as W_LINES ( The Field which stores the number of lines retrieved from the database ). Now write the code as shown below.
Now let us create a LOOP to Loop the records. For this Right click on Code_Overall -> Create > Flow Logic- -> Loop.
Give a Name and Description to the Loop. In the Tab -- DATA give the Internal table as IM_T_ADRC    INTO    IM_FS_ADRC.
To Display the contents create a TEMPLATE. Right Click on LOOP_OVERALL -> Create -> Template.
Give some Name and Description to the TEMPLATE. Create three Line Types namely, LINE1, LINE2, LINE3. Draw lines as shown in the screen shot below.
You can see 9 cells being designed. This is for sending 9 addresses each into one cell.
Now let us give a Condition for the Template to check whether the index is less than or equal to the last record. Go to the Tab -- Conditions and give the details as below :
Now we have to put 9 TEXTS in the Template in order to print  addresses in 9 cells. For each cell we have to Read the table IM_T_ADRC at corresponding index. In order to read the first record which is at index 1, Create Program Lines.
Give some Name and Description to the Program Lines. Give the Input Parameters as IM_T_ADRC & IM_FS_ADRC. Give the Output Parameters as W_INDEX. Write the Code lines as shown in the Screen shot below.
Now let us create a Text node for printing the Address in the Cell 1.
Give Name and Description to the Text as ADDR_1 and Text for Address 1. In the Screen shot below an arrow is placed on a button in application toolbar. Click on it to get the names of the fields. Expand the node Import Parameters. You will encounter the field list. Drag and Drop the Fields you want to display as shown below
In the Node ADDR_1 go to the Tab -- Conditions to specify a condition to stop the Loop if it is a Last Record.
In the Node ADDR_1 go to the Tab - Output options to specify the position where the record has to be printed. Give the Line and Column for the First record as 1 and 1
Now repeat the steps LOOP_CODE1 & ADDR_1 Nine (9)  times to populate into 9 Cells. The Screen would Appear like the Screen shot below:
In the Output options Tab of the node ADDR_2 through ADDR_9, the Line and Column numbers
 Will be as follows:
Finally Save and Activate the Form.
As we know, when we Activate the Smart Form it generates a Function Module automatically. To view the Function Module generated for this Smart Form, Go to Environment à Function Module Name. By Clicking on the Function Module Name, a pop up appears which gives the name of the Function Module.
Driver Program
Now Let us write a Driver program for this.
Driver program to Execute the form ZSF_PRINTADDRESS
Go to Transaction SE38 from the SAP Easy Access screen. Create a program with name ZSF_PRINTADDRESS.
As in the following program, declare the Selection screen elements, Work variables, Internal Tables etc... and call the Functions 'SSF_FUNCTION_MODULE_NAME' and /1BCDWB/SF00000410 (The one which we got in Smart form by going to Environment à Function Module Name). After calling the Function /1BCDWB/SF00000410 just replace it by FM_NAME as shown in the code below:
 REPORT ZSF_PRINTADDRESS.
 TABLES :
   adrc.
 SELECT-OPTIONS :
   s_addrno FOR adrc-addrnumber.
 DATA :
   t_adrc LIKE
 STANDARD TABLE
       OF adrc.
 DATA :
   w_fmname TYPE rs38l_fnam,
   fs_adrc LIKE LINE OF t_adrc.
 AT SELECTION-SCREEN.
   IF s_addrno IS INITIAL.
     MESSAGE e439(bc_global) WITH text-002.
   ENDIF.
   SELECT SINGLE *
     FROM adrc
     INTO fs_adrc
    WHERE addrnumber IN s_addrno.
   IF fs_adrc IS INITIAL.
     MESSAGE e439(bc_global) WITH text-003.
   ENDIF.
 START-OF-SELECTION.
   SELECT  addrnumber
                name1
                street
                city1
                country
                post_code1
     FROM  adrc
      INTO  CORRESPONDING FIELDS OF TABLE t_adrc
   WHERE  addrnumber IN s_addrno.
   CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
       EXPORTING
             formname           =  'ZSF_ADDRESS'
       IMPORTING
             fm_name            = w_fmname
       EXCEPTIONS
              no_form            = 1
              no_function_module = 2.
   CALL FUNCTION w_fmname
         EXPORTING
             im_t_adrc         = t_adrc
             im_s_addrnum_high = s_addrno-high
             im_s_addrnum_low  = s_addrno-low
         EXCEPTIONS
             formatting_error  = 1
             internal_error    = 2
             send_error        = 3
            user_canceled      = 4.
Finally Save and Activate the program.
Result
When the driver program/Print program is executed, the user should be able to enter a range of Address Numbers (ADRC-ADDRNUMBER).
-          Print the Address corresponding to the each Address number in a separate Window
-          Display lower address number against "From" and Higher address number against "To".
Execute the Program
On the Selection screen give some Input :
When the above input is executed the final output would be: