Sep 9 2011

Best Way to Handle Exceptions in ABAP Classes

I’ve been wanting to switch to exception classes for years but have found them overly cumbersome in most cases.  I’ve finally figured it out.  Here is a simple way to handle simple exceptions using ABAP exception classes from NetWeaver 6.40 and up.

1. Message Class

Make sure you have a message class for custom messages (SE91) with the required message in it

2. Exception Class

Create an exception class in SE80 e.g. ZCX_ERROR

For anyone without a PhD in computer science, or constrained by budget or time, inherit from CX_STATIC_CHECK.

In class type, select Exception Class and check the With Message Class option box.

3. Create a specific exception.

a. If you want parameters in your exception message create them as attributes in the exception class - e.g. FILE_ERROR_NO
b. Click the texts tab and create an Exception ID - e.g. FILE_ERROR
c. Click the Message Text button and link your Exception ID to a corresponding message in the message class.  You can also bind any message attributes (use & in the message text) to the corresponding exception attributes

That’s it.  You are now ready to handle this exception with a single line of code in the CATCH.

4. Raise the exception in your code

  data lo_ex type ref to zcx_error.

  try.


     write: / ‘Oops’.
     raise exception type zcx_error
        exporting textid = zcx_error=>file_error
                  file_error_no = 
100.


  catch zcx_error into lo_ex.


      message lo_ex type ‘E’.


  endtry.


Your MESSAGE is displayed automatically, with the attribute values inserted.   There are no calls to get_text() or get_longtext(), and no data declarations for the resulting message string.

This means a lot fewer references to SY-SUBRC in my code from now on.

Credits:

Thanks to James Wood for putting me on the right track in his book OO Programming with ABAP Objects, although he doesn’t quite nail it. Keller and Kruger don’t even get close  in their 2011 ABAP Objects book.


Aug 1 2011

SAP InnoJam 2011 in Sydney

UNL from ABAP

Sower was represented at SAP Innojam 2011 in Sydney by Peter Chapman.  At the event teams have 24 hours to develop solutions using the newest SAP technologies, including HANA, Gateway, StreamWork, Gravity and SUP.

The team was successful in developing a tool to automatically generate UML diagrams from any ABAP runtime trace log.  Fully integrated into the SAT transaction, their solution generated a UML flow diagram showing objects and calls between them graphically in Gravity, where it is available for collaboration using standard StreamWork tools.

Their solution will be presented at the SAUG later this week.


Jul 8 2011

Embedding WebDynpro into BSP

WebDynpro is great for read/write applications but weaker when it comes to presentation, flexibility and leveraging all the goodies available to normal web developers.

For the best of both worlds, you can build great applications in BSP using beautiful CSS and compelling animations (JQuery etc), and then load embedded WebDynpros when you need something heavier that you wouldn’t want to code or maintain in your BSP.
How do you get the URL for the WebDynpro page you want to embed?  Try this code for size.

    data lt_parameters          type tihttpnvp.
    data ls_parameters          type line of tihttpnvp.

    clear lt_parameters[].
    ls_parameters-name = ‘OBPLUG’.
    ls_parameters-value = i_plug.
    append ls_parameters to lt_parameters.
    clear ls_parameters.
    ls_parameters-name = ‘OBFUNC’.
    ls_parameters-value = i_func.
    append ls_parameters to lt_parameters.
    clear ls_parameters.
    cl_wd_utilities=>construct_wd_url( exporting namespace        = ‘MYNAME’
                                                 application_name = ‘MYAPP’
                                                 in_parameters    = lt_parameters

                                      importing out_absolute_url  = o_url ). 
You will need to manually load the URL reply into the target element on your page:
$.get(o_url, {}, function(reply){
 $(“#target”).html(reply);
 }, “html”);

Jul 2 2011

Create Transaction to Execute SAP BSP Application

I have created a BSP Application and want to run it using a transaction code created in SE93.

If you have transaction START_BSP on your NetWeaver box, then you can make a copy of this transaction with your variant.

However, if this is not installed, you need to write a program that determines the appropriate URL and calls the CALL_BROWSER function module.

Here is some sample code:

report  zbsp_start.

data: lv_url type string,
      lv_urlc(4096) type c,
      lt_parms type tihttpnvp.

start-of-selection.
  parameter: p_app type string.
  parameter: p_page type string.
  parameter: p_parms type string.
end-of-selection.

*– Create URL to BSP Application
  call method cl_http_ext_webapp=>create_url_for_bsp_application
    exporting
      bsp_application      = p_app
      bsp_start_page       = p_page
      bsp_start_parameters = lt_parms
    importing
      abs_url              = lv_url.

*– Call the BSP Application in the default Browser
  lv_urlc = lv_url.
  call function ‘CALL_BROWSER’
    exporting
      url                    = lv_urlc
      window_name            = ‘BSP’
      new_window             = ‘ ’
    exceptions
      frontend_not_supported = 1
      frontend_error         = 2
      prog_not_found         = 3
      no_batch               = 4
      unspecified_error      = 5
      others                 6.

  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.


Aug 12 2010

ABAP Proper Case Regex

I need to pull full user names from ADRP for an audit report, but the case is all over the place. This piece of code converts a character string to proper case (first letter uppercase). I wanted to handle Mc and Mac too, but ABAP implements the Posix regex syntax, which has no support for back referencing. If anyone knows a regular expression to catch the first character after Mac then please post a response.

report zzzzzzzz.

data:
li_ofs type i,
lv_name(30).

lv_name = ‘PETER CHAPMAN’.

write: / lv_name.

translate lv_name to lower case.
while sy-subrc = 0.
translate lv_name+li_ofs(1) to upper case.
find regex ‘\b[a-z]‘ in lv_name match offset li_ofs.
endwhile.

write: / lv_name.