Custom getter & setter methods in Webdynpro ABAP – Part 1
Hello… Welcome 🙂
While developing complex WDA applications, we may have to have many context nodes defined and its very much required to get and set data from/to these context nodes.
For getting and setting data from/to a context node, we need to follow the below process
- Define data of type node ( element/elements )
- Get the node reference by using node names
- Get the elements using node reference
- Get data / set data using element reference or node reference based on cardinality i.e. if cardinality is 0…1, 1..1 ( structure ) 0..n, 1…n ( table )
If we have many nodes in our application, say around 10-15 context nodes, we have to repeat above steps for each read/write data access
So, its better idea to have a common method for getting data from context node / for setting data to context node.
HELPER CLASS:
You would be wondering why this HELPER CLASS is needed ?,
It very much helpful to have all common methods in a class and it can be called from any view/ controller method.Hence,it is named as helper class to my application.
I have created the below 2 static methods in class ZCL_WD_HELPER_CLASS
- GET_CTX_STAT_ATTR( ) – Get static attributes / table data of context node
- SET_CTX_STAT_ATTR( ) – Set static attributes / table to context node
Now, let us look at these methods signature & logic 🙂
Method: GET_CTX_STAT_ATTR( ).
Signature:
Logic:
GET_CTX_STAT_ATTR |
---|
METHOD get_ctx_stat_attr. FIELD-SYMBOLS: <lt_stat_attr> TYPE STANDARD TABLE, “=================================== “check if type of target is table “check if context is supplied TRY. ” Return if node is not dirty, and caller wants values only if dirty “get node info “get the type descriptor of node lv_type_name = lo_nd_struct_descr->get_relative_name( ). “============================================ IF lo_nd_info->is_multiple( ) EQ abap_true AND “============================================ IF lo_typedescr_target->kind NE “create data object based cardinality ASSIGN lo_stat_data->* TO <lt_stat_attr>. IF <lt_stat_attr> IS ASSIGNED. ed_stat_attr = <lt_stat_attr>. “============================================ “Prepare lv_index, based on id_index “create data object based cardinality ASSIGN lo_stat_data->* TO <ls_stat_attr>. IF <ls_stat_attr> IS ASSIGNED. ed_stat_attr = <ls_stat_attr>. |
Okay, now let us see how to use the method GET_CTX_STAT_ATTR( ) to get data from context node
Example:
Get data : Let us say we have a context node MY_NODE_T in component controller context with cardinality 0….n /1..n
DATA lt_my_node_t TYPE wd_this->elements_my_node_t.
DATA lv_error_message TYPE string.
CALL METHOD zcl_wd_helper_class=>get_ctx_stat_attr
EXPORTING
io_context = wd_context
id_node_name = 'MY_NODE_T'
* id_index = 0
* ib_only_if_dirty = ABAP_FALSE
IMPORTING
ed_stat_attr = lt_my_node_t
ed_error_message = lv_error_message.
Get data : Let us say we have a context node MY_NODE_S in component controller context with cardinality 0….1 / 1…1
DATA ls_my_node_s TYPE wd_this->element_my_node_s.
DATA lv_error_message TYPE string.
CALL METHOD zcl_wd_helper_class=>get_ctx_stat_attr
EXPORTING
io_context = wd_context
id_node_name = 'MY_NODE_S'
* id_index = 0
* ib_only_if_dirty = ABAP_FALSE
IMPORTING
ed_stat_attr = ls_my_node_s
ed_error_message = lv_error_message.
Continued…….
Hi Ramakrishnappa,
Thanks for this informative step by step guide.
Regards,
Saurabh
Thanks a lot Saurabh 🙂 I am glad if its useful
Your feed back motivates me to go for more ➕
Regards,
Rama