on 02-01-2016 5:32 AM
Hi friends,
I am Saurav Lahiry. I am designing my first webdynpro application. In the output, i have got to enter data from the user in fields name and description
and then on clicking submit button, the data must be saved in the database. I saw many blogs relating to this, but i am unable to understand how to write the code.Please go through the attachment.
Regards,
Saurav Lahiry
Hi Saurav,
This is the simplest application that can be in webdynpro I believe
You don't need to write the logic in WDDOINIT . It should be written in the Action method of the Submit button .
Follow the steps below :
1.) Create your input fields and bind them to the corresponding context attributes in your controller .
2.) Create a submit button, and a corresponding method for action in the right side properties of the button. ONSubmit, that method should trigger.
3.) In that method, use the code wizard to fetch the values of the input fields and use them to update your database table as required.
There is very less manual coding required here. Code wizard along with correct assignment of variables would do .
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rohan,
Thanks a lot for the tips. I did all the steps but when i am clicking on SUBMIT button but it is not getting saved in my custom table ZWEBDYNPRO. Please check the code. I have written it inside onSUBMIT event in the button.
method ONACTIONSUBMIT .
TYPES : BEGIN OF ty_employee,
NAME TYPE pa0001-ename,
PLANS TYPE pa0001-plans,
END OF ty_employee.
DATA : itab TYPE STANDARD table of ty_employee ,
wa TYPE ty_employee.
DATA LO_ND_EMPLOYEE TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_EMPLOYEE TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_EMPLOYEE TYPE wd_this->element_employee.
DATA ls_table_data TYPE wd_this->element_employee.
DATA lt_table_data TYPE wd_this->elements_employee.
* navigate from <CONTEXT> to <EMPLOYEE> via lead selection
LO_ND_EMPLOYEE = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_EMPLOYEE ).
* @TODO handle non existant child
* IF lo_nd_employee IS INITIAL.
* ENDIF.
* get element via lead selection
LO_EL_EMPLOYEE = LO_ND_EMPLOYEE->GET_ELEMENT( ).
* alternative access via index
* lo_el_employee = lo_nd_employee->get_element( index = 1 ).
* @TODO handle not set lead selection
IF LO_EL_EMPLOYEE IS INITIAL.
ENDIF.
* get all declared attributes
LO_EL_EMPLOYEE->GET_STATIC_ATTRIBUTES(
IMPORTING
STATIC_ATTRIBUTES = LS_EMPLOYEE ).
*get all desired attributes in an internal table
CALL METHOD LO_ND_EMPLOYEE->GET_STATIC_ATTRIBUTES_TABLE
EXPORTING
FROM = 1
TO = 2147483647
IMPORTING
TABLE = lt_table_data
.
MOVE lt_table_data TO itab.
LOOP AT itab INTO wa.
MODIFY ZWEBDYNPRO FROM wa.
CLEAR wa.
ENDLOOP.
COMMIT WORK.
endmethod.
Hi Rishab,
Thanks a lot for the tips. I did all the steps but when i am clicking on SUBMIT button but it is not getting saved in my custom table ZWEBDYNPRO. Please check the code. I have written it inside onSUBMIT event in the button.
method ONACTIONSUBMIT .
TYPES : BEGIN OF ty_employee,
NAME TYPE pa0001-ename,
PLANS TYPE pa0001-plans,
END OF ty_employee.
DATA : itab TYPE STANDARD table of ty_employee ,
wa TYPE ty_employee.
DATA LO_ND_EMPLOYEE TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_EMPLOYEE TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_EMPLOYEE TYPE wd_this->element_employee.
DATA ls_table_data TYPE wd_this->element_employee.
DATA lt_table_data TYPE wd_this->elements_employee.
* navigate from <CONTEXT> to <EMPLOYEE> via lead selection
LO_ND_EMPLOYEE = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_EMPLOYEE ).
* @TODO handle non existant child
* IF lo_nd_employee IS INITIAL.
* ENDIF.
* get element via lead selection
LO_EL_EMPLOYEE = LO_ND_EMPLOYEE->GET_ELEMENT( ).
* alternative access via index
* lo_el_employee = lo_nd_employee->get_element( index = 1 ).
* @TODO handle not set lead selection
IF LO_EL_EMPLOYEE IS INITIAL.
ENDIF.
* get all declared attributes
LO_EL_EMPLOYEE->GET_STATIC_ATTRIBUTES(
IMPORTING
STATIC_ATTRIBUTES = LS_EMPLOYEE ).
*get all desired attributes in an internal table
CALL METHOD LO_ND_EMPLOYEE->GET_STATIC_ATTRIBUTES_TABLE
EXPORTING
FROM = 1
TO = 2147483647
IMPORTING
TABLE = lt_table_data
.
MOVE lt_table_data TO itab.
LOOP AT itab INTO wa.
MODIFY ZWEBDYNPRO FROM wa.
CLEAR wa.
ENDLOOP.
COMMIT WORK.
endmethod.
Hi Rohan,
Thanks a lot for the tips. I did all the steps but when i am clicking on SUBMIT button but it is not getting saved in my custom table ZWEBDYNPRO. Please check the code. I have written it inside onSUBMIT event in the button.
method ONACTIONSUBMIT .
TYPES : BEGIN OF ty_employee,
NAME TYPE pa0001-ename,
PLANS TYPE pa0001-plans,
END OF ty_employee.
DATA : itab TYPE STANDARD table of ty_employee ,
wa TYPE ty_employee.
DATA LO_ND_EMPLOYEE TYPE REF TO IF_WD_CONTEXT_NODE.
DATA LO_EL_EMPLOYEE TYPE REF TO IF_WD_CONTEXT_ELEMENT.
DATA LS_EMPLOYEE TYPE wd_this->element_employee.
DATA ls_table_data TYPE wd_this->element_employee.
DATA lt_table_data TYPE wd_this->elements_employee.
* navigate from <CONTEXT> to <EMPLOYEE> via lead selection
LO_ND_EMPLOYEE = WD_CONTEXT->GET_CHILD_NODE( NAME = WD_THIS->WDCTX_EMPLOYEE ).
* @TODO handle non existant child
* IF lo_nd_employee IS INITIAL.
* ENDIF.
* get element via lead selection
LO_EL_EMPLOYEE = LO_ND_EMPLOYEE->GET_ELEMENT( ).
* alternative access via index
* lo_el_employee = lo_nd_employee->get_element( index = 1 ).
* @TODO handle not set lead selection
IF LO_EL_EMPLOYEE IS INITIAL.
ENDIF.
* get all declared attributes
LO_EL_EMPLOYEE->GET_STATIC_ATTRIBUTES(
IMPORTING
STATIC_ATTRIBUTES = LS_EMPLOYEE ).
*get all desired attributes in an internal table
CALL METHOD LO_ND_EMPLOYEE->GET_STATIC_ATTRIBUTES_TABLE
EXPORTING
FROM = 1
TO = 2147483647
IMPORTING
TABLE = lt_table_data
.
MOVE lt_table_data TO itab.
LOOP AT itab INTO wa.
MODIFY ZWEBDYNPRO FROM wa.
CLEAR wa.
ENDLOOP.
COMMIT WORK.
endmethod.
Hey Saurav,
I think you've made it a bit complicated by using itab, I mean this is not needed as per your scenario . The structure itself would have been sufficient to update the database table .
Now that you have used it, debug and check whether your structure entries from the screen are coming in the internal table (itab) or not . If this is not working, you need to change your webdynpro code and simplify it as per this scenario.
Also check that the structure of your ZWEBDYNPRO table should be same as ty_employee because it updates on basis of primary keys in the table.
If structure is same, you can directly use MODIFY ZWEBDYNPRO FROM TABLE itab.
Hi Saurav,
Hope you are doing well...
* Inorder to save data to your table the workarea should be of the type table...
You need to do these below steps to save Data to your database...
1.First create a node in a context either in view controller or component controller and bind it to your input fields
Note:While you creating a node You should mention the same table name in the Dictionary structure field.
2.Coding part in Action method of button
data:lo_context type ref to if_wd_context_node.
call method wd_context->get_child_node
exporting
name = 'NODE' "Here NODE is the Name of your node which u created in your context
receiving
value = lo_context.
data:ls_employee type wd_this->element_node.
*Note:Here i have declared workarea from the local interface created for this view
* You can check it through below steps...
* Click on MAIN(View)-->Click on Attributes tab( Now you can see their Three attributes like WD_CONTEXT , WD_THIS , WD_COMP_CONTROLLER and their Associated type in Third column-->Click on Associated type( IF_MAIN ) of Attribute WD_THIS.
* In this you can see the System code for our program..
* You have to declare the Workarea using the Structure Element_*<> Which is specified under Types
* you can also declare Internal table Using Elements_*<>
call method lo_context->get_static_attributes
importing
static_attributes = ls_employee.
modify zemployee from ls_employee.
if sy-subrc eq 0.
commit work.
else.
rollback work.
endif.
Try this ....It will work...
Regards ,
Rohan
Hello,
Great that it worked for you finally.
For debugging, you just need to place an external breakpoint on your below method and then run the application .
It will stop at this line.
CALL METHOD LO_ND_EMPLOYEE->GET_STATIC_ATTRIBUTES_TABLE
EXPORTING
FROM = 1
TO = 2147483647
IMPORTING
TABLE = lt_table_data
Hi Saurav,
* You can debug using BREAK Statement or else you can also set break point by clicking on gray color vertical line in your editor.
* First of all,If you have created an instance(Object) for an interface and then you can get the object reference by giving node name .Then you can check inside the object reference.All the Data that entered in your Application will be in the object reference of that particular context node.
* So using those methods which provided in the interface( Here interface specifies the one which i mentioned for object creation),we need to extract the data to structure or workarea.
Hope you understand this....
Regards,
Rohan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hope you got the desired output...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I am still not getting desired output. Actually, i want to enter say Rohit in Name field and Sriram in description field. On clicking submit button, the values must be stored in the database. Please provide me with sample code, which i need to write in action of the button or in wddoinit.
Regards,
Saurav Lahiry
Hi,
Your XML says that the code is written in method WDDOINIT, which is not required to address your requirement. WDDOINIT is one of the factory method which would trigger first time when that controller is loaded.
Now let us come to your requirement. You need to read the data from input fields and save in the database.
Read the data
To read the data we must know the context binding of the input field (To locate this, go to Layout tab of the view controller, select the UI element(the input field), go to right side panel which shows UI element property, click on Binding column for Value property of the input field). Upon clicking, you will get one new window displaying the context node and attribute with which the input field is attached.
Now you need to know when the data is to be saved, I hope when you click the button, hence you need to create one event handler method. In Layout tab, you can select the Button and right side panel will display the element properties and bindings. there you get event sub section, you need to give a name to onAction event and then double click on it.
Now you need to read the values here, to do it from code wizard, do -> code wizard ->read context->select the attribute which we saw from Layout tab.
the same way you need to read other input fields.
Save the data
Once you have all the values of required input fields, you may do normal ABAP here, you may write the logic to save the values in the database (It is advisable to write database specific logic in the methods of assistance class and those methods must be called in the view methods)
I hope you had been to below link--
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Saurav,
NOTE : This entire process should be done in Actions of your Submit button...
* First you have to get the object reference of INTERFACE ( IF_WD_CONTEXT_NODE ) using WD_CONTEXT through method ( GET_CHILD_NODE ) ( Note : If the attributes are created below node ).
* Then using that reference,you need to Get attributes using method ( GET_ATTRIBUTES ) of INTERFACE ( IF_WD_CONTEXT_NODE ) to your structure or workarea.
* Through workarea you can change your database tables using modify statement
Regards,
Rohan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
91 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.