3.2.1 Create Sales Order in Java Web Dynpro Using BAPI
3.2.1 Create Sales Order in Java Web Dynpro Using BAPI
com
Tools: SAP NetWeaver Developr Studio SAP Netweaver 7.3 SP00 PAT0000.
monicaradytia@wordpress.com Prerequisite: SAP NetWeaver Developr Studio is installed in your computer SAP J2EE Engine is up. SAP As Java in NWDS already configured. R/3 Configuration is already configured. You have access to a remote SAP back-end system (SAP R/3). The SAP System Landscape Directory (SLD) and the SLD bridge are configured and running.
Knowledge: You have some experience with Java Web Dynpro applications. You have experience in Java programming. You are familiar with the use of Function Modules and BAPIs.
2.
monicaradytia@wordpress.com 3. A new window to create Development Component appear. Type dc_wd_salesorder as name of the new development component. Click Next.
monicaradytia@wordpress.com 4. A new web dynpro development component is created. An empty structure of this dc will be shown like screenshot beside.
6.
Type SalesOrderComp in Component Name field. Then choose the appropriate Component Package. Thick on Default Windows and Views only, since we will used model later.
monicaradytia@wordpress.com 7. A new window appear. If there is no revise from suggested name of window, interface, and view, continue by click Finish.
8.
The structure of our dc after created a component will be look like this screenshot.
11.
Type SOModel in Model Name. Browse appropriate model package, or rename the suggested name. Rename the Default logical name for model instance: WD_SO_MODELDATA _DEST and Default logical system name for RFC metadata: WD_SO_RFC_METAD ATA_DEST Click Next.
monicaradytia@wordpress.com 12. In new window appear, type in Function Name for the 3 BAPIs that will be used in this tutorial:
BAPI_SALES_CREATEF ROMDAT2 BAPI_TRANSACTION_ COMMIT BAPI_TRANSACTION_ ROLLBACK
13.
14.
Click Finish.
monicaradytia@wordpress.com 15. If you need rename, on next screen appear you can do that, but in this tutorial, we will remain the name of model as default. Click Next.
16.
monicaradytia@wordpress.com 17. Make sure your models connection type is UC_SCOPETYPE. Double click on model you have.
18.
20.
Fill the Destination with your RFC Application Data First. Click Next.
monicaradytia@wordpress.com 21. Fill up the form with target host of your SAP Backend. Click Next.
22.
Fill with your username that will you use to login in SAP Backend system. Click Next.
26.
Open the Data Modeler of the components. Here we can see that model already used in our SalesOrderComp.
29.
monicaradytia@wordpress.com 30. Structure of BAPI_SALES_CREATE_ FROMDAT2 appear. Here we only use: Order_Header_In, Order_Header_Inx, Order_Items_In, Order_Items_Inx, Order_Partners, Order_Schedules_In, Order_Schedules_Inx, Output. Click Finish.
monicaradytia@wordpress.com 31. On Method Name rename the method if you will. If not leave default. Click Finish.
32.
Custom Controller SOModelController is created. The model Bapi_Salesorder_Creat efromdat2_Input also automatically bind into custom controllers context.
35.
36.
monicaradytia@wordpress.com 37. Open Context tab of Sales Order View. Right click on Context, New > Node.
38.
39.
41.
42.
monicaradytia@wordpress.com 43. Choose the node. As the result, context of SalesOrderView will contains two new nodes from context mapping to model.
46.
Here we will create UI for header data sales order: - Document Type - Sales Organization - Distribution Channel - Division - Purchase Order No. Choose from Bapi_Salesorder_Creat efromdat2_Input > Order_Header_In. Thick on the appropriate element from the context appear. Click Next.
monicaradytia@wordpress.com
47.
Click Finish.
48.
Form with labels and text fields will automatically create based on context. The value of text fields also bound into context.
49.
We need little additional efforts to complete the UI. Here we will insert text field for title. On the Outline section of SalesOrderView right click on the header datas container > Insert Child.
51.
A new child will be generated. Rename the id property with Header_TextView and text property to Header Data.
52.
53.
We can make change to the attribute of the property. For the example I change the Layout property to not to be able to stretch horizontally. Thus the property is set to false.
monicaradytia@wordpress.com 54. Continue to create another UI, table for store item data. Now we choose Order_Items_In to be applied into template. Right click on SalesOrderView > Apply Template. Choose Table. Click Next. Choose Order_Items_In: - Itm_Number - Material - Target_Qty - Target_Qu Click Finish.
monicaradytia@wordpress.com
55.
Click Finish.
56.
Continue with create two additional button: Add and Delete. On Outline, right click on items container > Insert New Child. Choose Action > Button. Rename the default name identifier with Add. Then copy this button to be pasted and renamed it to Delete.
monicaradytia@wordpress.com 57. Arrange the order of the layout as appropriate thus the result will come like screenshot beside.
58.
We need to create multiple items in item data that will display in table first before bound into context and send to BAPI. Thus, we need to create additional contexts to temporary stores the data before send into BAPI. Double click on SOModelController. On Context tab, right click on Context > New > Attribute.
monicaradytia@wordpress.com 59. Chooose Manually. Enter: Name = Material. Type = string. Click Finish.
60.
61.
Repeat the process to create manually attribute to create two another attributes for quantity and UoM. Attributes: Name = Qty Type = integer Name = UoM Type = string
monicaradytia@wordpress.com
62.
Choose the three attributes by left click on each attribute by pressing shift keyboard. Then right click > Copy.
63.
Open the properties of SalesOrderView, and paste this three attributes into context.
Thus, the context will have three new attributes copy from custom controller.
monicaradytia@wordpress.com 64. Create context mapping. Open Context tab, choose Context Mapping. Drag each the new attribute from SalesOrderView to SOModelController to create context mapping. After finished mapping the context to custom controller, then you can create another UI for item data. Right click on SalesOrder View > Apply Template. Choose Form. Click Next.
65.
66.
68.
Do some order of the arrangement of UIs thus the final result will show like this.
69.
To trigger action to call BAPI_SALES_CREATEF ROMDAT2 we need a button that will generate event. Create a button and rename it to Create Sales Order. Position the button below the Item Data table.
monicaradytia@wordpress.com 70. Last, create a table for display log of creation of sales order. Right click on SalesOrderView > Apply Template. Click Next.
71.
Choose from node Bapi_Salesorder_Creat efromdat2 > Output > Return. Click Next.
72.
monicaradytia@wordpress.com 73. Last, add a label and an input field to display Sales Document Number. Use apply template and bound from node Output > Salesdocument.
monicaradytia@wordpress.com
The first way is by drag and drop data link. Click on SalesOrderView, then from Palette appear choose . If cursor already change Click SalesOrderView to ComponentController. Or
The another method by accessing through References in SalesOrderViews properties. Click Add button.
monicaradytia@wordpress.com
Result:
75.
monicaradytia@wordpress.com
77.
79.
monicaradytia@wordpress.com 81. Type addSalesOrderItem. This method will we use to write some logic to Add button. Click Finish.
82.
83
Since we not yet add method for Bapi_Transaction_Com mit. Now, add a method in custom controller for this BAPI. Click Finish.
monicaradytia@wordpress.com 84. For the three result, we now have three new methods in our custom controller. We will add some logic of this method later.
monicaradytia@wordpress.com
public void wdDoInit() { //@@begin wdDoInit() //$$begin Service Controller(683563848) initBapi_Salesorder_Createfromdat2_Input(); SOModel soModel = new SOModel(); Bapi_Salesorder_Createfromdat2_Input input = new Bapi_Salesorder_Createfromdat2_Input(soModel); wdContext.nodeBapi_Salesorder_Createfromdat2_Input().bin d(input); Bapisdhd1 header = new Bapisdhd1(soModel); Bapisdhd1X headerx = new Bapisdhd1X(soModel); Bapisditm item = new Bapisditm(soModel); Bapisditmx itemx = new Bapisditmx(soModel); Bapiparnr partner = new Bapiparnr(soModel); Bapischdl schedule = new Bapischdl(soModel); Bapischdlx schedulex = new Bapischdlx(soModel); partner.setPartn_Role("AG"); input.setOrder_Header_In(header); input.setOrder_Header_Inx(headerx); input.addOrder_Partners(partner); Bapi_Transaction_Commit_Input inputCommit = new Bapi_Transaction_Commit_Input(soModel); inputCommit.setWait(true); wdContext.nodeBapi_Transaction_Commit_Input().bind (inputCommit); //$$end //@@end }
87.
If there is still appear error, click on bubble lamp that appear beside error and do appropriate import.
88.
Finally SAVE.
monicaradytia@wordpress.com 89. Now we will add logic to the methods we created before. Double click on custom controller. After properties appear, open the Methods tab. First right click on method executeBapi_Salesorde r_Createfromdat2(), Navigate To > Implementation. SAVE.
public void executeBapi_Salesorder_Createfromdat2( ) { //@@begin executeBapi_Salesorder_Createfromdat2() //$$begin Service Controller(-460758325) IWDMessageManager manager = wdComponentAPI.getMessageManager (); try { wdContext.currentBapi_Salesorder_Createfromdat2_In putElement().modelObject().execute(); wdContext.nodeOutput().invalidate(); } catch(ARFC2ModelExecuteException e){ wdComponentAPI.getMessageManager().reportException ( e.getMessage()); } //$$end //@@end }
monicaradytia@wordpress.com 90. Find implementation of method executeBapi_Transacti onCommit() and add some additional java code. SAVE.
public void executeBapi_TransactionCommit( ) { //@@begin executeBapi_TransactionCommit() IWDMessageManager manager = wdComponentAPI.getMessageManager (); try { wdContext.currentBapi_Transaction_Commit_InputElement(). modelObject().execute(); wdContext.nodeBapi_Transaction_Commit_Input().invalidate (); wdContext.nodeOutput_1().invalidate(); } catch(ARFC2ModelExecuteException e){ wdComponentAPI.getMessageManager().reportException ( e.getMessage()); } //@@end }
91.
monicaradytia@wordpress.com
monicaradytia@wordpress.com
public void addSalesOrderItem( ) { //@@begin addSalesOrderItem() SOModel soModel = new SOModel(); Bapisdhd1 header = new Bapisdhd1(soModel); Bapisdhd1X headerx = new Bapisdhd1X(soModel); Bapisditm orderItem = new Bapisditm(soModel); Bapisditmx orderItemx = new Bapisditmx(soModel); Bapiparnr partner = new Bapiparnr(soModel); Bapischdl schedule = new Bapischdl(soModel); Bapischdlx schedulex = new Bapischdlx(soModel); IPublicSOModelController.IOrder_Header_InElement eleh = wdContext.nodeOrder_Header_In().createOrder_Header_InEle ment(header); IPublicSOModelController.IOrder_Header_InxElement elehx = wdContext.nodeOrder_Header_Inx().createOrder_Header_InxE lement(headerx); IPublicSOModelController.IOrder_Items_InElement elei = wdContext.nodeOrder_Items_In().createOrder_Items_InEleme nt(orderItem); IPublicSOModelController.IOrder_Items_InxElement eleix = wdContext.nodeOrder_Items_Inx().createOrder_Items_InxEle ment(orderItemx); IPublicSOModelController.IOrder_PartnersElement elep = wdContext.nodeOrder_Partners().createOrder_PartnersEleme nt(partner); IPublicSOModelController.IOrder_Schedules_InElement eles = wdContext.nodeOrder_Schedules_In().createOrder_Schedules _InElement(schedule); IPublicSOModelController.IOrder_Schedules_InxElement elesx = wdContext.nodeOrder_Schedules_Inx().createOrder_Schedule s_InxElement(schedulex);
monicaradytia@wordpress.com
//Set item number automatically based on data that user input String material = wdContext.currentContextElement().getMaterial(); int qty = wdContext.currentContextElement().getQty(); BigDecimal targetQty = new BigDecimal(qty); String uom = wdContext.currentContextElement().getUoM(); int counterItem = wdContext.nodeOrder_Items_In().size(); int item = 0; if (counterItem == 0){ item = 10; }else{ item = (counterItem * 10) + 10; } String itemNumber2 = "0000" + item; elei.setItm_Number(itemNumber2); elei.setMaterial(material); elei.setTarget_Qty(targetQty); elei.setTarget_Qu(uom); eleix.setUpdateflag("X"); eleix.setItm_Number(itemNumber2); eleix.setMaterial(true); eleix.setTarget_Qty(true); eleix.setTarget_Qu(true); eles.setItm_Number(itemNumber2); eles.setReq_Qty(targetQty); elesx.setUpdateflag("X"); elesx.setItm_Number("000010"); elesx.setReq_Qty(true); wdContext.nodeOrder_Items_In().addElement(elei); wdContext.nodeOrder_Items_Inx().addElement(eleix); wdContext.nodeOrder_Schedules_In().addElement(eles); wdContext.nodeOrder_Schedules_Inx().addElement(elesx); //@@end }
92.
monicaradytia@wordpress.com
public void deleteSalesOrderItem( ) { //@@begin deleteSalesOrderItem() int n = wdContext.nodeOrder_Items_In().size(); int leadSelected = wdContext.nodeOrder_Items_In().getLeadSelection(); for(int i = n - 1; i >= 0; --i){ if (wdContext.nodeOrder_Items_In().isMultiSelected(i) || leadSelected == i){ wdContext.nodeOrder_Items_In().removeElement(wdContext.n odeOrder_Items_In().getElementAt(i)); } } //@@end }
94.
This code means that when Add button is clicked then the event will call method onActionAddSalesOrde rItem(). Furthermore, it will call method addSalesOrderItem that already implemented in custom controller. Continue with Delete button. SAVE.
public void onActionAddSalesOrderItem(com.sap.tc.webdynpro.progmodel .api.IWDCustomEvent wdEvent ) { //@@begin onActionAddSalesOrderItem(ServerEvent) wdThis.wdGetSOModelControllerController().addSalesOrderI tem(); //@@end }
monicaradytia@wordpress.com
public void onActionDeleteItemSo(com.sap.tc.webdynpro.progmodel.api. IWDCustomEvent wdEvent ) { //@@begin onActionDeleteItemSo(ServerEvent) wdThis.wdGetSOModelControllerController().deleteSalesOrd erItem(); //@@end }
95.
monicaradytia@wordpress.com
Click Finish.
monicaradytia@wordpress.com 98. Deploying the project. Right click on SOLaunchApplication > Deploy New Archive and Run.
99.
monicaradytia@wordpress.com
monicaradytia@wordpress.com
Test connection:
monicaradytia@wordpress.com
monicaradytia@wordpress.com
100.
monicaradytia@wordpress.com 101. Result. Since, we not implemented yet data conversion, data such as order type is still using default language that acknowledged by BAPI.
monicaradytia@wordpress.com
102.
Checking the result. Based on the Sales Order number that created in Java Web Dynpro , we then checked the data in the SAP backend system. Now we already succeed to create simple application Create Sales Order in Java Web Dynpro and recorded automatically in SAP backend system.