ABAP Coding

Home / SAP / Archive by category "ABAP Coding" (Page 7)

Web Dynpro – Dynamic Drop Down Lists

Now as promised, creating the dynamic drop down lists.  I mostly skipped these in my previous post just because there are some extra steps, and rather than cram everything together, it seemed like a good point to break it up.  Keep in mind, the concept is the same as my previous post, only for a dropdown list, you need to build the list as well.   (See my previous post for all of the other data declarations).

Hope you find this useful, and as always, if you have a better way to do it, please let  me know. J

DATA LV_BIND_DROP TYPE STRING.
*** check for dropdown fields
dyn_children = rootnode_info->get_child_nodes( ).
READ TABLE dyn_children with key name = field TRANSPORTING NO FIELDS.
CHECK sy-subrc <> 0.
* create sub node of structure (tablename)
* I created a tabletype/structure for DROPDOWNNODE.  If has fields of Key &

* value
cl_wd_dynamic_tool=>create_nodeinfo_from_struct(
parent_info = rootnode_info
node_name = dyn_attr_info-name
structure_name = ‘DROPDOWNNODE’
is_multiple = abap_true ).
*** the On_enter is only if you want something to happen when a value

*** is selected.  I used it to set another context variable.
CONCATENATE LV_BIND ‘.VALUE’ INTO LV_BIND_DROP.
CALL METHOD cl_wd_dropdown_by_idx=>new_dropdown_by_idx
EXPORTING
bind_texts = lv_bind_drop
id         = lv_bind
state      = lv_state
on_Select  = ‘ON_ENTER’
RECEIVING
control    = lr_drop.

*** first, build up the list any way you want and put it into a table with

*** 2 columns, 1 key, 1 value
lo_nd_app->bind_table(
new_items            =  value_list
set_initial_elements = abap_true ).

*** if you want to specifically set a value for the dropdown in advance, here

*** just use this.  The tabix is for the value_list you populated.

lo_nd_app->set_lead_selection_index( sy-tabix ).

Web Dynpro – Dynamically adding Screen Elements

Now, one of the big things I needed for Rapier is to have the screens be completely configurable.  In BSP, this accomplished mostly in the page fragments by doing some looping.  This concept doesn’t exist in ABAP Web Dynpro, so I needed to learn how to do it in Web Dynpro.  Here’s what I learned for dynamically adding screen elements to web dynpro view…

First and foremost, if you want to make dynamic elements in a view, you need to have an attribute to go along with it.  As I use Web Dynpro more and more, this is becoming clear to me why.  The easiest way to get a value onto a screen is using a bound attribute.  So you have 2 choices.  You can create every possible attribute in your view, and use some logic to call the correct one, or you can dynamically create the attributes you need.  While either option “could” work for me, I chose to do this dynamically.  Based on future testing/performance, I may change in the future, but regardless, a fun trick J  Once you have the attribute, you can create the element.  In my case, I needed 2 elements.  A label and something else (input field, longtext, etc…).  The process is same for either one.

Some of the groundwork for this.  I’m using matrixlayout to control the transparent container all of these elements are being put in. I’m skipping dropdowns for this post.  I’ll talk more about those in a separate post.  I’ve done my best to leave the data declarations in here, so you have something that is pretty close to working right here 🙂

It’s funny looking back, it’s not much code for as long as it took me to figure it out.  Ha ha ha.

DATA : rootnode_info TYPE REF TO if_wd_context_node_info,
dyn_node_info TYPE REF TO if_wd_context_node_info,
dyn_node TYPE REF TO if_wd_context_node,
dyn_attributes type WDR_CONTEXT_ATTR_INFO_MAP,
dyn_children TYPE WDR_CONTEXT_CHILD_INFO_MAP.
DATA : dyn_attr_info TYPE wdr_context_attribute_info.
DATA lr_container TYPE REF TO cl_wd_uielement_container.
DATA lr_input TYPE REF TO cl_wd_input_field.
DATA lr_check TYPE REF TO cl_wd_checkbox.
DATA lr_longtext TYPE REF TO cl_wd_text_edit.
DATA lr_drop TYPE REF TO cl_wd_dropdown_by_idx.
DATA lr_label TYPE REF TO cl_wd_label.
DATA lr_invisible TYPE REF TO cl_wd_invisible_element.
DATA lr_table TYPE REF TO cl_wd_table.
DATA lo_nd_sflight TYPE REF TO if_wd_context_node.
DATA lr_button TYPE REF TO cl_wd_button.
DATA lr_grid_data TYPE REF TO cl_wd_grid_data.
DATA lr_flow_data TYPE REF TO cl_wd_flow_data.
DATA lr_matrix_hd TYPE REF TO cl_wd_matrix_head_data.
DATA lr_matrix TYPE REF TO cl_wd_matrix_data.
DATA LV_BIND TYPE STRING.
DATA LV_bind_row TYPE STRING.
DATA lv_state(2) TYPE n.
DATA lv_pwd TYPE wdy_boolean.
DATA lv_label TYPE string.
DATA lo_el_context TYPE REF TO if_wd_context_element.
DATA ls_context TYPE wd_this->Element_context.

lo_el_context = wd_context->get_element( ).

*This will create a attribute at run time…
dyn_attr_info-name = field.
rootnode_info = wd_context->get_node_info( ).
dyn_attributes = rootnode_info->get_attributes( ).

* Check to make sure the attribute doesn’t already exist
READ TABLE dyn_attributes with key name = field TRANSPORTING NO FIELDS.
CHECK sy-subrc <> 0.

CALL METHOD rootnode_info->add_attribute( EXPORTING attribute_info = dyn_attr_info ).

*For Dynamically creating Input Field
*Note : Before that change the Layout of ROOTUIELEMENTCONTAINER to MATRIX LAYOUT
lr_container ?= l_view->get_element( CONTAINER ).
lv_bind = field.

case type.
when ‘TEXT’ OR ‘TIME’ or ‘DATE’.
CALL METHOD cl_wd_input_field=>new_input_field
EXPORTING
bind_value = lv_bind
id         = lv_bind
state      = lv_state
on_enter   = ‘ON_ENTER’
RECEIVING
control    = lr_input.
when ‘CHECK’.
CALL METHOD cl_wd_checkbox=>new_checkbox
EXPORTING
bind_checked = lv_bind
id         = lv_bind
state      = lv_state
RECEIVING
control    = lr_check.
when ‘LONGTEXT’.
CALL METHOD cl_wd_text_edit=>new_text_edit
EXPORTING
bind_value = lv_bind
bind_rows  = lv_bind_row
id         = lv_bind
state      = lv_state
RECEIVING
control    = lr_longtext.
ENDCASE.
CALL METHOD cl_wd_label=>new_label
EXPORTING
label_for = lv_bind
text      = lv_label
RECEIVING
control   = lr_label.

*** once the elements are created, they must assigned to the screen as either
*** a matrix head data or matrix data (head starts a new line)
lr_matrix_hd = cl_wd_matrix_head_data=>new_matrix_head_data( lr_label ).
lr_matrix_hd->set_v_align( ’01’ ).
lr_label->set_layout_data( lr_matrix_hd ).
CALL METHOD lr_container->add_child( lr_label ).
case wa_configrma-type.
when ‘TEXT’ OR ‘TIME’ or ‘DATE’.
lr_matrix = cl_wd_matrix_data=>new_matrix_data( lr_input ).
lr_input->set_layout_data( lr_matrix ).
CALL METHOD lr_container->add_child( lr_input ).
when ‘CHECK’.
lr_matrix = cl_wd_matrix_data=>new_matrix_data( lr_check ).
lr_check->set_layout_data( lr_matrix ).
CALL METHOD lr_container->add_child( lr_check ).
when ‘LONGTEXT’.
lr_matrix = cl_wd_matrix_data=>new_matrix_data( lr_longtext ).
lr_longtext->set_layout_data( lr_matrix ).
CALL METHOD lr_container->add_child( lr_longtext ).
ENDCASE.

Web Dynpro – Creating a Radio Button

Well, I’m stilling hanging out in Web Dynpro land.  I found a new enhancement that Rapier 1.0 really needed.  So here I am again.  This time I had to focus on creating a radio button, because I wanted a customer to be able to select either an existing customer, or create a new one.  IN my brain, a radio button was a nice easy solution…  right???  As always, things are more complicated than I expected.  ha ha ha.  Well, here’s how it works.

You’ll need a context element that will hold the texts for the radio buttons.  This will dynamically determine the number of radio buttons displayed.  To make it work, you’ll need a node and an attribute.  The attribute should just be a string.  Be sure to add a supply function to the node.  This is how the texts will get populated.  See below for the code.

In the layout, create yourself a new element.  I used the RadioButtonGroupByIndex element.  I had a very defined set of values (for this example, 2) so that made life nice an easy.  Inside the element, you need to set a few things…  First, ColCount, this is the number of columns that will be used to display the radiobutton.  Second, texts…  this is a table that contains the texts.  Use the context node you created above.  Third, you’ll want an action for on select.  This will determine what happens when you select a radio button.

Now for some code.  First, you’ll need to populate the radio button text.  In the supply function you assigned in the context node, add something like the following code.

data : it type table of if_v_rp_user_create=>element_cust_method,
wa like line of it.
DATA lo_nd_app TYPE REF TO if_wd_context_node.

wa-text = ‘radio 1’.
append wa to it.
wa-text = ‘radio 2’.
append wa to it.
node->bind_table( it ).

So, those are the basic…

ABAP: Eliminating Errors by using exception ERROR_MESSAGE

I have to thank Jeremy Meier for this post.  I have to confess, I’m pretty excited to have my first guest post, so I just wanted to preface his post with a hearty thank you 🙂  The exception ERROR_MESSAGE is a technique I was unfamiliar with until Jer sent this to me.

I discovered this for one of our developers today – he was getting errors in using a standard SAP function module to get material requirements data from an MD04 function module for a report he was writing.  The code was calling a standard message when it hit a rare error check in the code”

if xyz <> abc.
MESSAGE ID ‘123’ TYPE ‘E’ NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

As this is used in MD04, it’s meant to throw the message to the screen – but when he tried to embed this function module in his Z-Report and ran it on a specific material number it caused an error to occur on the expected automated file download to excel to fail.

After looking for the right TRY-CATCH statement – which wouldn’t actully work because there was no “exception” or dump – the solution after digging a bit was to include the exception ERROR_MESSAGE to his function module call, even though it wasn’t a “defined exception” on the function header:

CALL FUNCTION ‘XYZ’
EXPORTING
VALUE1 = VARIABLE1
IMPORTING
VALUE2 = VARIABLE2
EXCEPTIONS
EXCEPTION_A = 1
EXCEPTION_B = 2
ERROR_MESSAGE = 3.

This action trapped the message and saved it to the SYST table where a standard FM can then be used to extract it if necessary, if the function module returns with sy-subrc=3:

if sy-subrc = 3.
CALL FUNCTION ‘BALW_BAPIRETURN_GET2’
EXPORTING
type  = sy-msgty
cl    = sy-msgid
number = sy-msgno
par1  = sy-msgv1
par2  = sy-msgv2
par3  = sy-msgv3
par4  = sy-msgv4
IMPORTING
return = return.
endif.

Realistically, anytime your using SAP function modules that are part of transactions, developers should try to us this to suppress the error messages and return them on their terms.

Web Dynpro – Dynamically Setting the Theme

I had to do some digging to figure this one out.  I have to give all of the credit to the link I found below.  This is geared toward a Non-Portal application,  Like my Rapier application.  It gives some great pointers on dynamically setting the theme within a web dynpro application.

http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/7015b1f9-535c-2910-c8b7-e681fe75aaf8?QuickLink=index&overridelayout=true&8053063684070

Here’s the quick details.

1.  In your application, add a parameter RUN.  You can use Type String, C or whatever you want.  This is just a flag.  I also added the WDFORCEEXTERNALSTYLESHEET = X.  I’ll be honest, I’m not sure if I need this or not.  From what I’ve read, this is required if you’re using the portal.  I’m not using the portal, but it works if I set this parameter, so I left it.

2.  Now, go to your main window.

3.  Add an outbound plug of type EXIT.  Be sure to give it a parameter of URL.  If you read my or used my post on the logoff button, you already have this =)

4.  Add an inbound plug (or use the default plug).  You need a startup plug is the only key here.  Now double click on it to go into the code.

5.  Once in the code, add the parameter of Run here.  and add the following code (or a variation of it).

  CALL METHOD CL_WD_UTILITIES=>CONSTRUCT_WD_URL
EXPORTING
APPLICATION_NAME = ‘XXX’ “Your web dynpro applicaiton name
IMPORTING
OUT_ABSOLUTE_URL = lv_e_url.

if run is INITIAL.
CONCATENATE lv_e_url
‘?sap-ep-themeroot=/SAP/PUBLIC/BC/UR/NW5/THEMES/SAP_HIGHCONT’
‘&run=N’ INTO lv_e_url.
wd_this->fire_logoff_exit_plg( url = lv_e_url ).
endif.

Here’s what happens.  The first time you log into the application, it works like normal, but run is initial.  so it leaves the current application, and re-runs it with the additional parameters of you set above.

Now I did run into some strange behavior.  I kept getting a dump because of parameter run.  I implemented OSS Note: 946490, still no luck.  I finally just entered a value into the RUN parameter in the application, and then blanked it out.  Then it finally worked.  Not sure what the deal was, but hope it helps you out.

Thanks for reading and I hope this helps…

Web Dynpro – Adding an Icon in a Dynamic Table Column

Here was another fun task I wanted my new ABAP Web Dynpro to be able to add an icon in a dynamic table column.  Again, I expected this to be easy…  well, not as much as I figured, so I thought it might be something you could use.  (and I’m looking for some fun things to write about).

 *** again, start with getting the reference to the table, and then getting all the columns
l_table ?= view->get_element( ID = ‘TB_XXX’ ).
lt_cols = l_table->get_columns( ).
LOOP AT lt_cols into wa_cols.
*** in this example, I want column DOC_ICON to show an icon, rather than text.
IF lv_col_fld = ‘DOC_ICON’.
*** for the correct column, you need to assign an image, rather than the standard text.  In this ***example, I just needed to tell it what to bind for the value (remember, I had this value already,
***so I just need to assign the correct context node.
lv_image = cl_wd_image=>new_image( bind_source = ‘XXX.DOC_ICON’ ).
*** this is the magic.  Telling it what cell editor to use what sets it as text vs. an Image.
Wa_cols->set_table_cell_editor( LV_IMAGE ).
ENDIF.
ENDLOOP.

And that’s the magic…  Hope this can make your life easier…  Because like the catchphrase goes, I learn things the hard way, so you don’t have to.

Web Dynpro – Changing Dynamic Table Header

Well, this strangely was a lot tougher than I expected.  I just wanted the ability to change the column headers based on my own descriptions (or perhaps by language).  Dynamic table header changes took more research than I originally expected, so here’s what I found…  Hope if can help you…

*** get the reference to your table
l_table ?= view->get_element( ID = ‘TB_XXX’ ).
*** get all of the columns so you can name them as you like
lt_cols = l_table->get_columns( ).
LOOP AT lt_cols into wa_cols.
*** Get the text you want to add to the column headings and assign to lv_col_text
lv_col_fld = “column name”.
l_col_hdr = wa_cols->get_header( ).
l_col_hdr->set_text( lv_col_text ).
wa_cols->set_header( l_col_hdr ).
ENDLOOP.

Happy coding =)

Web Dynpro – Creating a Dynamic Table

I hope you’re not getting tired of Web Dynpro.  I will get back to the function SM and VC stuff soon, but lately I’ve been consumed with converting my Rapier product to ABAP Web Dynpro, so as you can guess, that’s where all of my fun learning experiences have been.  For those you tired of this, I promise to be back in functional side again soon.  I’m not finished with my conversion, so that means its time to add new functionality…  Today it’s creating a dynamic table.  For everyone else, I hope you find this useful 🙂

So, to set the stage, a lot of the Rapier functionality is based on dynamic tables.  The idea is that you can define how you want your input and output fields to work.  That means, I needed to be able to build a dynamic table.  I had to a lot of online hunting to pull all this together, but finally got it working how I wanted.  Thought I’d share this with y’all.

I implemented all of this in the wdomodifyview method.

*** Coding for Dynamic Table
DATA: lt_col TYPE cl_abap_structdescr=>component_table,
col like LINE OF lt_col,
struct_type TYPE REF TO cl_abap_structdescr,
table_type TYPE REF TO cl_abap_tabledescr,
node_info TYPE REF TO if_wd_context_node_info,
node TYPE REF TO if_wd_context_node,
tab_XXX TYPE REF TO data,
row_XXX TYPE REF TO data,
l_root TYPE REF TO cl_wd_uielement_container,
l_node TYPE REF TO if_wd_context_node,
l_table TYPE REF TO cl_wd_table,
lt_cols TYPE CL_WD_TABLE_COLUMN=>TT_TABLE_COLUMN,
wa_cols TYPE REF TO CL_WD_TABLE_COLUMN,
l_col_hdr TYPE REF TO CL_WD_CAPTION,
lv_col_text TYPE string,
lv_col_fld TYPE string.
data: lv_image TYPE REF TO cl_wd_image.
data: nd type ref to if_wd_context_node,
nd_list type WDR_CONTEXT_CHILD_MAP,
wa_list TYPE WDR_CONTEXT_CHILD.
data: n type i,
idx TYPE i.
FIELD-SYMBOLS: <table> TYPE table,
<row> TYPE data,
<fs_col> TYPE any,
<fs_col2> TYPE any.

*** build table columns
loop at XXX into wa_XXX where visible = ‘X’.
col-name = “anything”.
*** note, if you use describe_by_name, LV_COL_FIELD must be a table-fieldname combination,
*** then the method will find the data type.  I wasn’t able to find any way to just enter in the date
*** type.
col-type ?= cl_abap_datadescr=>describe_by_name( LV_COL_FLD ).
append col to lt_col.
endloop.

*** build dynamic structure
struct_type = cl_abap_structdescr=>create( lt_col ).
*** returns meta information about the node
CALL METHOD wd_context->get_node_info
RECEIVING
node_info = node_info.

*** this simply checks to see if the node already exists to stop potential short dumps.  This is
***important depending on where you implement this code, and if it runs multiple times.
nd_list = wd_context->get_child_nodes(  ).
READ TABLE nd_list with KEY name = ‘XXX’ TRANSPORTING NO FIELDS.
if sy-subrc <> 0.
*** creates info object and adds it as a lower level node      CALL METHOD node_info->add_new_child_node(
exporting
name = ‘XXX’
static_element_rtti = struct_type
is_static = abap_false
RECEIVING
child_node_info = node_info ).
endif.
*** child node of lead selection or specific index
CALL METHOD wd_context->get_child_node
EXPORTING
name       = ‘XXX’
RECEIVING
child_node = node.
clear n.
n = node->get_element_count( ).
if n = 0.
*** Return RTTI object for sturcture type of static attributes
CALL METHOD node_info->get_static_attributes_type
RECEIVING
rtti = struct_type.
endif.

*** now you have all of the context nodes created…  next up, creating the table.
*** create table
table_type = cl_abap_tabledescr=>create( p_line_type = struct_type ).
FREE tab_xxx.
CREATE DATA tab_xxx TYPE HANDLE table_type.
ASSIGN tab_xxx->* to <table>.
refresh <table>.

*** create lines
FREE row_configequiphis.
CREATE DATA row_xxx TYPE HANDLE struct_type.
ASSIGN row_xxx->* to <row>.

*** populate the values for the table.  Don’t worry too much about this implementation.  This is
***uses dynamic tables to populate the structure.  What you need to do is populate all of your
***rows and columns to make a table that will be bound to your new table.
LOOP AT wd_this->it_xxx INTO wa_xxx.
loop at wd_this->it_xxx into wa_xxx.
ASSIGN COMPONENT wa_xxx-yyy OF STRUCTURE <row> to <fs_col>.
if sy-subrc = 0.
ASSIGN COMPONENT wa_xxx-yyy OF STRUCTURE wa_xxx to <fs_col2>.
if sy-subrc = 0.
<fs_col> = <fs_col2>.
endif.
endif.
endloop.
APPEND <row> to <table>.
ENDLOOP.

*** bind the table
node->bind_table( new_items = <table>
set_initial_elements = abap_true ).

*** build table
l_root ?= view->get_element( ‘TC_TABLE’ ).
l_node ?= wd_context->get_child_node( ‘XXX’ ).

cl_wd_dynamic_tool=>create_table_from_node(
ui_parent = l_root
table_id = ‘TB_TABLE’
node = l_node ).

There’s all the magic.  It’s as simple as creating the nodes, populating those nodes, and then binding them to the table.  Sounds easy right???  as you can see from the code above, it was a lot more complex than I imagined.  Hope this makes life easier for you.

 

Web Dynpro – Logoff Button

This one turned out to be pretty easy, but it sure did take some effort to get to that point…  I guess that’s the irony of it all.  ha ha ha.  So, here was my goal.  I have a web dynpro application that uses a generic user to login as the default.  Then you login with an internet user.  Now the trick that had me stumped for a while was how do I log off the internet user, and reset the application back to the initial login screen the default user.  So this is my solution for creating a logoff button that would reset the transaction back to my original internet user.

I’m sure this sounds easy to any of you Web Dynpro experts out there, but it had me hunting through textbooks, google and my few Web Dynpro contacts.  Finally came up with the trick…  so here you go.

First, in your main window, you need to create an outbound exit plug.  Be sure to give it a parameter of URL.

I recommend setting a context element to hold the intial URL.  You’ll need it later.

Next, in the main view, give your button or however you want to execute the log off procedure.

Once you call the log off procedure, you simply need to take the initial URL and add ‘?logout_submit=true’ to the end.

Here’s an example:

CONCATENATE lv_e_url ‘?logout_submit=true’ INTO lv_e_url.
data: L_REF_MAIN_WINDOW type ref to IG_XXX.
L_REF_MAIN_WINDOW =   WD_THIS->GET_XXX_CTR( ).
L_REF_MAIN_WINDOW->fire_LOGOFF_EXIT_plg( URL = lv_e_url ).

Note:  XXX is the main window name.  LOGOFF_EXIT is the exit plug name.

See what I mean…  it really is that easy =)  good luck.

 

 

ABAP – Suppressing BAPI Messages

I just learned a fun trick when using a BAPI.  I’ve been designing a dynpro transaction and I used a BAPI to pull some information.  What I discovered is that certain BAPI’s have some messages that automatically get displayed, and also caused a weird behavior of leaving the screen (without requesting it.  ha ha ha).  So today I talk about suppressing BAPI messages within a method or form.

Well, I learned a quick fix for this, when calling a function, add the DESTINATION = ‘NONE’.

this will suppress the messages of the BAPI, and just execute the data retrieval I was looking for.

This is a quick one…  I hope you find it useful.