ABAP BAPI 复制标准项目模板实现项目立项

一、复制标准项目模板实现项目立项

因为CJ20N录屏存在困难,所以想通过BDC实现复制项目模板创建项目立项行不通,因此需要通过BAPI解决。

因为项目立项包含:项目定义、WBS、网络、作业,因此需要分别调用四个BAPI实现该功能

二、CJ01 项目定义与WBS

输入项目定义编号和参数文件

 传入必输字段,点击WBS元素总览

 

 输入WBS层级,点击保存即可

创建项目BAPI

"-----------------------------@斌将军-----------------------------
DATA:gs_proj_bus2001_new TYPE bapi_bus2001_new,
     gt_bapiret2         TYPE TABLE OF bapiret2,
     gs_bapiret2         TYPE bapiret2.

gs_proj_bus2001_new-project_definition = 'NPD-2022037'."is_proj-pspid.       "项目编号
gs_proj_bus2001_new-description        = '测试'."is_proj-post1.       "PS: 短描述 (第一行文本)
gs_proj_bus2001_new-responsible_no     = '80000667'."lv_vernr.            "项目经理编号
gs_proj_bus2001_new-applicant_no     = '81000003'."lv_vernr.            "申请部门编号
gs_proj_bus2001_new-start              = '20220225'."is_proj-plfaz.       "开始日期
gs_proj_bus2001_new-finish             = '20220428'."is_proj-plsez.       "完成日期
gs_proj_bus2001_new-company_code       = '2000'."is_proj-vbukr.       "项目的公司代码
gs_proj_bus2001_new-profit_ctr         = ''."is_proj-prctr.       "利润中心
gs_proj_bus2001_new-project_profile    = 'APS0001'."is_proj-profl.       "项目参数文件
gs_proj_bus2001_new-plant              = '2000'."is_proj-werks.       "工厂
*IF is_proj-func_area IS NOT INITIAL.
*  gs_proj_bus2001_new-func_area        = is_proj-func_area.   "功能控制范围
*ENDIF.

CALL FUNCTION 'BAPI_PS_INITIALIZATION'.

CALL FUNCTION 'BAPI_BUS2001_CREATE'
  EXPORTING
    i_project_definition = gs_proj_bus2001_new
  TABLES
    et_return            = gt_bapiret2.


LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
ENDLOOP.

IF sy-subrc EQ 0.
  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ENDIF.
CHECK sy-subrc NE 0.
CALL FUNCTION 'BAPI_PS_PRECOMMIT'
  TABLES
    et_return = gt_bapiret2.

LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
ENDLOOP.

IF sy-subrc EQ 0.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
    IMPORTING
      return = gs_bapiret2.

  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.

  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = gs_bapiret2.
*   项目定义创建成功
ENDIF.
"-----------------------------@斌将军-----------------------------

创建WBS的BAPI

"-----------------------------@斌将军-----------------------------
DATA:lv_pspid           TYPE bapi_bus2001_new-project_definition,
     gt_wbs_bus2054_new TYPE TABLE OF bapi_bus2054_new,
     gs_wbs_bus2054_new TYPE bapi_bus2054_new,
     gt_bapiret2        TYPE TABLE OF bapiret2,
     gs_bapiret2        TYPE bapiret2.

CLEAR:gs_wbs_bus2054_new.
gs_wbs_bus2054_new-wbs_element                    = 'NPD-2022038'."gs_wbs-posid.     "工作分解结构元素 (WBS 元素)
gs_wbs_bus2054_new-description                    = '第一层'."gs_wbs-post1.     "PS: 短描述 (第一行文本)
gs_wbs_bus2054_new-responsible_no                 = '80000667'."lv_vernr.         "项目经理编号
*gs_wbs_bus2054_new-profit_ctr                     = ''."gs_wbs-prctr.     "利润中心
gs_wbs_bus2054_new-proj_type                      = '11'."gs_wbs-prart.     "项目类型
gs_wbs_bus2054_new-wbs_account_assignment_element = 'X'."gs_wbs-belkz.     "标志:科目分配元素
*gs_wbs_bus2054_new-wbs_billing_element            = ''."gs_wbs-fakkz.     "标识符: 开票元素
gs_wbs_bus2054_new-wbs_left                       = ''."gs_wbs-left_posid."左侧WBS
gs_wbs_bus2054_new-wbs_up                         = ''."gs_wbs-up_posid.  "上级WBS
*gs_wbs_bus2054_new-user_field_char20_1            = ''."gs_wbs-usr00.     "业务类别
*gs_wbs_bus2054_new-user_field_char20_2            = ''."gs_wbs-usr01.     "CRM 商机编码
*gs_wbs_bus2054_new-user_field_char10_1            = ''."'X'.              "产品
*gs_wbs_bus2054_new-statistical                    = ''."gs_wbs-xstat.     "统计
*gs_wbs_bus2054_new-wbs_cctr_posted_actual         = ''."gs_wbs-kostl.     "CCtr 过账
APPEND gs_wbs_bus2054_new TO gt_wbs_bus2054_new.

CLEAR:gs_wbs_bus2054_new.
gs_wbs_bus2054_new-wbs_element                    = 'NPD-2022038.001'."gs_wbs-posid.     "工作分解结构元素 (WBS 元素)
gs_wbs_bus2054_new-description                    = '第二层'."gs_wbs-post1.     "PS: 短描述 (第一行文本)
gs_wbs_bus2054_new-responsible_no                 = '80000667'."lv_vernr.         "项目经理编号
*gs_wbs_bus2054_new-profit_ctr                     = ''."gs_wbs-prctr.     "利润中心
gs_wbs_bus2054_new-proj_type                      = '11'."gs_wbs-prart.     "项目类型
gs_wbs_bus2054_new-wbs_account_assignment_element = 'X'."gs_wbs-belkz.     "标志:科目分配元素
*gs_wbs_bus2054_new-wbs_billing_element            = ''."gs_wbs-fakkz.     "标识符: 开票元素
gs_wbs_bus2054_new-wbs_left                       = ''."gs_wbs-left_posid."左侧WBS
gs_wbs_bus2054_new-wbs_up                         = 'NPD-2022038'."gs_wbs-up_posid.  "上级WBS
*gs_wbs_bus2054_new-user_field_char20_1            = ''."gs_wbs-usr00.     "业务类别
*gs_wbs_bus2054_new-user_field_char20_2            = ''."gs_wbs-usr01.     "CRM 商机编码
*gs_wbs_bus2054_new-user_field_char10_1            = ''."'X'.              "产品
*gs_wbs_bus2054_new-statistical                    = ''."gs_wbs-xstat.     "统计
*gs_wbs_bus2054_new-wbs_cctr_posted_actual         = ''."gs_wbs-kostl.     "CCtr 过账
APPEND gs_wbs_bus2054_new TO gt_wbs_bus2054_new.

CLEAR:gs_wbs_bus2054_new.
gs_wbs_bus2054_new-wbs_element                    = 'NPD-2022038.002'."gs_wbs-posid.     "工作分解结构元素 (WBS 元素)
gs_wbs_bus2054_new-description                    = '第三层'."gs_wbs-post1.     "PS: 短描述 (第一行文本)
gs_wbs_bus2054_new-responsible_no                 = '80000667'."lv_vernr.         "项目经理编号
*gs_wbs_bus2054_new-profit_ctr                     = ''."gs_wbs-prctr.     "利润中心
gs_wbs_bus2054_new-proj_type                      = '11'."gs_wbs-prart.     "项目类型
gs_wbs_bus2054_new-wbs_account_assignment_element = 'X'."gs_wbs-belkz.     "标志:科目分配元素
*gs_wbs_bus2054_new-wbs_billing_element            = ''."gs_wbs-fakkz.     "标识符: 开票元素
*gs_wbs_bus2054_new-wbs_left                       = 'NPD-2022037.001'."gs_wbs-left_posid."左侧WBS
gs_wbs_bus2054_new-wbs_up                         = 'NPD-2022038'."gs_wbs-up_posid.  "上级WBS
*gs_wbs_bus2054_new-user_field_char20_1            = ''."gs_wbs-usr00.     "业务类别
*gs_wbs_bus2054_new-user_field_char20_2            = ''."gs_wbs-usr01.     "CRM 商机编码
*gs_wbs_bus2054_new-user_field_char10_1            = ''."'X'.              "产品
*gs_wbs_bus2054_new-statistical                    = ''."gs_wbs-xstat.     "统计
*gs_wbs_bus2054_new-wbs_cctr_posted_actual         = ''."gs_wbs-kostl.     "CCtr 过账
APPEND gs_wbs_bus2054_new TO gt_wbs_bus2054_new.

lv_pspid = 'NPD-2022038'.

CALL FUNCTION 'BAPI_PS_INITIALIZATION'.

CALL FUNCTION 'BAPI_BUS2054_CREATE_MULTI'
  EXPORTING
    i_project_definition = lv_pspid
  TABLES
    it_wbs_element       = gt_wbs_bus2054_new
    et_return            = gt_bapiret2.

LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AE'.
ENDLOOP.

IF sy-subrc EQ 0.

  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.

  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.

ENDIF.
CHECK sy-subrc NE 0.
CALL FUNCTION 'BAPI_PS_PRECOMMIT'
  TABLES
    et_return = gt_bapiret2.

LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AE'.
ENDLOOP.

IF sy-subrc EQ 0.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
    IMPORTING
      return = gs_bapiret2.

  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.

  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = gs_bapiret2.
ENDIF.
"-----------------------------@斌将军-----------------------------

 三、CN21创建网络和作业

分配给WBS,点击作业

 

创建作业,分配给WBS,点击保存即可

创建网络BAPI

"-----------------------------@斌将军-----------------------------
DATA:gs_net_bus2002_new TYPE bapi_bus2002_new,
     gt_bapiret2        TYPE TABLE OF bapiret2,
     gs_bapiret2        TYPE bapiret2.

CLEAR : gs_net_bus2002_new.
gs_net_bus2002_new-project_definition = 'NPD-2022037'."is_network-pspid.       "项目定义
gs_net_bus2002_new-wbs_element        = 'NPD-2022037'."is_network-posid.       "工作分解结构元素 (WBS 元素)
gs_net_bus2002_new-short_text         = '网络测试'."is_network-ktext.       "描述
*  gs_net_bus2002_new-plant              = ''."is_network-werks.       "工厂
*  gs_net_bus2002_new-mrp_controller     = ''."is_network-dispo.       "MRP控制者
CALL FUNCTION 'BAPI_PS_INITIALIZATION'.
CALL FUNCTION 'BAPI_BUS2002_CREATE'
  EXPORTING
    i_network = gs_net_bus2002_new
  TABLES
    et_return = gt_bapiret2.
LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.

ENDLOOP.
IF sy-subrc EQ 0.
  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ENDIF.
CHECK sy-subrc NE 0.
CALL FUNCTION 'BAPI_PS_PRECOMMIT'
  TABLES
    et_return = gt_bapiret2.
LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.

ENDLOOP.
IF sy-subrc EQ 0.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
    IMPORTING
      return = gs_bapiret2.
  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = gs_bapiret2.
  READ TABLE gt_bapiret2 INTO gs_bapiret2 WITH KEY id = 'CNIF_PI'
                                                   number = '003'.
  IF sy-subrc EQ 0.
*    ev_aufnr = gs_bapiret2-message_v2.
  ENDIF.
ENDIF.
"-----------------------------@斌将军-----------------------------

创建作业BAPI

"-----------------------------@斌将军-----------------------------

DATA:lv_aufnr TYPE bapi_network_list-network.
DATA:gt_act_bus2002_new TYPE TABLE OF bapi_bus2002_act_new,
     gs_act_bus2002_new TYPE bapi_bus2002_act_new,
     gt_bapiret2        TYPE TABLE OF bapiret2,
     gs_bapiret2        TYPE bapiret2.

CLEAR:gs_act_bus2002_new.
gs_act_bus2002_new-activity           = '0010'."gs_activity-vornr.       "在网络和库网络中的作业编号
gs_act_bus2002_new-description        = '封装测试'."gs_activity-ltxa1.       "工序短文本
gs_act_bus2002_new-control_key        = ''."gs_activity-steus.       "控制码-内协(ZPS1)和成本(ZPS3)
gs_act_bus2002_new-wbs_element        = 'NPD-2022037.001'."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-duration_normal        = 2."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-work_cntr          = ''."gs_activity-arbpl.       "工作中心
gs_act_bus2002_new-work_activity      = ''."gs_activity-arbei.       "工作(时间单位H)
*-------------MOD ON 20200423--------Begin-----------------------------------
**    gs_act_bus2002_new-calculation_key    = gs_activity-indet.       "用于计算的码
*IF gs_activity-steus = 'ZPS1' AND gs_activity-arbei IS INITIAL.
*  gs_act_bus2002_new-calculation_key    = 0.       "用于计算的码
*ELSE.
*  gs_act_bus2002_new-calculation_key    = gs_activity-indet.       "用于计算的码
*ENDIF.
*-------------MOD ON 20200423--------End-------------------------------------
*gs_act_bus2002_new-acttype            = gs_activity-larnt.       "活动类型
*gs_act_bus2002_new-cost_elem          = gs_activity-sakto.       "成本要素编码
*gs_act_bus2002_new-profit_ctr         = gs_activity-prctr.       "利润中心
*IF gs_activity-steus EQ 'ZPS3'.
*  gs_act_bus2002_new-activity_costs   = gs_activity-prkst.       "作业中的成本
**    ELSE.
**      gs_act_bus2002_new-user_field_curr1 = gs_activity-prkst.       "用户字段 - 值3
*ENDIF.

APPEND gs_act_bus2002_new TO gt_act_bus2002_new.

CLEAR:gs_act_bus2002_new.
gs_act_bus2002_new-activity           = '0020'."gs_activity-vornr.       "在网络和库网络中的作业编号
gs_act_bus2002_new-description        = '项目产品设计'."gs_activity-ltxa1.       "工序短文本
gs_act_bus2002_new-control_key        = ''."gs_activity-steus.       "控制码-内协(ZPS1)和成本(ZPS3)
gs_act_bus2002_new-wbs_element        = 'NPD-2022037.001'."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-duration_normal        = 2."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-work_cntr          = ''."gs_activity-arbpl.       "工作中心
gs_act_bus2002_new-work_activity      = ''."gs_activity-arbei.       "工作(时间单位H)
APPEND gs_act_bus2002_new TO gt_act_bus2002_new.

CLEAR:gs_act_bus2002_new.
gs_act_bus2002_new-activity           = '0030'."gs_activity-vornr.       "在网络和库网络中的作业编号
gs_act_bus2002_new-description        = '项目立项'."gs_activity-ltxa1.       "工序短文本
gs_act_bus2002_new-control_key        = ''."gs_activity-steus.       "控制码-内协(ZPS1)和成本(ZPS3)
gs_act_bus2002_new-wbs_element        = 'NPD-2022037.002'."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-duration_normal        = 2."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-work_cntr          = ''."gs_activity-arbpl.       "工作中心
gs_act_bus2002_new-work_activity      = ''."gs_activity-arbei.       "工作(时间单位H)
APPEND gs_act_bus2002_new TO gt_act_bus2002_new.

CLEAR:gs_act_bus2002_new.
gs_act_bus2002_new-activity           = '0040'."gs_activity-vornr.       "在网络和库网络中的作业编号
gs_act_bus2002_new-description        = '项目评估'."gs_activity-ltxa1.       "工序短文本
gs_act_bus2002_new-control_key        = ''."gs_activity-steus.       "控制码-内协(ZPS1)和成本(ZPS3)
gs_act_bus2002_new-wbs_element        = 'NPD-2022037.002'."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-duration_normal        = 2."gs_activity-posid.       "工作分解结构元素 (WBS 元素)
gs_act_bus2002_new-work_cntr          = ''."gs_activity-arbpl.       "工作中心
gs_act_bus2002_new-work_activity      = ''."gs_activity-arbei.       "工作(时间单位H)
APPEND gs_act_bus2002_new TO gt_act_bus2002_new.

CALL FUNCTION 'BAPI_PS_INITIALIZATION'.
lv_aufnr = '4000088'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  = lv_aufnr
  IMPORTING
    output = lv_aufnr.

CALL FUNCTION 'BAPI_BUS2002_ACT_CREATE_MULTI'
  EXPORTING
    i_number    = lv_aufnr
  TABLES
    it_activity = gt_act_bus2002_new
    et_return   = gt_bapiret2.

LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AE'.
ENDLOOP.

IF sy-subrc EQ 0.
  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ENDIF.

CHECK sy-subrc NE 0.
CALL FUNCTION 'BAPI_PS_PRECOMMIT'
  TABLES
    et_return = gt_bapiret2.
LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AE'.
ENDLOOP.

IF sy-subrc EQ 0.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
    IMPORTING
      return = gs_bapiret2.

  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    IMPORTING
      return = gs_bapiret2.
ENDIF.
"-----------------------------@斌将军-----------------------------

 四、代码整合

通过整合四个BAPI,实现复制标准模板完成项目立项过程

"-----------------------------@斌将军-----------------------------
FUNCTION zps01_project_create.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(I_INPUT) TYPE  ZSPS_CJXM
*"  EXPORTING
*"     VALUE(E_RETURN) TYPE  BAPIRETURN
*"----------------------------------------------------------------------

  DATA:gs_proj_bus2001_new TYPE bapi_bus2001_new,
       gt_wbs_bus2054_new  TYPE TABLE OF bapi_bus2054_new,
       gs_wbs_bus2054_new  TYPE bapi_bus2054_new,
       gs_net_bus2002_new  TYPE bapi_bus2002_new,
       gt_act_bus2002_new  TYPE TABLE OF bapi_bus2002_act_new,
       gs_act_bus2002_new  TYPE bapi_bus2002_act_new,
       gt_bapiret2         TYPE TABLE OF bapiret2,
       gs_bapiret2         TYPE bapiret2.

  DATA:lv_new_pspid TYPE proj-pspid,
       lv_bz_pspid  TYPE projs-pspid,
       lv_ctumode   TYPE ctu_params-dismode,
       lv_update    TYPE ctu_params-updmode,
       lv_aufnr     TYPE bapi_network_list-network,
       lv_bzxm      TYPE proj-pspid,
       lv_prart     TYPE char10,
       lv_wtges     TYPE char17,
       lv_bzwbs     TYPE ps_posid,
*       lv_profl     TYPE proj-profl,
       lv_number    TYPE numc3,
       lv_check     TYPE char1,
       ls_return    TYPE bapireturn,
       lv_message   TYPE char200,
       lv_msg       TYPE char200.

  IF i_input-pspid IS INITIAL.
    e_return-type = 'E'.
    e_return-message = '标准项目编码为空'.
    RETURN.
  ENDIF.

  IF i_input-vbukr IS INITIAL.
    e_return-type = 'E'.
    e_return-message = '公司代码为空'.
    RETURN.
  ENDIF.

  "获取项目类型相关配置信息
  SELECT SINGLE
    *
  FROM zpst_prart
  INTO @DATA(ls_prart)
  WHERE bukrs = @i_input-vbukr
    AND zprart = @i_input-prart.

  "获取项目经理与OA账号关联表数据
  SELECT SINGLE
    vernr,
    verna
  FROM tcj04
  INTO @DATA(ls_cj04)
  WHERE verna = @i_input-usr00.

  IF ls_cj04 IS INITIAL.
    e_return-type = 'E'.
    e_return-message = '项目经理' && i_input-usr01 && '不存在'.
    RETURN.
  ENDIF.

  CLEAR:lv_bz_pspid.
  lv_bz_pspid = i_input-pspid."标准模板项目定义编号
  CALL FUNCTION 'CONVERSION_EXIT_ABPSN_INPUT'
    EXPORTING
      input  = lv_bz_pspid
    IMPORTING
      output = lv_bz_pspid.

  "获取相同项目类型,最大的项目编号
  CLEAR:lv_prart.
  lv_prart = i_input-prart && sy-datum+0(4) && '%'.
  CLEAR:lv_new_pspid.
  SELECT
  MAX( pspid )
  FROM proj
  INTO lv_new_pspid
  WHERE pspid LIKE lv_prart.

  IF lv_new_pspid IS INITIAL.
    lv_number = 1.
  ELSE.
    lv_number = lv_new_pspid+7(3).
    lv_number = lv_number + 1.
  ENDIF.

  lv_new_pspid = i_input-prart && '-' && sy-datum+0(4) && lv_number."要创建的项目定义编号

*  CLEAR:lv_profl.
*  lv_profl = ."项目参数文件
*  CASE i_input-prart.
*    WHEN 'NPD' OR 'NPI' OR 'RDT' OR 'AUT' OR 'TRY'.
*      lv_profl = '0001'.
*    WHEN 'TEH' OR 'EXP' OR 'ITA'.
*      lv_profl = '0002'.
*    WHEN 'COS'.
*      lv_profl = '0003'.
*    WHEN 'FAC'.
*      lv_profl = '0004'.
*    WHEN OTHERS.
*  ENDCASE.
*  IF i_input-vbukr = '1000'.
*    lv_profl = 'LRC' && lv_profl.
*  ELSEIF i_input-vbukr = '2000'.
*    lv_profl = 'APS' && lv_profl."项目参数文件
*  ENDIF.

  SELECT SINGLE
    *
  FROM projs
  INTO @DATA(ls_projs)
  WHERE pspid = @lv_bz_pspid.

  IF sy-subrc EQ 0.
    "--------------------------------创建项目--------------------------------------
    CLEAR:gs_proj_bus2001_new.
    gs_proj_bus2001_new-project_definition = lv_new_pspid.       "项目编号
    gs_proj_bus2001_new-description        = i_input-post1.       "PS: 短描述 (第一行文本)
    gs_proj_bus2001_new-responsible_no     = ls_cj04-vernr.            "项目经理编号
    gs_proj_bus2001_new-applicant_no     = ls_projs-astnr."lv_vernr.            "申请部门编号
    gs_proj_bus2001_new-start              = i_input-plfaz.       "开始日期
    gs_proj_bus2001_new-finish             = i_input-plsez.       "完成日期
*      gs_proj_bus2001_new-company_code       = '2000'."is_proj-vbukr.       "项目的公司代码
    gs_proj_bus2001_new-profit_ctr         = ''."is_proj-prctr.       "利润中心
    gs_proj_bus2001_new-project_profile    = ls_prart-profl."is_proj-profl.       "项目参数文件
*      gs_proj_bus2001_new-plant              = '2000'."is_proj-werks.       "工厂

    "初始化
    CALL FUNCTION 'BAPI_PS_INITIALIZATION'.

    "创建项目
    REFRESH:gt_bapiret2.
    CALL FUNCTION 'BAPI_BUS2001_CREATE'
      EXPORTING
        i_project_definition = gs_proj_bus2001_new
      TABLES
        et_return            = gt_bapiret2.

    CLEAR:lv_check,lv_message.
    LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
      lv_check = 'E'.
      lv_message = lv_message && gs_bapiret2-message.
      CLEAR:gs_bapiret2.
    ENDLOOP.

    IF lv_check = 'E'.
      "错误后初始化
      CALL FUNCTION 'ZPS01_PS_FLAG'.

      e_return-type = 'E'.
      e_return-message = lv_message.
      RETURN.
    ELSE.
      CLEAR:ls_return.
      "成功时提交
      CALL FUNCTION 'ZPS01_PS_PRECOMMIT'
        IMPORTING
          e_return = ls_return.
      IF ls_return-type = 'E'.
        e_return-type = 'E'.
        e_return-message = ls_return-message.
        RETURN.
      ENDIF.

      "--------------------------------创建WBS--------------------------------------
      SELECT
        pspnr,"标准 WBS: 内部项目号 (具有退出(根据 ID ))
        posid,"标准项目中的 WBS 元素
        post1,"PS: 短描述 (第一行文本)
        psphi,"当前的内部项目号 - 标准 WBS
        stufe"项目层次的等级
      FROM prpss
      INTO TABLE @DATA(lt_prpss)
      WHERE psphi = @ls_projs-pspnr.

      SORT lt_prpss BY pspnr.

      IF lt_prpss IS NOT INITIAL.
        LOOP AT lt_prpss INTO DATA(ls_prpss).
          CLEAR:gs_wbs_bus2054_new.
          IF sy-tabix = 1.
            gs_wbs_bus2054_new-wbs_element = lv_new_pspid.
            gs_wbs_bus2054_new-user_field_char20_1 = i_input-usr00.
            gs_wbs_bus2054_new-description                    = i_input-post1."ls_prpss-post1."PS: 短描述 (第一行文本)
          ELSE.
            gs_wbs_bus2054_new-wbs_element = lv_new_pspid && '.' && ls_prpss-posid+10(3)."'NPD-2022037' '.' '001'"工作分解结构元素 (WBS 元素)
            gs_wbs_bus2054_new-wbs_up                         = lv_new_pspid."gs_wbs-up_posid.  "上级WBS
            gs_wbs_bus2054_new-description                    = ls_prpss-post1."PS: 短描述 (第一行文本)
          ENDIF.

          gs_wbs_bus2054_new-responsible_no                 = ls_cj04-vernr."项目经理编号
          "gs_wbs_bus2054_new-profit_ctr                     = ''."gs_wbs-prctr.     "利润中心
          gs_wbs_bus2054_new-proj_type                      = ls_prart-prart.     "项目类型
          gs_wbs_bus2054_new-wbs_account_assignment_element = 'X'."gs_wbs-belkz.     "标志:科目分配元素
          "gs_wbs_bus2054_new-wbs_billing_element            = ''."gs_wbs-fakkz.     "标识符: 开票元素
          gs_wbs_bus2054_new-wbs_left                       = ''."gs_wbs-left_posid."左侧WBS
          "gs_wbs_bus2054_new-user_field_char20_1            = ''."gs_wbs-usr00.     "业务类别
          "gs_wbs_bus2054_new-user_field_char20_2            = ''."gs_wbs-usr01.     "CRM 商机编码
          "gs_wbs_bus2054_new-user_field_char10_1            = ''."'X'.              "产品
          "gs_wbs_bus2054_new-statistical                    = ''."gs_wbs-xstat.     "统计
          "gs_wbs_bus2054_new-wbs_cctr_posted_actual         = ''."gs_wbs-kostl.     "CCtr 过账
          APPEND gs_wbs_bus2054_new TO gt_wbs_bus2054_new.
          CLEAR:ls_prpss.
        ENDLOOP.

        CALL FUNCTION 'BAPI_PS_INITIALIZATION'.
        "创建WBS
        REFRESH:gt_bapiret2.
        CALL FUNCTION 'BAPI_BUS2054_CREATE_MULTI'
          EXPORTING
            i_project_definition = lv_new_pspid
          TABLES
            it_wbs_element       = gt_wbs_bus2054_new
            et_return            = gt_bapiret2.

        CLEAR:lv_check,lv_message.
        LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
          lv_check = 'E'.
          lv_message = lv_message && gs_bapiret2-message.
          CLEAR:gs_bapiret2.
        ENDLOOP.

        IF lv_check = 'E'.
          "错误后初始化
          CALL FUNCTION 'ZPS01_PS_FLAG'.

          e_return-type = 'E'.
          e_return-message = lv_message.
          RETURN.
        ELSE.
          CLEAR:ls_return.
          "成功时提交
          CALL FUNCTION 'ZPS01_PS_PRECOMMIT'
            IMPORTING
              e_return = ls_return.
          IF ls_return-type = 'E'.
            e_return-type = 'E'.
            e_return-message = ls_return-message.
            RETURN.
          ENDIF.

          "--------------------------------创建网络--------------------------------------
          CLEAR : gs_net_bus2002_new.
          gs_net_bus2002_new-project_definition = lv_new_pspid."is_network-pspid.       "项目定义
          gs_net_bus2002_new-wbs_element        = lv_new_pspid."is_network-posid.       "工作分解结构元素 (WBS 元素)
          gs_net_bus2002_new-profile         = ls_prart-profid."is_network-ktext.       "网络参数文件
          CLEAR:ls_prpss.
*          READ TABLE lt_prpss INTO ls_prpss WITH KEY stufe = 1."获取最上层WBS的描述
*          IF sy-subrc EQ 0.
*            gs_net_bus2002_new-short_text         = ls_prpss-post1."is_network-ktext.       "描述
*          ENDIF.
          gs_net_bus2002_new-short_text         = i_input-post1."ls_prpss-post1."is_network-ktext.       "描述
          CALL FUNCTION 'BAPI_PS_INITIALIZATION'.
          "创建网络
          REFRESH:gt_bapiret2.
          CALL FUNCTION 'BAPI_BUS2002_CREATE'
            EXPORTING
              i_network = gs_net_bus2002_new
            TABLES
              et_return = gt_bapiret2.

          CLEAR:lv_check,lv_message.
          LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
            lv_check = 'E'.
            lv_message = lv_message && gs_bapiret2-message.
            CLEAR:gs_bapiret2.
          ENDLOOP.

          IF lv_check = 'E'.
            "错误后初始化
            CALL FUNCTION 'ZPS01_PS_FLAG'.

            e_return-type = 'E'.
            e_return-message = lv_message.
            RETURN.
          ELSE.
            "成功时提交
            REFRESH:gt_bapiret2.
            CALL FUNCTION 'BAPI_PS_PRECOMMIT'
              TABLES
                et_return = gt_bapiret2.

            CLEAR:lv_check,lv_message.
            LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
              lv_check = 'E'.
              lv_message = lv_message && gs_bapiret2-message.
              CLEAR:gs_bapiret2.
            ENDLOOP.
            IF lv_check EQ 'E'.
              CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

              CALL FUNCTION 'ZPS01_PS_FLAG'.
            ELSE.

              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  wait = 'X'.

              READ TABLE gt_bapiret2 INTO gs_bapiret2 WITH KEY id = 'CNIF_PI'
                                                   number = '003'.
              IF sy-subrc EQ 0.
                lv_aufnr = gs_bapiret2-message_v2.
              ENDIF.
            ENDIF.
            IF ls_return-type = 'E'.
              e_return-type = 'E'.
              e_return-message = ls_return-message.
              RETURN.
            ENDIF.

            "--------------------------------创建作业--------------------------------------
            READ TABLE lt_prpss INTO ls_prpss WITH KEY stufe = 1."获取最上层WBS的描述

            SELECT
              plko~plnty,"
              plko~plnnr,"
              plko~plnal,"
              plko~zaehl,"
              plko~pspnr,"

              plpo~plnkn,"
              plpo~vornr,"
              plpo~ltxa1,"
              plpo~dauno,"
              plpo~daune,"
              plpo~pspnr AS zbzwbs"
            FROM plko
            INNER JOIN plpo ON plko~plnnr = plpo~plnnr
            INTO TABLE @DATA(lt_plko)
            WHERE plko~pspnr = @ls_prpss-pspnr
              AND plko~plnty = '0'
              AND plpo~plnty = '0'.
            IF lt_plko IS NOT INITIAL.
              LOOP AT lt_plko INTO DATA(ls_plko).
                CLEAR:gs_act_bus2002_new.
                gs_act_bus2002_new-activity           = ls_plko-vornr.       "在网络和库网络中的作业编号
                gs_act_bus2002_new-description        = ls_plko-ltxa1.       "工序短文本
                gs_act_bus2002_new-control_key        = ''."gs_activity-steus.       "控制码-内协(ZPS1)和成本(ZPS3)
                CALL FUNCTION 'CONVERSION_EXIT_KONPT_OUTPUT'
                  EXPORTING
                    input  = ls_plko-zbzwbs
                  IMPORTING
                    output = lv_bzwbs.
                gs_act_bus2002_new-wbs_element = lv_new_pspid && lv_bzwbs+11."工作分解结构元素 (WBS 元素)
                gs_act_bus2002_new-duration_normal        = ls_plko-dauno.       "工作时间
                gs_act_bus2002_new-duration_normal_unit        = ls_plko-daune.       "工作时间单位
                gs_act_bus2002_new-work_cntr          = ''."gs_activity-arbpl.       "工作中心
                gs_act_bus2002_new-work_activity      = ''."gs_activity-arbei.       "工作(时间单位H)
                APPEND gs_act_bus2002_new TO gt_act_bus2002_new.
                CLEAR:ls_plko.
              ENDLOOP.

              CALL FUNCTION 'BAPI_PS_INITIALIZATION'.

              CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
                EXPORTING
                  input  = lv_aufnr
                IMPORTING
                  output = lv_aufnr.

              CALL FUNCTION 'BAPI_BUS2002_ACT_CREATE_MULTI'
                EXPORTING
                  i_number    = lv_aufnr
                TABLES
                  it_activity = gt_act_bus2002_new
                  et_return   = gt_bapiret2.
              CLEAR:lv_check,lv_message.
              LOOP AT gt_bapiret2 INTO gs_bapiret2 WHERE type CA 'AEX'.
                lv_check = 'E'.
                lv_message = lv_message && gs_bapiret2-message.
                CLEAR:gs_bapiret2.
              ENDLOOP.

              IF lv_check = 'E'.
                "错误后初始化
                CALL FUNCTION 'ZPS01_PS_FLAG'.

                e_return-type = 'E'.
                e_return-message = lv_message.
                RETURN.
              ELSE.
                CLEAR:ls_return.
                "成功时提交
                CALL FUNCTION 'ZPS01_PS_PRECOMMIT'
                  IMPORTING
                    e_return = ls_return.
                IF ls_return-type = 'E'.
                  e_return-type = 'E'.
                  e_return-message = ls_return-message.
                  RETURN.
                ENDIF.
              ENDIF.
            ELSE.
              e_return-type = 'E'.
              e_return-message = '标准项目抬头WBS' && ls_prpss-pspnr && '的作业不存在'.
              RETURN.
            ENDIF.
          ENDIF.
        ENDIF.
      ELSE.
        e_return-type = 'E'.
        e_return-message = '标准WBS不存在'.
        RETURN.
      ENDIF.
    ENDIF.
  ELSE.
    e_return-type = 'E'.
    e_return-message = '标准项目不存在'.
    RETURN.
  ENDIF.

  "如果预算为空,则不维护预算
  IF i_input-wtges IS NOT INITIAL.
    "维护预算
    "设置成本控制范围
    SET PARAMETER ID 'CAC' FIELD 'LRCG'.
    "调用BDC
    REFRESH:gt_bdcdata[].
    PERFORM bdc_dynpro      USING 'SAPMKBUD' '0200'.
    PERFORM bdc_field       USING 'BDC_OKCODE' '=MYENTER'.
    PERFORM bdc_field       USING 'BDC_CURSOR' 'PROJ-PSPID'.
    PERFORM bdc_field       USING 'PROJ-PSPID' lv_new_pspid."项目定义

    PERFORM bdc_dynpro      USING 'SAPLKBPP' '0320'.
    PERFORM bdc_field       USING 'BDC_OKCODE' '=POST'.
    PERFORM bdc_field       USING 'BDC_CURSOR' 'BPDY-WERT1(03)'.
    CLEAR:lv_wtges.
    lv_wtges = i_input-wtges.
    CONDENSE lv_wtges NO-GAPS.
    PERFORM bdc_field       USING 'BPDY-WERT1(01)' lv_wtges."项目预算

    lv_ctumode = 'N'.    "N就是一步完成,A是分步enter完成
    REFRESH gt_messtab[].
    CALL TRANSACTION 'CJ30' USING gt_bdcdata
          MODE lv_ctumode
          UPDATE 'S'
          MESSAGES INTO gt_messtab.
    CLEAR:lv_message,gt_messtab.
    LOOP AT gt_messtab.
      CLEAR:lv_msg.
      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          msgid               = gt_messtab-msgid
          msgnr               = gt_messtab-msgnr
          msgv1               = gt_messtab-msgv1
          msgv2               = gt_messtab-msgv2
          msgv3               = gt_messtab-msgv3
          msgv4               = gt_messtab-msgv4
        IMPORTING
          message_text_output = lv_msg.
      CONCATENATE lv_message lv_msg INTO lv_message.
    ENDLOOP.

    READ TABLE gt_messtab WITH KEY msgtyp = 'S'
                                      msgid = 'BP'
                                      msgnr = '043'.
    IF sy-subrc = 0 .
      e_return-type = 'S'.
      e_return-message = '项目创建成功'.
      e_return-message_v1 = lv_new_pspid.
    ELSE.
      e_return-type = 'E'.
      e_return-message = '项目创建成功,但预算维护失败'.
      e_return-message_v1 = lv_new_pspid.
    ENDIF.
  ELSE.
    e_return-type = 'S'.
    e_return-message = '项目创建成功'.
    e_return-message_v1 = lv_new_pspid.
  ENDIF.
ENDFUNCTION.
"-----------------------------@斌将军-----------------------------

程序中因为多次调用提交和初始化逻辑,因此封装成一个函数

初始化函数

"-----------------------------@斌将军-----------------------------
FUNCTION zps01_ps_flag.
*"----------------------------------------------------------------------
*"*"本地接口:
*"----------------------------------------------------------------------

  CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.

  CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
    EXPORTING
      i_precommit_ok = 'Y'.

ENDFUNCTION.
"-----------------------------@斌将军-----------------------------

BAPI提交函数

"-----------------------------@斌将军-----------------------------
FUNCTION zps01_ps_precommit.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  EXPORTING
*"     REFERENCE(E_RETURN) TYPE  BAPIRETURN
*"----------------------------------------------------------------------

  DATA:gt_return TYPE TABLE OF bapiret2,
       gs_return TYPE bapiret2.
  DATA:lv_check   TYPE char1,
       lv_message TYPE char200.

  CALL FUNCTION 'BAPI_PS_PRECOMMIT'
    TABLES
      et_return = gt_return.

  CLEAR:lv_check,lv_message.
  lv_check = 'S'.
  LOOP AT gt_return INTO gs_return WHERE type CA 'AEX'.
    lv_check = 'E'.
    lv_message = lv_message && gs_return-message.
    CLEAR:gs_return.
  ENDLOOP.
  IF sy-subrc EQ 0.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

    CALL FUNCTION 'ZPS01_PS_FLAG'.
  ELSE.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
  ENDIF.

  e_return-type = lv_check.
  e_return-message = lv_message.
ENDFUNCTION.
"-----------------------------@斌将军-----------------------------

 

张贴在2