Wednesday, September 7, 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.

Wednesday, July 6, 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");

Wednesday, June 29, 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.

Sunday, August 22, 2010

Enhance your ABAP Pretty Printer

I like my code to have TYPE statements lined up, and assignments as well. So I've enhanced the PRETTY_PRINTER function (yes it is a funciton module) to do that, and insert a comment block at the top as well.

If you don't know how to enhance the pretty printer function to call this code then you probably shouldn't be messing with it. 

Note that after enhancing the PRETTY_PRINTER function SAP will dump any open ABAP source that other programmers may be working on.  Best to do this early or late in the day or you will hear a lot of complaining... :-)

*--------------------------------------------------------------------------
* Developer:        Peter Chapman
* Created:          Thursday, 15. July 2010
* Modified:         Thursday, 12. August 2010
*-----------------------------------------------------------------------


function zpretty_printer.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  CHANGING
*"     REFERENCE(CT_CODE) TYPE  SWBSE_MAX_LINE_TAB
*"----------------------------------------------------------------------

*-- Indent                   type statements nicely during pretty print
  data zf                    type i.
  data zi                    type i.
  data lv_date               type string.
  data lt_days               type table of casdayattr.
  data zs(29).

  field-symbols:
                        type casdayattr,
                       type swbse_max_line.

  check sy-uname = 'PCHAPMAN'.

*-- make sure it is a program
  find first occurrence of regex '^(function|method|report|class)' in table ct_code match line zi.
  check zi > 0.

*-- Handle coding block
  call function 'DAY_ATTRIBUTES_GET'
    tables
      day_attributes         = lt_days.
  read table lt_days index 1 assigning .

  find first occurrence of regex 'Peter Chapman' in table ct_code match line zi.
  if sy-subrc <> 0 or zi > 3.
    insert initial line into ct_code index 1 assigning .
                       = '*-----------------------------------------------------------------------'.
    insert initial line into ct_code index 2 assigning .
                       = '* Developer:        Peter Chapman'.
    insert initial line into ct_code index 3 assigning .
                       = '* Created:'.
    +20                = -day_string.
    insert initial line into ct_code index 4 assigning .
                       = '* Modified:'.
    +20                = -day_string.
    insert initial line into ct_code index 5 assigning .
                       = '*-----------------------------------------------------------------------'.
    insert initial line into ct_code index 6 assigning .
  endif.

  find first occurrence of regex 'Modified' in table ct_code match line zi.
  if sy-subrc = 0 and zi < 6.
    read table ct_code index zi assigning .
                       = '* Modified:'.
    +20                = -day_string.
  endif.

  loop at ct_code assigning .

    clear: zf, zi.

*-- Handle TYPE indentation
    find first occurrence of regex '\btype\b' in  match offset zf.
    if zf > 0 and zf < 29.
      zi                     = 29 - zf.
      +29              = +zf.
      +zf(zi)          = zs.
      modify ct_code from .
    endif.

*-- Handle = indentation
    find first occurrence of regex '^\s*\S*\s*(=)[^=]*$' in .
    " start of line, blanks, alphas, blanks = no more \= end of line
    if sy-subrc = 0.
      find first occurrence of regex '=' in  match offset zf.

      if zf > 0 and zf < 29.
        zi                   = 29 - zf.
        +29            = +zf.
        +zf(zi)        = zs.
      endif.

      zs                     = 5.

    endif.

  endloop.

endfunction.

Monday, February 1, 2010

Text descriptions for object types

Where does SAP store the text for object descriptions in SE10 - TRE071X-OBJ_DESCRI ?

Not in a table. By analysing the search help SCTSOBJECT I found a form called GET_SYSTEM_TYPES in program SAPLTR_OBJECTS. Here all the object texts are hard coded and populated into an internal table with the KO100 structure.

Monday, March 23, 2009

How to print PDF forms from the SAP spooler

As of SAP Web Application Server 6.40, you can create a form in the new PDF-based form solution, which is integrated into the ABAP Workbench (SE80) and the SAP NetWeaver Developer Studio... more

Sunday, March 22, 2009

Embedding paint.exe in SAP GUI transactions

Here is some code to embed the paint application into a GUI application. Tablet signatures, anyone? However, my preferred approach is using a Flex components in web dynpros - much slicker but you will need EP1 for NW.



report zzzzzzzz.

include ole2incl.

selection-screen begin of screen 1100.
selection-screen end of screen 1100.

*----------------------------------------------------------------------*
* CLASS cl_paint DEFINITION
*----------------------------------------------------------------------*
class cl_paint definition.

public section.

methods:
constructor,

on_close_document
for event on_close_document of i_oi_document_proxy
importing document_proxy has_changed.

private section.

data:
go_control type ref to i_oi_container_control,
go_proxy type ref to i_oi_document_proxy.


endclass. "cl_paint DEFINITION

*----------------------------------------------------------------------*
* CLASS cl_paint IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class cl_paint implementation.

method constructor.

data:
retcode type soi_ret_string.

*-- Create the OLE control
call method c_oi_container_control_creator=>get_container_control
importing
control = go_control
retcode = retcode.
call method c_oi_errors=>raise_message
exporting
type = 'E'.

*-- Attach the OLE control to the screen container
call method go_control->init_control
exporting
r3_application_name = 'Signature Pad'(000)
inplace_enabled = 'X'
inplace_scroll_documents = 'X'
parent = cl_gui_container=>screen0
register_on_close_event = 'X'
register_on_custom_event = 'X'
importing
retcode = retcode.
call method c_oi_errors=>raise_message
exporting
type = 'E'.

*-- Create a proxy to the Paint application in the OLE container
call method go_control->get_document_proxy
exporting
document_type = 'Paint.Picture'
document_format = 'OLE'
importing
document_proxy = go_proxy
retcode = retcode.
if retcode ne c_oi_errors=>ret_ok.
exit.
endif.

*-- Create a new paint document
call method go_proxy->create_document
exporting
create_view_data = 'X'
open_inplace = 'X'
importing
retcode = retcode.
if retcode ne c_oi_errors=>ret_ok.
exit.
endif.

set handler me->on_close_document for go_proxy.

endmethod. "constructor


method on_close_document.

" Save the signature here

endmethod.

endclass. "cl_paint IMPLEMENTATION


start-of-selection.

data:
go_picture type ref to cl_paint.

create object go_picture.

call selection-screen 1100.