Function module BAPI_PROJECT_MAINTAIN to maintain SAP project definition, WBS, Milestone

The BAPI_PROJECT_MAINTAIN BAPI function module allows you to replicate functionality of transaction CJ02. This is the change project transaction where you create project definitions that contain WBS elements.


You can then add milestones to each WBS element.


Anyway I struggled to find a full example of how this function module should be called so have created the below example ABAP report. This updates the project definition description, adds a new WBS element and add a milestone to a WBS. It's not everything you will need it to do but will allow you to see it working and give you a good idea how it all hangs together. Should then be able to extract everything else from the FM help documentation and will have an idea how to implement the following:

IMethodProject
OBJECTTYPE = WBS-Element
METHOD = Create
OBJECTKEY = TRAINING
REFNUMBER = 000001



--Below ABAP Report selection screen Parameters

p_proj
Enter a project definition. If this already exists in your SAP system it will update the description, if not it will create it filling in mandatory fields. This Replicates transactions CJ01(create) or CJ02(change) so if you are unsure of the mandatory fields create one via this CJ01.

p_addwbs
Check this to create a new WBS on project

p_newwbs
The WBS to be created on the above project. This must not already exist otherwise will return an error message saying it already exists.

p_addms
Check this to add a new milestone a WBS (one entered below)

p_mswbs
WBS element that exists on project. This is where the new milestone will be added.



--Example coding for updating Project Definition, Adding a WBS and Adding Milestones

*&---------------------------------------------------------------------*
*& Report  ZBAPI_EMPLOYEE_DEQUEUE
*&
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
REPORT  ZBAPI_EMPLOYEE_DEQUEUE.
data: wa_PROJDEF type  BAPI_PROJECT_DEFINITION_EX,
      wa_PROJDEF_edt type BAPI_PROJECT_DEFINITION,
      wa_projdef_upd type BAPI_PROJECT_DEFINITION_UP,
      wa_RETURN type  BAPIRETURN1,
      ld_projdef type BAPIPR-PROJECT_DEFINITION,
      ld_pspnr type proj-pspnr.
data: IT_IWBS_ELEMENT type STANDARD TABLE OF BAPI_WBS_ELEMENTS,
      IT_IWBS_ELEMENT_UPD type STANDARD TABLE OF BAPI_WBS_ELEMENT_UPDATE,
      wa_IWBS_ELEMENT_UPD like line of IT_IWBS_ELEMENT_UPD,
      IT_EWBS_ELEMENT type STANDARD TABLE OF  BAPI_WBS_ELEMENT_EXP,
      wa_EWBS_ELEMENT like line of IT_EWBS_ELEMENT,
      IT_EWBS_MILESTONE type STANDARD TABLE OF  BAPI_WBS_MILESTONE_EXP,
      WA_EWBS_MILESTONE LIKE LINE OF IT_EWBS_MILESTONE,
      IT_EWBS_MILESTONE_upd type STANDARD TABLE OF BAPI_WBS_MILESTONE_UPD,
      WA_EWBS_MILESTONE_upd LIKE LINE OF IT_EWBS_MILESTONE_upd,
      IT_EWBS_HIERARCHIE type STANDARD TABLE OF  BAPI_WBS_HIERARCHIE,
      IT_ACTIVITY type STANDARD TABLE OF  BAPI_NETWORK_ACTIVITY_EXP,
      IT_MESSAGE type STANDARD TABLE OF  BAPI_METH_MESSAGE,
      wa_message like line of it_message,
      it_projmethod type STANDARD TABLE OF BAPI_METHOD_PROJECT,
      wa_projmethod like line of it_projmethod..
data: IT_UWBS_ELEMENT type STANDARD TABLE OF BAPI_WBS_ELEMENT,
      wa_UWBS_ELEMENT like line of IT_UWBS_ELEMENT,
      IT_UWBS_ELEMENT_UPD type STANDARD TABLE OF BAPI_WBS_ELEMENT_UPDATE,
      wa_UWBS_ELEMENT_UPD like line of IT_UWBS_ELEMENT_UPD.
*Demonstrate BAPI to replicate transaction CJ02 functionality
parameter: p_proj type BAPIPR-PROJECT_DEFINITION default 'AA.XXXX.000002',     "project definition
           p_addwbs AS CHECKBOX DEFAULT 'X' ,
           p_newwbs type BAPI_WBS_ELEMENT-wbs_element default 'AA.XXXX.000001',"add new wbs to project
           p_addms   AS CHECKBOX DEFAULT 'X',
           p_mswbs type BAPI_WBS_ELEMENT-wbs_element default 'AA.XXXX.000002'. "add milestones to this wbs
ld_projdef = p_proj.
*Get current Project Definition details including WBS's and Milestones
CALL FUNCTION 'BAPI_PROJECT_GETINFO'
  EXPORTING
    PROJECT_DEFINITION     = ld_projdef
*   WITH_ACTIVITIES        =
    WITH_MILESTONES        = 'X'
*   WITH_SUBTREE           =
  IMPORTING
    E_PROJECT_DEFINITION   = wa_PROJDEF
    RETURN                 = wa_RETURN
  TABLES
    I_WBS_ELEMENT_TABLE    = IT_IWBS_ELEMENT
    E_WBS_ELEMENT_TABLE    = IT_EWBS_ELEMENT
    E_WBS_MILESTONE_TABLE  = IT_EWBS_MILESTONE
    E_WBS_HIERARCHIE_TABLE = IT_EWBS_HIERARCHIE
    E_ACTIVITY_TABLE       = IT_ACTIVITY
    E_MESSAGE_TABLE        = IT_MESSAGE.
MOVE-CORRESPONDING wa_PROJDEF TO wa_PROJDEF_edt.
*Could loop around WBS elements which already exist on Project Def (from BAPI_PROJECT_GETINFO)
*loop at IT_EWBS_ELEMENT into wa_EWBS_ELEMENT.
*MOVE-CORRESPONDING wa_EWBS_ELEMENT to wa_UWBS_ELEMENT.
**Ensure all mandatory fields are populated
wa_UWBS_ELEMENT-description = 'WBS Element Description'.
wa_UWBS_ELEMENT-wbs_element = p_newwbs.
wa_UWBS_ELEMENT-WBS_BILLING_ELEMENT       = 'X'.
APPEND wa_UWBS_ELEMENT to it_UWBS_ELEMENT.
*endloop.
REFRESH IT_EWBS_MILESTONE.
*You Could loop around WBS elements which already exist on Project Def (from BAPI_PROJECT_GETINFO)
*loop at IT_EWBS_MILESTONE into WA_EWBS_MILESTONE.
wa_EWBS_MILESTONE-MILESTONE_USAGE = '00001'. "get possible usages from milestone screen(see milestone screen image above)
"if creating, followign wbs can't already exist on the project definition
wa_EWBS_MILESTONE-wbs_element = p_mswbs.
wa_EWBS_MILESTONE-DESCRIPTION = 'Milestone Desc'.
APPEND WA_EWBS_MILESTONE  TO IT_EWBS_MILESTONE .
*endloop.
*Set Project Definition details
wa_projdef_upd-DESCRIPTION = 'X'.
*wa_projdef_upd-PROJECT_DEFINITION = 'X'.
MOVE-CORRESPONDING wa_projdef to wa_projdef_edt.
wa_projdef_edt-PROJECT_DEFINITION = ld_projdef.
wa_projdef_edt-DESCRIPTION = 'Project Def Description'.
*Convert project ID from its output format to its internal database storage format
 CALL FUNCTION 'CONVERSION_EXIT_KONPD_INPUT'
   EXPORTING
     INPUT           = ld_projdef "output format
  IMPORTING
    OUTPUT          = ld_pspnr
  EXCEPTIONS
    NOT_FOUND       = 1
    OTHERS          = 2.
* Check if Project definition already exists
select single pspnr
  into ld_pspnr
  from proj
 where pspnr eq ld_pspnr.
if sy-subrc ne 0.
  wa_projdef_edt-PROJECT_PROFILE = 'ZRG0002'.
  wa_projdef_edt-RESPONSIBLE_NO  = '00010011'.
  wa_projdef_edt-APPLICANT_NO    = '00000001'.
  wa_projdef_edt-PROFIT_CTR   = 'RGMECH'.
*Set BAPI trigger to CREATE Project Definition
  wa_projmethod-OBJECTTYPE =  'ProjectDefinition'.
  wa_projmethod-METHOD = 'Create'.  "''Update'
  wa_projmethod-OBJECTKEY = ld_projdef.
  wa_projmethod-REFNUMBER = '000001'.
  append wa_projmethod to it_projmethod.
else.
*Set BAPI trigger to UPDATE Project Definition
  wa_projmethod-OBJECTTYPE =  'ProjectDefinition'.
  wa_projmethod-METHOD = 'Update'.
  wa_projmethod-OBJECTKEY = ld_projdef.
  wa_projmethod-REFNUMBER = '000001'.
  append wa_projmethod to it_projmethod.
endif.
* Set BAPI Trigger to create WBS based on WBS table passed
if p_addwbs eq 'X'.
  wa_projmethod-objecttype = 'WBS-Element'.
  wa_projmethod-method = 'create'.
  wa_projmethod-objectkey = p_newwbs.  "can be anything
  wa_projmethod-refnumber = '000001'.
  APPEND wa_projmethod TO it_projmethod.
endif.
* Set BAPI Trigger to create WBS-Milestones based on Milestone table passed
if p_addms eq 'X'.
  wa_projmethod-objecttype = 'WBS-Milestone'.
  wa_projmethod-method = 'Create'.
  clear: wa_projmethod-objectkey.
  wa_projmethod-refnumber = '000001'.
  APPEND wa_projmethod TO it_projmethod.
endif.
* Set BAPI Trigger to save and wait
CLEAR wa_projmethod.
wa_projmethod-method = 'SaveAndWait'.
APPEND wa_projmethod TO it_projmethod.
*Call BAPI to update Project Definition
CALL FUNCTION 'BAPI_PROJECT_MAINTAIN'
  EXPORTING
    I_PROJECT_DEFINITION         = wa_PROJDEF_EDT
    I_PROJECT_DEFINITION_UPD     = wa_PROJDEF_UPD
  IMPORTING
    RETURN                       = wa_RETURN
  TABLES
    I_METHOD_PROJECT             = it_projmethod
*   I_WBS_ELEMENT_TABLE_UPDATE   = IT_UWBS_ELEMENT_upd
    I_WBS_ELEMENT_TABLE          = IT_UWBS_ELEMENT
    I_WBS_MILESTONE_TABLE        = IT_EWBS_MILESTONE
*   I_WBS_MILESTONE_TABLE_UPDATE = IT_EWBS_MILESTONE_upd
*   I_WBS_HIERARCHIE_TABLE       = IT_EWBS_HIERARCHIE
**   I_NETWORK                    =
**   I_NETWORK_UPDATE             =
**   I_ACTIVITY                   =
**   I_ACTIVITY_UPDATE            =
**   I_RELATION                   =
**   I_RELATION_UPDATE            =
    E_MESSAGE_TABLE              = it_message
**   I_ACTIVITY_ELEMENT           =
**   I_ACTIVITY_ELEMENT_UPDATE    =
**   I_ACTIVITY_MILESTONE         =
*   *                            =
*   I_ACTIVITY_MILESTONE_UPDATE  =
  .
loop at it_message into wa_message.
  write:/ wa_message-MESSAGE_TEXT.
endloop.


Related Articles

BAPI & RFC in SAP ABAP Development Info and Example code for calling BAPI's and remote function calls
SAP Remote Function Call using the RFC destination parameter