Skip to Content
avatar image
Former Member

Call Method from a ABAP Program

Hi all,

I wrote two programs.

Program 1

REPORT ztm_prog1.
TABLES: scarr.


SELECTION-SCREEN BEGIN OF BLOCK 001 WITH FRAME TITLE text-001.
SELECT-OPTIONS: carrid FOR scarr-carrid.
SELECTION-SCREEN END OF BLOCK 001.

CLASS program DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
    CLASS-METHODS initialization.
  PRIVATE SECTION.


ENDCLASS.
CLASS program IMPLEMENTATION.
  METHOD main.


  ENDMETHOD.
  METHOD initialization.
    CLEAR: carrid.
    carrid = VALUE #( low = 'LH' option = 'EQ' sign = 'I' ) .
    APPEND carrid TO carrid.
  ENDMETHOD.
ENDCLASS.


INITIALIZATION.
  program=>initialization( ).


START-OF-SELECTION.
  program=>main( ).

Program 2

REPORT ztm_prog2.


**** Call the method from Program 1
(ztm_prog1)program=>main( ). "How is the syntax ????

How can i call the static method main from program ztm_prog1?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Nov 16, 2016 at 01:09 PM

    You shouldn't do that but you can by using absolute type names.

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 17, 2016 at 04:59 AM

    You shouldn't code like that (as already told by the 3 other people)

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 16, 2016 at 01:18 PM

    If you are going to reuse those methods, all you have to do is move it into its own "real" class - that way both the original program and any other program can access the code the way they are supposed to.

    A short cut to make this simple (if you have the correct version of ABAP backend) is that in the Source-Code Based mode in SE24, you should be able to just cut and paste the entire class in there and just change the name of the class in the source code.

    Add comment
    10|10000 characters needed characters exceeded

    • OP doesn't state timeline of the first program written, it could already be in production. Either way OPer was not thinking ahead that the wonderful code in program 1 could be reused - in that I totally agree with you about OP needing to do their job!

  • Nov 19, 2016 at 11:01 AM
    -1

    Hello Thomas,

    note Horst's proposal does not trigger the INITIALIZATION event:

    CALL METHOD ('\PROGRAM=ZTM_PROG1\CLASS=PROGRAM')=>('MAIN').

    Further options

    1. extract the local class logic into a separate include and use it in both programs
    2. the simplest thing that could possibly work: SUBMIT
    SUBMIT ZTM_PROG1 WITH carrid = '?whatever?' AND RETURN.

    JNN

    Add comment
    10|10000 characters needed characters exceeded

    • Thomas' class has no class-constructor. 

      Yes, and I am suggesting that he do change the initialization to a constructor so that the common initial code does not have to be explicitly called.

      starting the report via SUBMIT 

      You still haven't considered if that is really an option - what if program_1 changes system data that is not wanted at the moment, or is part of a workflow, or worse still sends out a end user document or customer facing document? Would you put in checks in program 1 to prevent that from happening?

      about global against local classes (cf. public vs published). 

      The author is talking about code ownership and formalities around it, not about the programming mechanisms.

  • avatar image
    Former Member
    Nov 17, 2016 at 04:59 PM
    -4

    As others suggested in this thread, it is not recommended to code this way.

    But if you do not have any workaround( in cases like fixing other's code and cannot create a separate class etc), in ZTM_PROG1 have the PROGRAM=>MAIN() in a subroutine for e.g. PERFORM call_method and call this subroutine from ZTM_PROG2.

    Add comment
    10|10000 characters needed characters exceeded