文章目录
- BAPI_BATCH_CHANGE:修改批次的特征值
- 实现步骤
- 定义变量
- 获取对象/类等
- 获取已维护特性值
- 新特性值更新
- 注意事项
- 常见的错误:Object xxx does not exist
- 最终效果
- 字段介绍
- 正式测试-310测试环境(HS)
- 特性值数据内表介绍
- 运行效果
- 完整代码
BAPI_BATCH_CHANGE:修改批次的特征值
现在有一个需求是要修改批次里面的某一个特征值,所以需要使用到BAPI_BATCH_CHANGE这个BAPI,除此之外,还有VB_BATCH_2_CLASS_OBJECT函数和BAPI_OBJCL_GETDETAIL。
特别注意
- 注意函数
VB_BATCH_2_CLASS_OBJECT
的i_matnr
参数,注意前导0的问题。因为这个前导0,我的批次的类还取不到,哭了,后面发现两个系统的物料一个是有前导0,一个是没有的,所以要注意。 - 注意BAPI函数
BAPI_OBJCL_GETDETAIL
的objectkey_long
参数,不使用objectkey
。
实现步骤
定义变量
DATA e_objek TYPE cuobn.
DATA e_obtab TYPE tabelle.
DATA e_klart TYPE klassenart.
DATA e_class TYPE klasse_d.
DATA allocvaluesnum TYPE STANDARD TABLE OF bapi1003_alloc_values_num. "分配 BAPI - NUM、DATE、TIME 类型的值
DATA allocvalueschar TYPE STANDARD TABLE OF bapi1003_alloc_values_char. "分配 BAPI - CHAR、BOOL 类型的值
DATA allocvaluescurr TYPE STANDARD TABLE OF bapi1003_alloc_values_curr. "分配 BAPI - CURR 类型值
DATA return TYPE STANDARD TABLE OF bapiret2.DATA: lt_numnew TYPE TABLE OF bapi1003_alloc_values_num,lt_charnew TYPE TABLE OF bapi1003_alloc_values_char,lt_currnew TYPE TABLE OF bapi1003_alloc_values_curr,lt_returnnew TYPE TABLE OF bapiret2.DATA: ls_char TYPE bapi1003_alloc_values_char,ls_return TYPE bapiret2.
获取对象/类等
函数名称:VB_BATCH_2_CLASS_OBJECT
CALL FUNCTION 'VB_BATCH_2_CLASS_OBJECT'EXPORTINGi_matnr = 'EWM_RAW_01' "物料i_charg = '2022061302' "批次i_werks = '1710' "工厂IMPORTINGe_objek = e_objeke_obtab = e_obtabe_klart = e_klarte_class = e_class.
获取已维护特性值
BAPI名称:BAPI_OBJCL_GETDETAIL
CALL FUNCTION 'BAPI_OBJCL_GETDETAIL'EXPORTING"objectkey = e_objek "如果是物料是40位的,选这个会报错,要选OBJECTKEY_LONGobjecttable = e_obtab "对于分类的对象的数据库表名称classnum = e_class "分类编号classtype = e_klart "分类的类型"keydate = sy-datum
* UNVALUATED_CHARS = ' '
* LANGUAGE = SY-LANGUobjectkey_long = e_objek "关键字字段
* IMPORTING
* STATUS = STATUS
* STANDARDCLASS = STANDARDCLASSTABLESallocvaluesnum = allocvaluesnumallocvalueschar = allocvaluescharallocvaluescurr = allocvaluescurrreturn = return.
新特性值更新
BAPI名称:BAPI_BATCH_CHANGE
lt_numnew[] = allocvaluesnum.
lt_charnew[] = allocvalueschar.
lt_currnew[] = allocvaluescurr.
lt_returnnew[] = return.IF lt_charnew[] IS NOT INITIAL.LOOP AT lt_charnew INTO ls_char.ls_char-value_char = '1'. "特征值ls_char-value_neutral = '1'. "特征值ls_char-value_char_long = '1'. "特征值ls_char-value_neutral_long = '1'. "特征值MODIFY lt_charnew FROM ls_char.ENDLOOP.
ELSE.ls_char-charact = 'Z_SW_DW_BA'. "特征名ls_char-value_char = '1'. "特征值ls_char-value_neutral = '1'. "特征值ls_char-value_char_long = '1'. "特征值ls_char-value_neutral_long = '1'. "特征值APPEND ls_char TO lt_charnew[].
ENDIF."--> 新特性值更新
CALL FUNCTION 'BAPI_OBJCL_CHANGE'EXPORTING
* OBJECTKEY = OBJECTKEYobjecttable = e_obtabclassnum = e_classclasstype = e_klart
* STATUS = '1'
* STANDARDCLASS = STANDARDCLASS
* CHANGENUMBER = CHANGENUMBER
* KEYDATE = SY-DATUM
* NO_DEFAULT_VALUES = ' '
* KEEP_SAME_DEFAULTS = ' 'objectkey_long = e_objek
* IMPORTING
* CLASSIF_STATUS = CLASSIF_STATUSTABLESallocvaluesnumnew = lt_numnewallocvaluescharnew = lt_charnewallocvaluescurrnew = lt_currnewreturn = lt_returnnew.LOOP AT return INTO ls_return WHERE type = 'E' OR type = 'A'.WRITE: ls_return-message.CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.EXIT.
ENDLOOP.IF sy-subrc <> 0.CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = 'X'.
ENDIF.
注意事项
常见的错误:Object xxx does not exist
注:在S/4之前的系统,物料编码是18位长度,可以直接使用参数objectkey接收Object key的值,特性的值是可以被正确读取出来的。
但是在S/4系统,物料编码是40位,若还是使用objectkey接收Object Key的值,则会出现如上的错误。
所以在S/4系统,应该改用参数objectkey_long。
最终效果
字段介绍
正式测试-310测试环境(HS)
特性值数据内表介绍
事务代码MSC3N的界面如下:
运行效果
原始的值记录:
现在修改成功,原来的值修改成了VENDOR002
完整代码
"-------------- 测试 获取对象和类 LHY20240422 --------------DATA e_objek TYPE cuobn.DATA e_obtab TYPE tabelle.DATA e_klart TYPE klassenart.DATA e_class TYPE klasse_d.CALL FUNCTION 'VB_BATCH_2_CLASS_OBJECT'EXPORTINGi_matnr = '000000100000008374' "物料号,一定要注意前导0!!!,否则获取类e_class不成功,在自己购买的测试系统的测试物料是没有前导0的i_charg = '2404220002' "批次号i_werks = '1100'IMPORTINGe_objek = e_objeke_obtab = e_obtabe_klart = e_klarte_class = e_class.DATA allocvaluesnum TYPE STANDARD TABLE OF bapi1003_alloc_values_num. "NUM、DATE、TIME 类型的特征值DATA allocvalueschar TYPE STANDARD TABLE OF bapi1003_alloc_values_char. "CHAR、BOOL 类型的特征值DATA allocvaluescurr TYPE STANDARD TABLE OF bapi1003_alloc_values_curr. "CURR 类型特征值DATA return TYPE STANDARD TABLE OF bapiret2.DATA: lt_numnew TYPE TABLE OF bapi1003_alloc_values_num,lt_charnew TYPE TABLE OF bapi1003_alloc_values_char,lt_currnew TYPE TABLE OF bapi1003_alloc_values_curr,lt_returnnew TYPE TABLE OF bapiret2.DATA: ls_char TYPE bapi1003_alloc_values_char,ls_return TYPE bapiret2."-------------- 测试 获取已维护特性值--------------CALL FUNCTION 'BAPI_OBJCL_GETDETAIL'EXPORTING
* OBJECTKEY = e_objekobjecttable = e_obtabclassnum = e_classclasstype = e_klart
* KEYDATE = SY-DATUM
* UNVALUATED_CHARS = ' '
* LANGUAGE = SY-LANGUobjectkey_long = e_objek "如果是物料是40位的,选这个会报错(类型不匹配),要选OBJECTKEY_LONG
* IMPORTING
* STATUS =
* STANDARDCLASS =TABLESallocvaluesnum = allocvaluesnumallocvalueschar = allocvaluescharallocvaluescurr = allocvaluescurrreturn = return.lt_numnew[] = allocvaluesnum.lt_charnew[] = allocvalueschar.lt_currnew[] = allocvaluescurr.lt_returnnew[] = return.READ TABLE lt_charnew INTO ls_char WITH KEY charact = 'ZMM015'.IF sy-subrc <> 0."不存在ZMM015特征,可以创建ls_char-charact = 'ZMM015'.ls_char-value_char = 'VENDOR001'. "特征值ls_char-value_neutral = 'VENDOR001'. "特征值ls_char-value_char_long = 'VENDOR001'. "特征值ls_char-value_neutral_long = 'VENDOR001'. "特征值APPEND ls_char TO lt_charnew.ELSE. "如果已经存在,可以修改其特征值ls_char-value_char = 'VENDOR002'. "特征值ls_char-value_neutral = 'VENDOR002'. "特征值ls_char-value_char_long = 'VENDOR002'. "特征值ls_char-value_neutral_long = 'VENDOR002'. "特征值MODIFY lt_charnew FROM ls_char INDEX sy-tabix.ENDIF."---------------- 测试 新特性值更新--------------CALL FUNCTION 'BAPI_OBJCL_CHANGE'EXPORTING
* OBJECTKEY =objecttable = e_obtabclassnum = e_classclasstype = e_klart
* STATUS = '1'
* STANDARDCLASS =
* CHANGENUMBER =
* KEYDATE = SY-DATUM
* NO_DEFAULT_VALUES = ' '
* KEEP_SAME_DEFAULTS = ' 'objectkey_long = e_objek
* IMPORTING
* CLASSIF_STATUS =TABLESallocvaluesnumnew = lt_numnewallocvaluescharnew = lt_charnewallocvaluescurrnew = lt_currnewreturn = lt_returnnew.LOOP AT return INTO ls_return WHERE type = 'E' OR type = 'A'.WRITE: ls_return-message.CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.EXIT.ENDLOOP.IF sy-subrc <> 0.CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = 'X'.ENDIF.