前言背景:​本次实施的FMS项目,由于自定义的增强字段比较多,且几乎所有的增强字段都建了张配置表,即使字段代码+字段描述这种形式,大概有100多个,一直通过SE11去建,整个人都懵逼了。作为一名程序猿,肯定是不能忍啊,当然是想着能偷懒就偷懒,这种无限重复的事情怎么可能一直做!所以想着能不能用程序去创建好了,酱紫就不用人为的SE11去弄了!


实现过程:​百度+google,几乎没找到什么demo,但是函数倒是找到了GOX_GEN_DOMA_STD,然后花了一天时间吧,总算是研究出怎么玩的了。以下是封装好的一个函数,当然,这个函数还不是完全版,但是应对目前项目上的需求足够了,批量创建的话,我想的是把数据整理成一个EXCEL模板,然后导入SAP内,每个域都通过这个函数去创建,那么不就批量了嘛!

FUNCTION ZZF_CREATE_DOMAIN.
*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" REFERENCE(DEVCLASS) TYPE DEVCLASS DEFAULT '$TMP'
*" REFERENCE(IV_REQUEST_WB) TYPE TRKORR OPTIONAL
*" REFERENCE(DOMANAME) TYPE DOMNAME
*" REFERENCE(DDLANGUAGE) TYPE DDLANGUAGE OPTIONAL
*" REFERENCE(DATATYPE) TYPE DATATYPE_D
*" REFERENCE(LENG) TYPE LENG
*" REFERENCE(OUTLEN) TYPE OUTPUTLEN OPTIONAL
*" REFERENCE(DECIMALS) TYPE DECIMALS OPTIONAL
*" REFERENCE(LOWERCASE) TYPE LOWERCASE OPTIONAL
*" REFERENCE(SIGNFLAG) TYPE SIGNFLAG OPTIONAL
*" REFERENCE(VALEXI) TYPE VALEXI OPTIONAL
*" REFERENCE(CONVEXIT) TYPE CONVEXIT OPTIONAL
*" TABLES
*" ET_DD07V STRUCTURE DD07V OPTIONAL
*" RETURN STRUCTURE BAPIRETURN
*"----------------------------------------------------------------------



DATA: LT_NEW_OBJECT TYPE COMT_GOX_DEF_HEADER,
LT_OLD_OBJECT LIKE LT_NEW_OBJECT,
LV_DOMA_NAME TYPE CHAR32,
LV_DEVCLASS TYPE DEVCLASS,
LV_REQUEST_WB TYPE TRKORR,
LS_NEW_OBJECT LIKE LINE OF LT_NEW_OBJECT,
LS_NEW_OBJECT_DETAILS TYPE LINE OF COMT_GOX_TABLE_ENTRY_FIELDS,
LT_RETURNTAB TYPE BAPIRETTAB,
LS_RETURN LIKE LINE OF LT_RETURNTAB.

LV_DOMA_NAME = DOMANAME.
LV_REQUEST_WB = IV_REQUEST_WB.
MOVE DEVCLASS TO LV_DEVCLASS.

LS_NEW_OBJECT-OBJECT_TYPE = 'DOMA'.
LS_NEW_OBJECT-OBJECT_NAME = DOMANAME.

TRY.
DATA(LV_GUID) = CL_SYSTEM_UUID=>IF_SYSTEM_UUID_STATIC~CREATE_UUID_C32( ).
CATCH CX_UUID_ERROR .
ENDTRY.

MOVE LV_GUID TO LS_NEW_OBJECT-KEY_GUID .
DATA(LV_PARENT_GUID) = LS_NEW_OBJECT-KEY_GUID.
MOVE 'DDLANGUAGE' TO LS_NEW_OBJECT_DETAILS-FIELDNAME."语言
MOVE DDLANGUAGE TO LS_NEW_OBJECT_DETAILS-FIELDVALUE.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'DATATYPE'."数据类型
LS_NEW_OBJECT_DETAILS-FIELDVALUE = DATATYPE.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'LENG'."类型长度
LS_NEW_OBJECT_DETAILS-FIELDVALUE = LENG.
SHIFT LS_NEW_OBJECT_DETAILS-FIELDVALUE LEFT DELETING LEADING SPACE.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

CLEAR LS_NEW_OBJECT_DETAILS.
IF OUTLEN IS NOT INITIAL .
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'OUTPUTLEN'."输出长度 默认和类型长度一致
LS_NEW_OBJECT_DETAILS-FIELDVALUE = OUTLEN.
SHIFT LS_NEW_OBJECT_DETAILS-FIELDVALUE LEFT DELETING LEADING SPACE.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.
ENDIF.

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'DECIMALS'."小数位
LS_NEW_OBJECT_DETAILS-FIELDVALUE = DECIMALS.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'LOWERCASE'."是否区分大小写
LS_NEW_OBJECT_DETAILS-FIELDVALUE = LOWERCASE.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'SIGNFLAG'."符号
LS_NEW_OBJECT_DETAILS-FIELDVALUE = SIGNFLAG.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'VALEXI'."是否存在固定值
LS_NEW_OBJECT_DETAILS-FIELDVALUE = VALEXI.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

*CLEAR LS_NEW_OBJECT_DETAILS.
*LS_NEW_OBJECT_DETAILS-FIELDNAME = 'CONVEXIT'."转换历程
*LS_NEW_OBJECT_DETAILS-FIELDVALUE = 'X'.
*APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.
CLEAR LS_NEW_OBJECT_DETAILS.
APPEND LS_NEW_OBJECT TO LT_NEW_OBJECT.
CLEAR LS_NEW_OBJECT.

IF ET_DD07V IS NOT INITIAL .

LOOP AT ET_DD07V ASSIGNING FIELD-SYMBOL(<FW_DD07V>).
LS_NEW_OBJECT-OBJECT_TYPE = 'DOMA_SET'.
TRY.
LV_GUID = CL_SYSTEM_UUID=>IF_SYSTEM_UUID_STATIC~CREATE_UUID_C32(
).
CATCH CX_UUID_ERROR .
ENDTRY.
LS_NEW_OBJECT-KEY_GUID = LV_GUID.
LS_NEW_OBJECT-PARENT_KEY = LV_PARENT_GUID.
LV_PARENT_GUID = LV_GUID.
"域值

CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'VALPOS'."值关键字
LS_NEW_OBJECT_DETAILS-FIELDVALUE = <FW_DD07V>-VALPOS.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.

IF <FW_DD07V>-DDTEXT IS NOT INITIAL .
CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'DDTEXT'."简短描述
LS_NEW_OBJECT_DETAILS-FIELDVALUE = <FW_DD07V>-DDTEXT.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.
ENDIF.

IF <FW_DD07V>-DOMVALUE_L IS NOT INITIAL .
CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'DOMVALUE_L'."下限值
LS_NEW_OBJECT_DETAILS-FIELDVALUE = <FW_DD07V>-DOMVALUE_L.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.
ENDIF.

IF <FW_DD07V>-DOMVALUE_H IS NOT INITIAL .
CLEAR LS_NEW_OBJECT_DETAILS.
LS_NEW_OBJECT_DETAILS-FIELDNAME = 'DOMVALUE_H'."DOMVALUE_H
LS_NEW_OBJECT_DETAILS-FIELDVALUE = <FW_DD07V>-DOMVALUE_H.
APPEND LS_NEW_OBJECT_DETAILS TO LS_NEW_OBJECT-DETAILS.
ENDIF.

APPEND LS_NEW_OBJECT TO LT_NEW_OBJECT.
CLEAR LS_NEW_OBJECT.

ENDLOOP.
ENDIF.

DATA ET_BAPIRETURN TYPE BAPIRETTAB.
DATA ET_TRANSPORT TYPE COMT_GOX_TRANS_OBJECT.

CALL FUNCTION 'GOX_GEN_DOMA_STD'
EXPORTING
IV_OBJECT_NAME = LV_DOMA_NAME
IT_OBJECT_NEW = LT_NEW_OBJECT
IT_OBJECT_OLD = LT_OLD_OBJECT
IV_DEVCLASS = LV_DEVCLASS
IV_REQUEST_WB = LV_REQUEST_WB
IMPORTING
ET_BAPIRETURN = ET_BAPIRETURN
ET_TRANSPORT = ET_TRANSPORT
.


APPEND LINES OF ET_BAPIRETURN TO RETURN.

*CHECK ET_BAPIRETURN IS INITIAL.

DATA: LV_DDOBJNAME TYPE DDOBJNAME,
LS_DD02V TYPE DD02V.

LV_DDOBJNAME = LV_DOMA_NAME.
DATA NAME TYPE DDOBJNAME.
DATA STATE TYPE DDOBJSTATE.
DATA LANGU TYPE SY-LANGU.
DATA GOTSTATE TYPE DDGOTSTATE.
DATA DD01V_WA TYPE DD01V.
DATA DD07V_TAB TYPE STANDARD TABLE OF DD07V.

CALL FUNCTION 'DDIF_DOMA_GET'
EXPORTING
NAME = LV_DDOBJNAME
STATE = 'A'
LANGU = SY-LANGU
IMPORTING
GOTSTATE = GOTSTATE
DD01V_WA = DD01V_WA
TABLES
DD07V_TAB = DD07V_TAB
EXCEPTIONS
ILLEGAL_INPUT = 1
.

*DATA NAME TYPE DDOBJNAME.
*DATA DD01V_WA TYPE DD01V.
*DATA DD07V_TAB TYPE STANDARD TABLE OF DD07V.
*
CALL FUNCTION 'DDIF_DOMA_PUT'
EXPORTING
NAME = LV_DDOBJNAME
DD01V_WA = DD01V_WA
TABLES
DD07V_TAB = DD07V_TAB
EXCEPTIONS
DOMA_NOT_FOUND = 1
NAME_INCONSISTENT = 2
DOMA_INCONSISTENT = 3
PUT_FAILURE = 4
PUT_REFUSED = 5
.
IF SY-SUBRC NE 0.
RETURN.
ENDIF.

* ACTIVE TABLE
CALL FUNCTION 'DDIF_DOMA_ACTIVATE'
EXPORTING
NAME = LV_DDOBJNAME
AUTH_CHK = ' '
.

CHECK SY-SUBRC = 0.


ENDFUNCTION.

测试数据


SAP abap使用程序批量创建域_字段

SAP abap使用程序批量创建域_程序猿_02


创建成果

SAP abap使用程序批量创建域_创建域_03

SAP abap使用程序批量创建域_创建域_04