BAPI and JAVA Program to Interface Two System

I am working with a client, who wish to use both SAP and P3e.  The data from SAP will be send back to P3e. With respect to this I am working on field mapping betweem the 2 systems.

Develope Z Bapi using the standard BAPI and use jave program to interface these two system.  We are also working on the same and for your help, please find attached the ZBAPI developed.     *-- Waris
 

FUNCTION zbapi_project_maintain.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(WBS_ELEMENT) LIKE  BAPI_WBS_ELEMENT-WBS_ELEMENT
*"     VALUE(WBS_ELEMENT_DESCRIPTION) LIKE
*"        BAPI_PROJECT_DEFINITION-DESCRIPTION
*"     VALUE(WBS_ELEMENT1) LIKE  BAPI_WBS_ELEMENT-WBS_ELEMENT
*"     VALUE(PROJECT_DEFINITION) LIKE
*"        BAPI_WBS_ELEMENT-PROJECT_DEFINITION
*"     VALUE(DESCRIPTION) LIKE  BAPI_PROJECT_DEFINITION-DESCRIPTION
*"     VALUE(NETWORK) LIKE  BAPI_NETWORK-NETWORK
*"     VALUE(NETWORK1) LIKE  BAPI_NETWORK-NETWORK
*"     VALUE(NETWORK_TYPE) LIKE  BAPI_NETWORK-NETWORK_TYPE
*"     VALUE(PROFILE) LIKE  BAPI_NETWORK-PROFILE
*"     VALUE(ACTIVITY) LIKE  BAPI_NETWORK_ACTIVITY-ACTIVITY
*"     VALUE(CONTROL_KEY) LIKE  BAPI_NETWORK_ACTIVITY-CONTROL_KEY
*"     VALUE(ACT_DESC) LIKE  BAPI_NETWORK_ACTIVITY-DESCRIPTION
*"     VALUE(NETWORK_PREDECESSOR) LIKE
*"        BAPI_NETWORK_RELATION-NETWORK_PREDECESSOR
*"     VALUE(ACTIVITY_PREDECESSOR) LIKE
*"        BAPI_NETWORK_RELATION-ACTIVITY_PREDECESSOR
*"     VALUE(NETWORK_SUCCESSOR) LIKE
*"        BAPI_NETWORK_RELATION-NETWORK_SUCCESSOR
*"     VALUE(ACTIVITY_SUCCESSOR) LIKE
*"        BAPI_NETWORK_RELATION-ACTIVITY_SUCCESSOR
*"     VALUE(RELATION_TYPE) LIKE  BAPI_NETWORK_RELATION-RELATION_TYPE
*"     VALUE(ELEMENT) LIKE  BAPI_ACT_ELEMENT-ELEMENT
*"     VALUE(UP) LIKE  BAPI_WBS_HIERARCHIE-UP
*"     VALUE(DOWN) LIKE  BAPI_WBS_HIERARCHIE-DOWN
*"     VALUE(LEFT) LIKE  BAPI_WBS_HIERARCHIE-LEFT
*"     VALUE(RIGHT) LIKE  BAPI_WBS_HIERARCHIE-RIGHT
*"     VALUE(UP1) LIKE  BAPI_WBS_HIERARCHIE-UP
*"     VALUE(DOWN1) LIKE  BAPI_WBS_HIERARCHIE-DOWN
*"     VALUE(LEFT1) LIKE  BAPI_WBS_HIERARCHIE-LEFT
*"     VALUE(RIGHT1) LIKE  BAPI_WBS_HIERARCHIE-RIGHT
*"     VALUE(VPLANT) LIKE  BAPI_NETWORK-PLANT
*"     VALUE(VPROJ_TYPE) LIKE  BAPI_WBS_ELEMENT-PROJ_TYPE
*"  TABLES
*"      WBS_CREATE_ERROR STRUCTURE  BAPI_METH_MESSAGE OPTIONAL
*"      NETWORK_CREATE_ERROR STRUCTURE  BAPI_METH_MESSAGE OPTIONAL
*"      WBS_HIERARCHY_ERROR STRUCTURE  BAPI_METH_MESSAGE OPTIONAL
*"      ACTIVITY_CREATE_ERROR STRUCTURE  BAPI_METH_MESSAGE OPTIONAL
*"      ACTIVITY_RELATION_ERROR STRUCTURE  BAPI_METH_MESSAGE OPTIONAL
*"----------------------------------------------------------------------
  DATA : BEGIN OF itproj OCCURS 0.
          INCLUDE STRUCTURE bapi_project_definition.
  DATA :END OF itproj.
  DATA : BEGIN OF itprojupd OCCURS 0.
          INCLUDE STRUCTURE bapi_project_definition_up.
  DATA :END OF itprojupd.

  DATA : BEGIN OF itwbsmethod OCCURS 0.
          INCLUDE STRUCTURE bapi_method_project.
  DATA :END OF itwbsmethod.

  DATA : BEGIN OF itwbs OCCURS 0.
          INCLUDE STRUCTURE bapi_wbs_element.
  DATA :END OF itwbs.

  DATA : BEGIN OF itnetmethod OCCURS 0.
          INCLUDE STRUCTURE bapi_method_project.
  DATA :END OF itnetmethod.

  DATA : BEGIN OF itnetwork OCCURS 0.
          INCLUDE STRUCTURE bapi_network.
  DATA :END OF itnetwork.

  DATA : BEGIN OF itwbshierarchy OCCURS 0.
          INCLUDE STRUCTURE bapi_wbs_hierarchie.
  DATA :END OF itwbshierarchy.

  DATA : BEGIN OF iterr OCCURS 0.
          INCLUDE STRUCTURE bapi_meth_message .
  DATA :END OF iterr.

  DATA : iterr1 LIKE bapi_meth_message OCCURS 0 WITH HEADER LINE.
  DATA : itemsg LIKE bapi_meth_message OCCURS 0 WITH HEADER LINE.
  DATA : itemsg1 LIKE bapi_meth_message OCCURS 0 WITH HEADER LINE.
  DATA : itemsg2 LIKE bapi_meth_message OCCURS 0 WITH HEADER LINE.


  DATA : BEGIN OF itactivity OCCURS 0.
          INCLUDE STRUCTURE bapi_network_activity.
  DATA :END OF itactivity.

  DATA : BEGIN OF itactmethod OCCURS 0.
          INCLUDE STRUCTURE bapi_network_activity_up.
  DATA :END OF itactmethod.

  DATA  :BEGIN OF itrelation OCCURS 0.
          INCLUDE STRUCTURE bapi_network_relation.
  DATA :END OF itrelation.

  DATA : v_str(100) TYPE c.
  DATA : v_str1(34) TYPE c.


  IF NOT wbs_element IS INITIAL  AND
     NOT project_definition IS INITIAL.
*     AND NOT description IS INITIAL.


    MOVE wbs_element TO itwbs-wbs_element.
    MOVE project_definition TO itwbs-project_definition.
*    MOVE vproj_type TO itwbs-proj_type.
    MOVE WBS_ELEMENT_DESCRIPTION TO itwbs-description.
    IF NOT wbs_element1 IS INITIAL.
      MOVE vproj_type TO itwbs-proj_type.
    ELSE.
      MOVE ' ' TO itwbs-proj_type.
    ENDIF.

    APPEND itwbs.

    MOVE project_definition TO itproj-project_definition.

    MOVE description to itproj-description.
    APPEND itproj.

    MOVE '000010' TO itwbsmethod-refnumber.
    MOVE 'WBS-ELEMENT' TO itwbsmethod-objecttype.
    MOVE 'CREATE' TO itwbsmethod-method.
    MOVE  wbs_element TO itwbsmethod-objectkey.
    APPEND itwbsmethod.

    MOVE '000000' TO itwbsmethod-refnumber.
    MOVE ' ' TO itwbsmethod-objecttype.
    MOVE 'SAVE' TO itwbsmethod-method.
    MOVE  ' ' TO itwbsmethod-objectkey.

    APPEND itwbsmethod.


************ create WBS Elements******************
    REFRESH iterr.

    CALL FUNCTION 'BAPI_PROJECT_MAINTAIN'
         EXPORTING
              i_project_definition         = itproj
              i_project_definition_upd     = itprojupd
*    IMPORTING
*         RETURN                       =
         TABLES
              i_method_project             = itwbsmethod
*         I_WBS_ELEMENT_TABLE_UPDATE   =
              i_wbs_element_table          = itwbs
*         I_WBS_MILESTONE_TABLE        =
*         I_WBS_MILESTONE_TABLE_UPDATE =
*         I_WBS_HIERARCHIE_TABLE       = itwbshierarchy
*         i_network                    = itnetwork
*         I_NETWORK_UPDATE             =
*         i_activity                   = itactivity
*         i_activity_update            = itactmethod
*         I_RELATION                   =
*         I_RELATION_UPDATE            =
         e_message_table              = iterr.
*         I_ACTIVITY_ELEMENT           =
*         I_ACTIVITY_ELEMENT_UPDATE    =
*         I_ACTIVITY_MILESTONE         =
*         I_ACTIVITY_MILESTONE_UPDATE  =

    IF sy-subrc = 0.

    ENDIF.

    LOOP AT iterr.
      MOVE : iterr-method to wbs_create_error-method,
             iterr-object_type to wbs_create_error-OBJECT_TYPE,
             iterr-internal_object_id to
             wbs_create_error-INTERNAL_OBJECT_ID,

             iterr-external_object_id to
             wbs_create_error-EXTERNAL_OBJECT_ID,

             iterr-message_id to wbs_create_error-MESSAGE_ID,
             iterr-message_number to wbs_create_error-MESSAGE_NUMBER,
             iterr-message_type to wbs_create_error-MESSAGE_TYPE,
             iterr-message_text to wbs_create_error-MESSAGE_TEXT.
       append wbs_create_error.
       clear iterr.
     endloop.

      COMMIT WORK .
      WAIT UP TO 1 SECONDS.
    ENDIF. " if not wbs_element initial


************ create network ***********
 if not network is initial.

    MOVE '000001' TO itnetmethod-refnumber.
    MOVE 'NETWORK' TO itnetmethod-objecttype.
    MOVE 'CREATE' TO itnetmethod-method.
    MOVE  network TO itnetmethod-objectkey.
    APPEND itnetmethod.

    MOVE '000000' TO itnetmethod-refnumber.
    MOVE ' ' TO itnetmethod-objecttype.
    MOVE 'SAVE' TO itnetmethod-method.
    MOVE  ' ' TO itnetmethod-objectkey.

    APPEND itnetmethod.


    MOVE network TO itnetwork-network.
    MOVE network_type TO itnetwork-network_type.
    MOVE vplant TO itnetwork-plant.
    MOVE 'NEW NETWORK' TO itnetwork-short_text.
    MOVE wbs_element TO itnetwork-wbs_element.
    MOVE '001' TO itnetwork-mrp_controller.
    MOVE project_definition TO itnetwork-project_definition.
    MOVE profile TO itnetwork-profile.
    APPEND itnetwork.

      REFRESH itemsg.

      CALL FUNCTION 'BAPI_NETWORK_MAINTAIN'
*    IMPORTING
*         RETURN                      =
           TABLES
              i_method_project            = itnetmethod
              i_network                   =  itnetwork
*         I_NETWORK_UPDATE            =
*         I_ACTIVITY                  =
*         I_ACTIVITY_UPDATE           =
*         I_RELATION                  =
*         I_RELATION_UPDATE           =
           e_message_table             = itemsg.
*         I_ACTIVITY_ELEMENT          =
*         I_ACTIVITY_ELEMENT_UPDATE   =
*         I_ACTIVITY_MILESTONE        =
*         I_ACTIVITY_MILESTONE_UPDATE =


      IF sy-subrc = 0.

      ENDIF.

    LOOP AT itemsg.
      MOVE : itemsg-method to network_create_error-method,
             itemsg-object_type to network_create_error-OBJECT_TYPE,
             itemsg-internal_object_id to
             network_create_error-INTERNAL_OBJECT_ID,

             itemsg-external_object_id to
             network_create_error-EXTERNAL_OBJECT_ID,

             itemsg-message_id to network_create_error-MESSAGE_ID,
             itemsg-message_number to
             network_create_error-MESSAGE_NUMBER,

             itemsg-message_type to network_create_error-MESSAGE_TYPE,
             itemsg-message_text to network_create_error-MESSAGE_TEXT.
       append network_create_error.
       clear itemsg.
     endloop.


      COMMIT WORK .
      WAIT UP TO 1 SECONDS.
  endif.  "if not network is initial.


********* this method will decide hierarchy of WBS *******

   IF NOT wbs_element1 IS INITIAL.
   "AND NOT wbs_element_parent IS INITIAL.


      CLEAR:itwbsmethod,itwbshierarchy,itwbs,iterr1.

      REFRESH:itwbsmethod,itwbshierarchy,itwbs,iterr1.

*********MAINTAIN HIERARCHY*************************************
******* New WBS along with position w.r.t parent********
*      MOVE wbs_element_parent TO itwbshierarchy-wbs_element.
      MOVE wbs_element TO itwbshierarchy-wbs_element.
      MOVE project_definition TO itwbshierarchy-project_definition.
      MOVE up TO itwbshierarchy-up.
      MOVE down TO itwbshierarchy-down.
      MOVE left TO itwbshierarchy-left.
      MOVE right TO itwbshierarchy-right.
      APPEND itwbshierarchy.
      CLEAR itwbshierarchy.

********Pass WBS elements parent with position w.r.t child***
      MOVE wbs_element1 TO itwbshierarchy-wbs_element.
      MOVE project_definition TO itwbshierarchy-project_definition.
      MOVE up1 TO itwbshierarchy-up.
      MOVE down1 TO itwbshierarchy-down.
      MOVE left1 TO itwbshierarchy-left.
      MOVE right1 TO itwbshierarchy-right.
      APPEND itwbshierarchy.
      CLEAR itwbshierarchy.

******** Method for saving and creating WBS**********

      MOVE 'WBS-HIERARCHY' TO itwbsmethod-objecttype.
      MOVE 'CREATE' TO itwbsmethod-method.
      APPEND itwbsmethod.
      CLEAR itwbsmethod.

      MOVE 'SAVE' TO itwbsmethod-method.
      APPEND itwbsmethod.
      CLEAR itwbsmethod.

***********end method ****************

*****  WBS updating it with proj_type***
*      MOVE wbs_element_parent TO itwbs-wbs_element.
      MOVE wbs_element TO itwbs-wbs_element.
      MOVE project_definition TO itwbs-project_definition.
      MOVE WBS_ELEMENT_DESCRIPTION TO itwbs-description.
      MOVE vproj_type TO itwbs-proj_type.
      APPEND itwbs.
*****  WBS updating it with proj_type***


      CALL FUNCTION 'BAPI_PROJECT_MAINTAIN'
           EXPORTING
                i_project_definition         = itproj
                i_project_definition_upd     = itprojupd
*    IMPORTING
*         RETURN                       =
           TABLES
                i_method_project             = itwbsmethod
*         I_WBS_ELEMENT_TABLE_UPDATE   =
                i_wbs_element_table          = itwbs
*         I_WBS_MILESTONE_TABLE        =
*         I_WBS_MILESTONE_TABLE_UPDATE =
           i_wbs_hierarchie_table       = itwbshierarchy
*         I_NETWORK                    = itnetwork
*         I_NETWORK_UPDATE             =
*         I_ACTIVITY                   = itactivity
*         I_ACTIVITY_UPDATE            = itactmethod
*         I_RELATION                   =
*         I_RELATION_UPDATE            =
           e_message_table              = iterr1.
*         I_ACTIVITY_ELEMENT           =
*         I_ACTIVITY_ELEMENT_UPDATE    =
*         I_ACTIVITY_MILESTONE         =
*         I_ACTIVITY_MILESTONE_UPDATE  =

      IF sy-subrc = 0.

      ENDIF.


    LOOP AT iterr1.
      MOVE : iterr1-method to wbs_hierarchy_error-method,
             iterr1-object_type to wbs_hierarchy_error-OBJECT_TYPE,
             iterr1-internal_object_id to
             wbs_hierarchy_error-INTERNAL_OBJECT_ID,

             iterr1-external_object_id to
             wbs_hierarchy_error-EXTERNAL_OBJECT_ID,

             iterr1-message_id to wbs_hierarchy_error-MESSAGE_ID,
            iterr1-message_number to wbs_hierarchy_error-MESSAGE_NUMBER,
             iterr1-message_type to wbs_hierarchy_error-MESSAGE_TYPE,
             iterr1-message_text to wbs_hierarchy_error-MESSAGE_TEXT.
       append wbs_hierarchy_error.
       clear iterr1.
     endloop.


      COMMIT WORK .
      WAIT UP TO 1 SECONDS.          .


    ENDIF." if not wbs_element1 initial


**************** create activity in network************

    CLEAR : itactivity,itnetmethod,itemsg1,itnetwork.
    REFRESH :itactivity,itnetmethod,itemsg1,itnetwork.

    IF  NOT network IS INITIAL.

      CONCATENATE itemsg-internal_object_id activity INTO v_str.
      MOVE  itemsg-internal_object_id+0(12) TO itactivity-network.

    ELSE.
      CONCATENATE network1 activity INTO v_str.
      MOVE  network1 TO itactivity-network.

    ENDIF.

    MOVE activity TO itactivity-activity.
    MOVE act_desc TO itactivity-description.
    MOVE 'PS01' TO itactivity-control_key.
    MOVE project_definition TO itactivity-project_definition.
    MOVE wbs_element TO itactivity-wbs_element.
    MOVE 'W7' TO itactivity-factory_calendar.
    APPEND itactivity.

    MOVE '000001' TO itnetmethod-refnumber.
    MOVE 'NETWORKACTIVITY' TO itnetmethod-objecttype.
    MOVE 'CREATE' TO itnetmethod-method.
    MOVE  v_str TO itnetmethod-objectkey.

    APPEND itnetmethod.
    CLEAR  itnetmethod.


    MOVE 'SAVE' TO itnetmethod-method.

    APPEND itnetmethod.
    CLEAR  itnetmethod.

    MOVE network1 TO itnetwork-network.
    MOVE '001' TO itnetwork-mrp_controller.
    APPEND itnetwork.


    CALL FUNCTION 'BAPI_NETWORK_MAINTAIN'
*    IMPORTING
*         RETURN                      =
         TABLES
            i_method_project            = itnetmethod
            i_network                   =  itnetwork
*         I_NETWORK_UPDATE            =
         i_activity                  = itactivity
*         I_ACTIVITY_UPDATE           =
*         I_RELATION                  =
*         I_RELATION_UPDATE           =
         e_message_table             = itemsg1.
*         I_ACTIVITY_ELEMENT          =
*         I_ACTIVITY_ELEMENT_UPDATE   =
*         I_ACTIVITY_MILESTONE        =
*         I_ACTIVITY_MILESTONE_UPDATE =


    IF sy-subrc = 0.

    ENDIF.

    LOOP AT itemsg1.
      MOVE : itemsg1-method to activity_create_error-method,
             itemsg1-object_type to activity_create_error-OBJECT_TYPE,
             itemsg1-internal_object_id to
             activity_create_error-INTERNAL_OBJECT_ID,

             itemsg1-external_object_id to
             activity_create_error-EXTERNAL_OBJECT_ID,

             itemsg1-message_id to activity_create_error-MESSAGE_ID,
             itemsg1-message_number to
             activity_create_error-MESSAGE_NUMBER,

             itemsg1-message_type to activity_create_error-MESSAGE_TYPE,
             itemsg1-message_text to activity_create_error-MESSAGE_TEXT.
       append activity_create_error.
       clear itemsg1.
     endloop.



    COMMIT WORK .
    WAIT UP TO 1 SECONDS.

*********************** create network relationship*********************

    CLEAR : itnetmethod,itemsg2,itnetwork,itrelation.
    REFRESH :itnetmethod,itemsg2,itnetwork,itrelation.

    MOVE '000001' TO itnetmethod-refnumber.
    MOVE 'NETWORKRELATION' TO itnetmethod-objecttype.
    MOVE 'CREATE' TO itnetmethod-method.
    CONCATENATE relation_type network_predecessor activity_predecessor
                network_successor activity_successor INTO v_str1.
    MOVE  v_str1 TO itnetmethod-objectkey.

    APPEND itnetmethod.
    CLEAR  itnetmethod.


    MOVE 'SAVE' TO itnetmethod-method.

    APPEND itnetmethod.
    CLEAR  itnetmethod.


    MOVE network1 TO itnetwork-network.
    APPEND itnetwork.
    CLEAR itnetwork.

    MOVE network_predecessor TO itrelation-network_predecessor.
    MOVE  activity_predecessor TO itrelation-activity_predecessor.
    MOVE network_successor TO itrelation-network_successor.
    MOVE activity_successor TO itrelation-activity_successor.
    MOVE relation_type TO itrelation-relation_type.
    APPEND itrelation.
    CLEAR itrelation.

    CALL FUNCTION 'BAPI_NETWORK_MAINTAIN'
*    IMPORTING
*         RETURN                      =
     TABLES
        i_method_project            = itnetmethod
        i_network                   =  itnetwork
*         I_NETWORK_UPDATE            =
*         I_ACTIVITY                  = itactivity
*         I_ACTIVITY_UPDATE           =
     i_relation                  = itrelation
*         I_RELATION_UPDATE           =
     e_message_table             = itemsg2.
*         I_ACTIVITY_ELEMENT          =
*         I_ACTIVITY_ELEMENT_UPDATE   =
*         I_ACTIVITY_MILESTONE        =
*         I_ACTIVITY_MILESTONE_UPDATE =

    IF sy-subrc = 0.

    ENDIF.

    LOOP AT itemsg2.
      MOVE : itemsg2-method to activity_relation_error-method,
             itemsg2-object_type to activity_relation_error-OBJECT_TYPE,
             itemsg2-internal_object_id to
             activity_relation_error-INTERNAL_OBJECT_ID,

             itemsg2-external_object_id to
             activity_relation_error-EXTERNAL_OBJECT_ID,

             itemsg2-message_id to activity_relation_error-MESSAGE_ID,
             itemsg2-message_number to
             activity_relation_error-MESSAGE_NUMBER,

             itemsg2-message_type to
              activity_relation_error-MESSAGE_TYPE,

             itemsg2-message_text to
             activity_relation_error-MESSAGE_TEXT.

       append activity_relation_error.
       clear itemsg2.
     endloop.
.
    COMMIT WORK .
    WAIT UP TO 1 SECONDS.          .

  ENDFUNCTION.

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.