Monday, March 17, 2008

SAP ABAP Tutorial: Module Pool Programming. Part 3

SAP ABAP Tutorial: Module Pool Programming. Part 3

We will now proceed further and see the code that needs to be put in place to actually create the sales order.

Code for the PBO Module

*&---------------------------------------------------------------------*
*& Module pool       ZSALESORDSCREEN                                   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*


INCLUDE ZSALESORDERTOP                          .                      "

* INCLUDE ZSALESORDERO01                          .                    *
* INCLUDE ZSALESORDERI01                          .                    *
* INCLUDE ZSALESORDERF01                          .                    *

*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_9000 OUTPUT.
  SET PF-STATUS 'ZSALES'.
  SET TITLEBAR 'ZSL'.

ENDMODULE.                 " STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9000 INPUT.

CASE OK_CODE.

When 'BACK'.
leave to screen 0.

When 'ORDE'.

Perform Create_Salesord.
leave to screen 0.

endcase.

ENDMODULE.                 " USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*&      Form  Create_Salesord
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM Create_Salesord.

*-------------------------Data Declaration-----------------------------*
Data: st_BAPISDHEAD like BAPISDHEAD, " Sales Order Header Data
      ta_BAPIITEMIN like BAPIITEMIN occurs 0 with header line, " Ln item
      ta_BAPIPARTNR like BAPIPARTNR occurs 0 with header line, " Partner
      d_BAPIRETURN1 like BAPIRETURN1,          " Bapi return msg
      d_vbeln like bapivbeln-VBELN.            " Sales Order Number


* Move the data to create sales order in the repective parameters------*
move: txtordtype    to st_BAPISDHEAD-DOC_TYPE,     " Sales document type
      txtpurchord   to st_BAPISDHEAD-PURCH_NO_C,
*      '00010'     to ta_BAPIITEMIN-ITM_NUMBER,
      txtmatno     to ta_BAPIITEMIN-MATERIAL,
*      '1100'      to ta_BAPIITEMIN-PLANT,
      txtqty         to ta_BAPIITEMIN-REQ_QTY,
      'AG'        to ta_BAPIPARTNR-PARTN_ROLE,  " Sold to Party
      txtsoldto   to ta_BAPIPARTNR-PARTN_NUMB.
* Append the internal tables-------------------------------------------*
       append ta_BAPIPARTNR.
       clear ta_BAPIPARTNR.
       append ta_BAPIITEMIN.
       clear ta_BAPIITEMIN.
* Move ship to party---------------------------------------------------*
       move: 'RG'    to ta_BAPIPARTNR-PARTN_ROLE, " Ship to party
             txtshipto   to ta_BAPIPARTNR-PARTN_NUMB.
* Append the internal tables-------------------------------------------*
       append ta_BAPIPARTNR.
       clear ta_BAPIPARTNR.


* Call the Bapi to create the sales order

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT1'
  EXPORTING
    ORDER_HEADER_IN           = st_BAPISDHEAD
*   WITHOUT_COMMIT            = ' '
*   CONVERT_PARVW_AUART        = 'X'
 IMPORTING
   SALESDOCUMENT              = d_vbeln
*   SOLD_TO_PARTY             =
*   SHIP_TO_PARTY             =
*   BILLING_PARTY             =
   RETURN                    = d_BAPIRETURN1
  TABLES
    ORDER_ITEMS_IN            = ta_BAPIITEMIN
    ORDER_PARTNERS            = ta_BAPIPARTNR
*   ORDER_ITEMS_OUT           =
*   ORDER_CFGS_REF            =
*   ORDER_CFGS_INST           =
*   ORDER_CFGS_PART_OF        =
*   ORDER_CFGS_VALUE          =
*   ORDER_CCARD               =
*   ORDER_CFGS_BLOB           =
*   ORDER_SCHEDULE_EX         =
          .

if d_vbeln <> space.
*          Message I001.
*          Message I002 with D_vbeln.
          Message ID 'ZEXERCISE2' TYPE 'S' Number '000'.
*          write: 'Sales order No. ', d_vbeln.
endif.

ENDFORM.                    " Create_Salesord



*&---------------------------------------------------------------------*
*& Include ZSALESORDERTOP                                              *
*&                                                                     *
*&---------------------------------------------------------------------*

PROGRAM  ZSALESORDSCREEN               .

Data: ok_code(4),
      txtordtype(2),
      txtsoldto(10),
      txtshipto(10),
      txtpurchord(10),
      txtmatno(18),
      txtqty(13).

SAP ABAP Tutorial: Module Pool Programming. Part 2

SAP ABAP Tutorial: Module Pool Programming. Part 2

Once the program
'ZSALESORDSCREEN' is created Saved and Activated, run Transaction SE51.

Enter screen number '9000'

Screen numbers can be up to 4 characters long, all of which must be digits. Screen numbers from 9000 are reserved for customer-specific screens.
In the Screen Painter enter a short Description and Activate the screen.

Once this is done you will be presented with a screen that will have three tabs, namely Attributes/Element List/Flow Logic

Click on Element List and type Ok_Code and Activate.

Now click on Flow Logic.

You should see the following code in the Flow Logic.

PROCESS BEFORE OUTPUT.
 MODULE STATUS_9000.

PROCESS AFTER INPUT.
 MODULE USER_COMMAND_9000.

Create
MODULE STATUS_9000 by double clicking on it.  The following code will be automatically created.


INCLUDE ZSALESORDERTOP                          .                      "

* INCLUDE ZSALESORDERO01                        .                    *
* INCLUDE ZSALESORDERI01                          .                    *
* INCLUDE ZSALESORDERF01                          .                    *

*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_9000 OUTPUT.
*  SET PF-STATUS 'xxxxxxxx'.
*  SET TITLEBAR 'xxx'.

ENDMODULE.                 " STATUS_9000  OUTPUT


Uncomment the following code.

  SET PF-STATUS 'ZSALES'. ------------------ Menu Bar for the custom Sales Order Screen.
  SET TITLEBAR 'ZSL'.       -------------------- Title.


Give appropriate names to PF-STATUS and TITLE BAR. Once the code is uncommented and the appropriate names are given. Double click on the name
ZSALES. Once you see the prompt click on create object.

You will see the following three options in the resulting screen.
                                                                                                           
Menu bar                                     Status for Sales Order Screen                                          
Application toolbar                      Status for Sales Order Screen                                          
Function keys                              Status for Sales Order Screen


Click on the Function Keys Drop Down Box. You will see the Standard Tool Bar with Icons that you normally see in the SAP Screen.
Enter the following values in the empty fields.

Standard Toolbar                                                                                                                             
Ente     SAVE     BACK     EXIT     CANCEL    PRINT     FIND     FINDNEXT   FIRSTPAGE  PREVP     NEXT    LAST

Activate the PF-STATUS.

You will be prompted to enter the Function Text and Info Text for the button Enter.
Please enter the following values.

Function Text   'Ente'
Info Text          'Enter'

Execute Transaction SE80 and Activate the complete program by right clicking on it.

We will now create a transaction code for our program. To create a transaction code right click on the main program and from the pop-up menu select Create-----Transaction.

In the dialog box enter the following values.

Transaction Code     'ZSALESORD'
Short Description     'Create Sales Order'


And Click on Save.
You will now be asked to enter Program name and Screen Number. Enter the following values.

Program         ZSALESORDSCREEN
Screen            9000  

Save and Activate the Program

Now we need to enter code to create the Sales order. We also need to create the Screen Elements.

In transaction SE80 in the left pane click on Screens and then double click on 9000.
Now from the menu click on LAYOUT button.

We need to create Labels and Text Boxes for the following fields.

Filed  (Label Display)          Label Name              TextBox Name         Def Length
Order Type                           lblordtyp                     txtordtyp                               2
Sales Org                             lblsalesorg                  txtsalesorg                            4
Distribution Channel            lbldistchnl                   txtdistchnl                             2
Division                                lbldiv                           txtdiv                                    2
Sold-to Party                        lblsoldto                     txtsoldto                            10
Ship-to Party                        lblshipto                      txtshipto                            10
Purchase Ord                       lblpurchord                 txtpurchord                        10
Material                                lblmat                          txtmat                                18
Quantity                               lblqty                           txtqty                                 13

We also need to create a command button as follows

Label                                 Name
Create Order                     lblcreateord


We will now look at the property box which is displayed after double clicking on each screen element.

Order Type (Label)
                   Name                  lblordtyp
                   Text                    Order_Type
                 

Order Type (Text Box)
                   Name                  txtordtyp
                   Text                   
                   Def. Length         2


Similarly fill the Property box for other fileds

Important

Property Box for Command Button in this case the Function Code is important

Create Order  
                           Name                  cmdord
                           Text                    Create Order           
                           Fct Code             ORDE
        

SAP ABAP Tutorial: Module Pool Programming. Part 3                                                                                        

SAP ABAP Module Pool Programming Tutorial Part 1

SAP ABAP Tutorial: Module Pool Programming. Part 1

Related Transaction Codes:

SE51: Screen Painter Initial Screen
SE41: Menu Painter Initial Screen
SE38: ABAP Editor Initial Screen
SE80: Object Navigator


In this tutorial we will explore module pool programming. We will create a sales order.
We will combine the Initial screen and the Overview Screen of Transaction VA01 Create Standard Order into one screen.

We will use the following BAPI to create the Sales Order

BAPI_SALESORDER_CREATEFROMDAT1

The following fields will be updated.

Order Type                              OR
Sales Org                                1000
Dsitribution Channel                10
Division                                    00


Sold-to Party                               7777
Ship-to Party                               7777
Purch Ord No                              15493
Material                                       y-351
Order Quantity                             10

We will now proceed and create the main program.
Run Transaction SE80 and create a program 'ZSALESORDSCREEN'. First just create this program save and activate.

To do so in the dropdown box select Program and type the name
'ZSALESORDSCREEN' in the filed below and hit enter.
You will see a prompt that says 'Program does not exist' do you want to create a new object?

Say yes.
Create a program with TOPINCLUDE, give an appropriate name to the TOPINCLUDE and save the program.

See
SAP ABAP Tutorial: Module Pool Programming. Part 2