Wednesday, August 4, 2010

Redefining a method of a view

If you need to redefine a method because you want to alter the standard behavior of the system, you should always first 'enhance the component and enhance the view'. When enhancing, the system generates enherited z-classes containing only inherited methods. How to redefine a view can be found here.

If you want to alter the coding of the standard, you can now redefine an inherited method. Redefining means that the system no longer calls the standard class, but your redefined class.

It is recommended to always call the standard coding either in the beginning of the redefined class or in the end, depending on what kind of alteration you are planning. This way, if the standard functionality changes, your code still calls this changed functionality.

The standard (inherited) method can always be called using ABAP coding:

CALL METHOD super->[NAME OF THE CURRENT METHOD]
    EXPORTING
        [IMPORTING PARAMETERS] = [IMPORTING PARAMETERS]
    IMPORTING
        [EXPORTING PARAMETERS] = [EXPORTING PARAMETERS]

Of course if there are no parameters, the importing and exporting parameters can be left out.

Example:

CALL METHOD super->do_prepare_output
    EXPORTING
        iv_first_time = iv_first_time.

This method calls the do_prepare_output of the parent class.

5 comments:

  1. Pieter can you help me with this. I need to set the default currency field for a sales quotation. The context node is btadminh. I have enhance the appropriate view component and context node. I'm pretty certain i have to redefine the get_currency method. So if I right click on this method for the currency field there is an option to redefine this method. Now at the top it has a comment about calling the super class...but I'm uncertain what parameters to pass to this function. More information and sample code can be found here http://forums.sdn.sap.com/Thread.jspa?Threadid=1868178&tstart=. But no one is able to help me. This blog post was the best I could find.

    ReplyDelete
  2. Pieter I got it..I ended up just uncommenting the code and I was able to put my code below it. This was an excellent post. Thank You Pieter. Here is the result of my code

    * First call super class
    CALL METHOD SUPER->GET_CURRENCY
    EXPORTING
    ATTRIBUTE_PATH = attribute_path
    ITERATOR = iterator
    RECEIVING
    VALUE = value.

    * Check the system language
    if value eq ''.
    IF sy-langu EQ 'E'.
    VALUE = 'USD'. "its English so default USD
    elseif sy-langu EQ 'D'.
    VALUE = 'EUR'. "its German so EUR
    ENDIF.
    endif.

    ReplyDelete
  3. Cool. Be aware though that this will probably have the side effect that the user will not be able to delete the value if he wants to (which of course might not be a problem in your case).

    Good to hear that you found a solution :-)

    ReplyDelete
  4. Pieter the default value is set, however when I save my quotation the CRM system saves it but it has one error in that error pop up that opens up on the web browser. The error is "please select a currency" but one is already defaulted. Maybe I need to call the set currency method that you mentioned in the other blog post??

    ReplyDelete
  5. This is resolved pieter see my other comment in your blog post. This blog has been fantastic help for me!

    ReplyDelete