Pages

Thursday, February 10, 2011

Webdynpro Sample - Creating SO

Creating a Sales Order Interface with WebDynpro

A. SE80 -> Create WD Component.

B. Goto ComponentController Context and declare the relevant structure which will be used to pass data to BAPI - BAPI_SALESORDER_CREATEFROMDAT2

Declare all the Structure with their respective Dictionary structure. Keep Cardinality as 1:n

Right click on CONTEXT and create a NODE to declare Structures.

C. To add fields: Click on the Structure name and select Transfer Attribute from the below Table as shown.

Oval: Click here

Select all fields or check selective fields only.

Above picture shows the way it should look once completed.

Please note that more nodes/Structure can be added as per requirement. But here as a Tutorial I will restrict it to these many Structures only.

Context Designing is complete.

Note: Context level Nodes or Attributes can be considered as Global Nodes or Attribute. When proper mapping is done with Context node and ViewContexts, the data from such a Node/Attribute can be used anywhere in any views.

D. Create View for designing the Sales Order creation page:

E. Create Layout of the View:

a. The local context should be created as shown below (Dummy is the same view as First. Created for Tutorial purpose):

Create Header Data Table

Drag and drop the relevant node from ComponentController and select on Import all sub-nodes/attributes when asked.

Click YES for all of the above pop-ups.

Similarly create Nodes for ITEM, PARTNER, SCHEDULE and drag drop relevant nodes from global context to map them.

After dragging and mapping is done, the local context will look as shown below:

Create 2 other variables LV_SOLD_TO and LV_SHIP_TO which will be used in later part of the development. These 2 variables will be of type VBAK-KUNNR.

b. Layout designing:

i. Page Header Element (Dummy is the same view as First. Created for Tutorial purpose)

ii. Header for SO:

Header data is a Group Element.

Properties of Header data element

Note: Maintain Column Count as 4 to achieve desired design.

iii. Add Input Field and Label for the Field

Input fields should always be bound to a value from Local.

Oval: Click to bind to local Context

On selecting the binding field, the displayed Value :

Oval: “Required” – Mandatory InputOval: “View_Name.Node_Name.Attribute_Name”

iv. Create Other Input fields and Blocks for ITEM and PARTNER data in similar way. The Layout appears as shown below:

These blocks are all Group elements.

Each of them has Layout as “GridLayout” with 4 or 2 columns, as required.

v. Add Buttons

Use a Transparent Container to add buttons to the Layout. Create 2 Buttons as shown below:

The “Create Order” button has an “onAction” event as “CREATE”. Double click on this event to write Event handling code in it. Similarly create “RESET” event for the button “Reset” as shown below. The event handling code for both the buttons will be explained in the Code section of this Document.

Before moving to the Codes, add the created View “First” to the Window. Basically a window should always consist of a View. To add the View, double click on the existing Window component on the Right panel. Right click on the Window displayed on the left panel and select “Embed View”. Select the created view “First”. It should appear as below:

Create Webdynpro Application in the Right panel. Right click on the name of the Webdynpro (ZWD_VA01 in this case) and select “Web Dynpro Application”.

Save and Activate.

Now we can run the Webdynpro from the Application and the screen should look like below. Presently all the fields are dummy fields and does not have any functions bound to it.

Layout design is completed. We will move to the Code part of the development.

Codes for creating SO:

In the Method Tab of the View “First”, we will be writing codes to implement 2 events for the 2 Buttons used in the screen. No other methods need to be created or defined.

1. ONACTIONCREATE

Oval: ClickUse the Code Wizard for referring to various elements used in the Layout.

For example, Header node can be read like this:

Finally group all the global declarations, which will be used repeatedly by the Code, at top as shown below:

DATA LO_ND_HEADER TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_HEADER TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_HEADER TYPE WD_THIS->ELEMENT_HEADER.
DATA LO_ND_ITEM TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_ITEM TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_ITEM TYPE WD_THIS->ELEMENT_ITEM.
DATA LO_EL_CONTEXT TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_CONTEXT TYPE WD_THIS->ELEMENT_CONTEXT.
DATA LV_SOLD_TO LIKE LS_CONTEXT-LV_SOLD_TO.
DATA LV_SHIP_TO LIKE LS_CONTEXT-LV_SHIP_TO.

a. Get Header details entered by User:

* navigate from to

via lead selection
LO_ND_HEADER = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_HEADER ).

* get element via lead selection
LO_EL_HEADER = LO_ND_HEADER->GET_ELEMENT( ).

* get all declared attributes
LO_EL_HEADER->GET_STATIC_ATTRIBUTES(
IMPORTING
STATIC_ATTRIBUTES = LS_HEADER ).

b. Get Item details entered by User:

* navigate from to via lead selection
LO_ND_ITEM = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_ITEM ).

* get element via lead selection
LO_EL_ITEM = LO_ND_ITEM->GET_ELEMENT( ).

* get all declared attributes
LO_EL_ITEM->GET_STATIC_ATTRIBUTES(
IMPORTING
STATIC_ATTRIBUTES = LS_ITEM ).

c. Get Partner details entered by User:

* get element via lead selection
LO_EL_CONTEXT = WD_CONTEXT->GET_ELEMENT( ).

* get single attribute
LO_EL_CONTEXT->GET_ATTRIBUTE(
EXPORTING
NAME =
`LV_SOLD_TO`
IMPORTING
VALUE = LV_SOLD_TO ).

* get single attribute
LO_EL_CONTEXT->GET_ATTRIBUTE(
EXPORTING
NAME =
`LV_SOLD_TO`
IMPORTING
VALUE = LV_SHIP_TO ).

d. Create Message handling parameters:

Use the Code Wizard to get references and Message methods.

Error message:

Success message:

DATA LO_API_CONTROLLER TYPE REF TO IF_WD_CONTROLLER.
DATA LO_MESSAGE_MANAGER TYPE REF TO IF_WD_MESSAGE_MANAGER.

LO_API_CONTROLLER ?= WD_THIS->WD_GET_API( ).

CALL METHOD LO_API_CONTROLLER->GET_MESSAGE_MANAGER
RECEIVING
MESSAGE_MANAGER = LO_MESSAGE_MANAGER.

Note: Placement of below code has been shown in the next Point.

* report Success message
CALL METHOD LO_MESSAGE_MANAGER->REPORT_SUCCESS
EXPORTING
MESSAGE_TEXT = lv_so.
* PARAMS =
* MSG_USER_DATA =
* VIEW =
* SHOW_AS_POPUP =
* IS_PERMANENT = ABAP_FALSE
* SCOPE_PERMANENT_MSG =
* CONTROLLER_PERMANENT_MSG =
* MSG_INDEX =
* CANCEL_NAVIGATION =
.

* report Error message
CALL METHOD LO_MESSAGE_MANAGER->REPORT_ERROR_MESSAGE
EXPORTING
MESSAGE_TEXT =
'Sales Order could not be created'
* PARAMS =
* MSG_USER_DATA =
* VIEW =
* SHOW_AS_POPUP =
* IS_PERMANENT =
* SCOPE_PERMANENT_MSG =
* CONTROLLER_PERMANENT_MSG =
* MSG_INDEX =
* CANCEL_NAVIGATION =
.

e. Create the Order using the data entered by the User:

DATA: IT_HEADER TYPE BAPISDHD1,
IT_ITEM
TYPE STANDARD TABLE OF BAPISDITM,
IT_PARTNER
TYPE STANDARD TABLE OF BAPIPARNR,
WA_PARTNER
TYPE BAPIPARNR,
IT_SCHEDULE
TYPE STANDARD TABLE OF BAPISCHDL,
IT_RETURN
TYPE STANDARD TABLE OF BAPIRET2,
WA_RETURN
TYPE BAPIRET2.

MOVE LS_HEADER TO IT_HEADER.
APPEND LS_ITEM TO IT_ITEM.
IF LV_SOLD_TO IS NOT INITIAL.
WA_PARTNER-PARTN_NUMB = LV_SOLD_TO.
WA_PARTNER-PARTN_ROLE =
'AG'. "Role type as per the Customized system
ENDIF.
IF LV_SHIP_TO IS NOT INITIAL.
WA_PARTNER-PARTN_NUMB = LV_SHIP_TO.
WA_PARTNER-PARTN_ROLE =
'WE'. "Role type as per the Customized system
ENDIF.
APPEND WA_PARTNER TO IT_PARTNER.
CLEAR WA_PARTNER.
APPEND LS_SCHEDULE TO IT_SCHEDULE.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
* SALESDOCUMENTIN =
ORDER_HEADER_IN = IT_HEADER
* ORDER_HEADER_INX =
* SENDER =
* BINARY_RELATIONSHIPTYPE =
* INT_NUMBER_ASSIGNMENT =
* BEHAVE_WHEN_ERROR =
* LOGIC_SWITCH =
* TESTRUN =
* CONVERT = ' '
* IMPORTING
* SALESDOCUMENT =
TABLES
RETURN = IT_RETURN
ORDER_ITEMS_IN = IT_ITEM
* ORDER_ITEMS_INX =
ORDER_PARTNERS = IT_PARTNER
* ORDER_SCHEDULES_IN = IT_SCHEDULE
* ORDER_SCHEDULES_INX =
* ORDER_CONDITIONS_IN =
* ORDER_CONDITIONS_INX =
* ORDER_CFGS_REF =
* ORDER_CFGS_INST =
* ORDER_CFGS_PART_OF =
* ORDER_CFGS_VALUE =
* ORDER_CFGS_BLOB =
* ORDER_CFGS_VK =
* ORDER_CFGS_REFINST =
* ORDER_CCARD =
* ORDER_TEXT =
* ORDER_KEYS =
* EXTENSIONIN =
* PARTNERADDRESSES =
.
DATA: LV_SO(200) TYPE C.
READ TABLE IT_RETURN INTO WA_RETURN WITH KEY TYPE = 'E'.
IF SY-SUBRC NE 0. "success
COMMIT WORK.
READ TABLE IT_RETURN INTO WA_RETURN WITH KEY TYPE = 'S'
ID = 'V1'
parameter = 'SALES_HEADER_IN'.
IF WA_RETURN-MESSAGE_V2 IS NOT INITIAL.
CONCATENATE 'Sales Order:' WA_RETURN-MESSAGE_V2
'generated successfully' INTO LV_SO SEPARATED BY space.
ENDIF.
* report message
CALL METHOD LO_MESSAGE_MANAGER->REPORT_SUCCESS
EXPORTING
MESSAGE_TEXT = lv_so.
* PARAMS =
* MSG_USER_DATA =
* VIEW =
* SHOW_AS_POPUP =
* IS_PERMANENT = ABAP_FALSE
* SCOPE_PERMANENT_MSG =
* CONTROLLER_PERMANENT_MSG =
* MSG_INDEX =
* CANCEL_NAVIGATION =
.

ELSE.
* report message
CALL METHOD LO_MESSAGE_MANAGER->REPORT_ERROR_MESSAGE
EXPORTING
MESSAGE_TEXT =
'Sales Order could not be created'
* PARAMS =
* MSG_USER_DATA =
* VIEW =
* SHOW_AS_POPUP =
* IS_PERMANENT =
* SCOPE_PERMANENT_MSG =
* CONTROLLER_PERMANENT_MSG =
* MSG_INDEX =
* CANCEL_NAVIGATION =
.

ENDIF.

2. ONACTIONRESET

Use the Code Wizard again to refer to the Elements on the Layout. Refer each block and use the method INVALIDATE( ) to reset the values entered by the User on pressing the “reset” button.

DATA LO_ND_HEADER TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_HEADER TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_HEADER TYPE WD_THIS->ELEMENT_HEADER.
* navigate from to

via lead selection
LO_ND_HEADER = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_HEADER ).
LO_ND_HEADER->INVALIDATE( ).

DATA LO_ND_ITEM TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_ITEM TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_ITEM TYPE WD_THIS->ELEMENT_ITEM.
* navigate from to via lead selection
LO_ND_ITEM = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_ITEM ).
LO_ND_ITEM->INVALIDATE( ).

DATA LO_ND_PARTNER TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_PARTNER TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_PARTNER TYPE WD_THIS->ELEMENT_PARTNER.
* navigate from to via lead selection
LO_ND_PARTNER = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_PARTNER ).
LO_ND_PARTNER->INVALIDATE( ).

Execute the Application with data:

SO created:

Error Handling:

Using a Material which is not defined in the mentioned Sales Org./Dist.Chan/Div. combination: