Thursday, 2 December 2021

Tips about material classification mass upload

Function module ‘BAPI_OBJCL_CREATE’ can be used to upload new material classification data, and the function module ‘BAPI_OBJCL_CHANGE’ can be used to change existing material classification data. Before doing the upload data, it’s better to verify the material classification already existed or not by function module ‘BAPI_OBJCL_GETDETAIL’.

Be careful regards classification data as existing or not by checking the return message type from ‘BAPI_OBJCL_GETDETAIL’.

◉ <S>: Currently, material already has such an assignment for this specific classification.

SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Exam Preparation, SAP ABAP Certification, SAP ABAP Career

◉ <I>:  Assignment doesn’t exist now. But could be been created before then deleted later.

SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Exam Preparation, SAP ABAP Certification, SAP ABAP Career

<E>: There’ll be no error no matter assignment existed or not for this specific classification, but errors like not existing material.

SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Exam Preparation, SAP ABAP Certification, SAP ABAP Career

Nothing special for new classification assignment& creation for material, little tips when comes to change mode as material classification contains 3 different types of data and has to consider new data merged with already existing data without changes. Just refer to the below code as a reference.

DATA:
* Existing characteristic values
      it_old_num  type tt_bapi1003_alloc_values_num,
      it_old_char type tt_bapi1003_alloc_values_char,
      it_old_curr type tt_bapi1003_alloc_values_curr,
* Charcateristics values.
      it_new_num  type tt_bapi1003_alloc_values_num,
      wa_new_num  like line of it_new_num,
      it_new_char type tt_bapi1003_alloc_values_char,
      wa_new_char like line of it_new_char,
      it_new_curr type tt_bapi1003_alloc_values_curr,
      it_bapi_ret like table of bapiret2,
      wa_bapi_ret like bapiret2.

 * Get Existing Characteristic values.
       CALL FUNCTION 'BAPI_OBJCL_GETDETAIL'
          EXPORTING
            OBJECTKEY              = i_class-OBJECTKEYNEW
            OBJECTTABLE            = i_class-OBJECTTABLENEW
            CLASSNUM               = i_class-CLASSNUMNEW
            CLASSTYPE              = i_class-CLASSTYPENEW
          TABLES
            ALLOCVALUESNUM         = it_old_num
            ALLOCVALUESCHAR        = it_old_char
            ALLOCVALUESCURR        = it_old_curr
            RETURN                 = it_bapi_ret
                  .
 if return type = 'S' but it_old_num/it_old_char/it_old_curr are empty
    or return type = 'I'. "new creation mode
          CALL FUNCTION 'BAPI_OBJCL_CREATE'
            EXPORTING
              OBJECTKEYNEW            = i_class-OBJECTKEYNEW
              OBJECTTABLENEW          = i_class-OBJECTTABLENEW
              CLASSNUMNEW             = i_class-CLASSNUMNEW
              CLASSTYPENEW            = i_class-CLASSTYPENEW
*             STATUS                  = '1'
*             STANDARDCLASS           =
*             CHANGENUMBER            =
*             KEYDATE                 = SY-DATUM
*             NO_DEFAULT_VALUES       = ' '
*           IMPORTING
*             CLASSIF_STATUS          =
            TABLES
*             ALLOCVALUESNUM          =
             ALLOCVALUESCHAR          =  lt_charact
*            ALLOCVALUESCURR          =
              RETURN                  =  lt_class_err
                    .

 else. "update mode
*         Move all existing values to the update tables
          if it_old_num[] is not initial.
            append lines of it_old_num  to it_new_num.
          endif.
          if it_old_char[] is not initial.
            append lines of it_old_char to it_new_char.
          endif.
          if it_old_curr[] is not initial.
            append lines of it_old_curr to it_new_curr.
          endif.

          refresh: it_cl_chars.
*     get assigment between class& char
          call function 'CLME_FEATURE_ATTR_OF_CLASS'
            exporting
              class                 = i_class-CLASSNUMNEW
              classtype             = i_class-CLASSTYPENEW
              language              = sy-langu
              key_date              = sy-datum
*         WITH_VALUES           = 'X'
*         CHANGE_NUMBER         = ' '
            tables
              tfeatures             = it_cl_chars
*         TVALUES               = TVALUES
            exceptions
              class_not_found       = 1
              no_authority          = 2
              others                = 3.

*   update data from upload file base on existing values
          clear i_charact.
          LOOP AT it_charact INTO i_charact
                   WHERE objectkeynew = p_head-material
                   and   CLASSTYPENEW = i_class-CLASSTYPENEW
                   and   CLASSNUMNEW  = i_class-classnumnew.
*   loop at all char need to be update for current class
            clear: wa_cl_chars.
            read table it_cl_chars into wa_cl_chars
                          with key atnam = i_charact-charact.
            if sy-subrc = 0.
              if wa_cl_chars-atfor = 'NUM'  or
                 wa_cl_chars-atfor = 'DATE' or
                 wa_cl_chars-atfor = 'TIME'.
* All numeric, date and time values-> it_new_num.
                wa_new_num-charact = i_charact-charact.
* Convert into internal format.
                perform convert_to_internal using i_charact-charact
                                                  i_charact-value_char
                                         changing wa_new_num-value_from.
* Check if the value already exists, update with new value.
                clear ls_value_num.
                read table it_new_num into ls_value_num with key charact = i_charact-charact.
                if sy-subrc = 0.
                  ls_value_num-value_from = wa_new_num-value_from.
                  modify it_new_num from ls_value_num index sy-tabix.
                  clear: wa_new_num, ls_value_num.
                else.
* Value did not exist, append it.
                  append wa_new_num to it_new_num.
                  clear  wa_new_num.
                endif.
              else.
* if not num/date/time, means All char types-> it_new_char
                wa_new_char-charact       = i_charact-charact.
                wa_new_char-value_char    = i_charact-value_char.
                wa_new_char-value_neutral = i_charact-value_char.

* Check if the value already exists, update with new value.
                clear ls_value_char.
                read table it_new_char into ls_value_char with key charact = i_charact-charact.
                if sy-subrc = 0.
                  ls_value_char-value_char    = wa_new_char-value_char.
                  ls_value_char-value_neutral = wa_new_char-value_neutral.
                  modify it_new_char from ls_value_char index sy-tabix.
                  clear: wa_new_char, ls_value_char.
                else.
* Value did not exist, append it.
                  append wa_new_char to it_new_char.
                  clear  wa_new_char.
                endif.
              endif. "DATE/TIME/NUM
            else.
              it_report-statu = c_error.
              CONCATENATE i_charact-charact text-e38 into it_report-messa.
              APPEND it_report.
              CONTINUE.
            endif.
          ENDLOOP.

* Create/Change Classification values.
          refresh it_bapi_ret.
          call function 'BAPI_OBJCL_CHANGE'
            exporting
              objectkey                = i_class-OBJECTKEYNEW
              objecttable              = i_class-OBJECTTABLENEW
              classnum                 = i_class-CLASSNUMNEW
              classtype                = i_class-CLASSTYPENEW
*         STATUS                   = '1'
*         STANDARDCLASS            = STANDARDCLASS
*         CHANGENUMBER             = CHANGENUMBER
*         KEYDATE                  = SY-DATUM
*         NO_DEFAULT_VALUES        = ' '
*       IMPORTING
*         CLASSIF_STATUS           = CLASSIF_STATUS
            tables
              allocvaluesnumnew        = it_new_num
              allocvaluescharnew       = it_new_char
              allocvaluescurrnew       = it_new_curr
              return                   = it_bapi_ret.

 endif.

Source: sap.com

No comments:

Post a Comment