Hi All,

I would like to share one of my recent experiences which I hope will make someone else’ life easier. My requirement was to maintain the multi level categorization for a transaction. Thus the transaction’s value for category was getting changed while the save happens.

Untitled picture.png

Above is the screenshot of the category which in programming you will get the values in ET_SUBJECT which is the returning parameter for CRM_ORDER_READ function module.

You can write the below code in appropriate BADI/program which you think most suitable for your purpose.

TYPES:BEGIN OF l_ty_asp_id,

        asp_id TYPE crm_erms_cat_guid,

        val_to TYPE crm_erms_cat_timestamp,

        END OF l_ty_asp_id.

* Data declarations

  DATA: l_v_user              TYPE syuname, ” User name maintained in static variable

        l_v_bp_guid           TYPE bu_partner_guid, ” BP GUID

        l_v_logical_key       TYPE crmt_logical_key,

        l_v_bpid              TYPE personid,        ” BP Id

        l_v_catid             TYPE crm_erms_cat_ca_id,  ” Category ID

        l_wa_partner_com      TYPE crmt_partner_com, ” Partner details

        l_wa_field_names      TYPE crmt_input_field_names, ” Input fields

        l_wa_input_fields     TYPE crmt_input_field,            ” local work area to store the input fields

        l_i_input_fields      TYPE crmt_input_field_names_tab, ” Internal table for input fields

        l_wa_subject_com      TYPE crmt_subject_com,

        l_i_subject_com       TYPE crmt_subject_comt,

        l_is_field_names      TYPE crmt_input_field_names_tab,

        l_is_input_fields     TYPE crmt_input_field_tab,

        l_i_subject           TYPE crmt_subject_wrkt,

        l_i_asp_id            TYPE TABLE OF l_ty_asp_id.

* Constants

  CONSTANTS:

            l_c_ref_kind      TYPE crmt_object_kind      VALUE ‘A’,        ” Reference kind as header

            l_c_prof_type TYPE crmt_subject_profile_type VALUE ‘A’,               ” local constant to store the value for profile type

            l_c_mode      TYPE crmt_mode                 VALUE ‘A’,               ” local constant to store the value for mode

            l_c_profile   TYPE crmt_fieldname            VALUE ‘SUBJECT_PROFILE’, ” local constant to store the field name for the field SUBJECT_PROFILE

            l_c_hand      TYPE crmt_fieldname            VALUE ‘HANDLE’,          ” local constant to store the field name for the field HANDLE

            l_c_header    TYPE crmt_fieldname            VALUE ‘GUID’,            ” local constant to store the field name for the field GUID

            l_c_ref_head  TYPE crmt_fieldname            VALUE ‘REF_GUID’,        ” local constant to store the field name for the field REF_GUID

            l_c_kind      TYPE crmt_fieldname            VALUE ‘REF_KIND’,        ” local constant to store the field name for the field REF_KIND

            l_c_prof_typ  TYPE crmt_fieldname            VALUE ‘PROFILE_TYPE’,    ” local constant to store the field name for the field PROFILE_TYPE

            l_c_obj_name  TYPE crmt_object_name          VALUE ‘SUBJECT’,         ” local constant to store the field name for the field SUBJECT

            l_c_asp_id    TYPE crmt_fieldname            VALUE ‘ASP_ID’,    ” local constant to store the field name for the field ASP_ID

            l_c_cat_id    TYPE crmt_fieldname            VALUE ‘CAT_ID’,    ” local constant to store the field name for the field CAT_ID

            l_c_asp_value TYPE crm_erms_cat_as_id        VALUE ‘YOUR CATEGORIZATION SCHEMA’.

  FIELD-SYMBOLS:<fs_asp_id> TYPE l_ty_asp_id.

  CLEAR:l_wa_field_names.

* Select the corresponding category ID

* First fetch the active ASP ID. For this following things are to be done.

* 1) Fetch the asp_guid from crmc_erms_cat_as which has status R( released)

* 2) Take the val_to which is the highest.

  SELECT asp_guid

         val_to

    FROM crmc_erms_cat_as

    INTO TABLE l_i_asp_id

   WHERE asp_id = l_c_asp_value

     AND asp_state = ‘R’.

  IF sy-subrc = 0.

    SORT l_i_asp_id DESCENDING BY val_to.

    READ TABLE l_i_asp_id ASSIGNING <fs_asp_id> INDEX 1.”Reading the highest value.

    IF sy-subrc = 0.   

* Now read the category id for the ASP id.

      SELECT crmc_erms_cat_ca~cat_id UP TO 1 ROWS

        FROM crmc_erms_cat_ca

        JOIN crmc_erms_cat_cd

          ON crmc_erms_cat_ca~cat_guid = crmc_erms_cat_cd~cat_guid

        INTO l_v_catid

       WHERE crmc_erms_cat_cd~cat_label_uc = ca_solution_team

         AND crmc_erms_cat_ca~asp_guid = <fs_asp_id>-asp_id.

      ENDSELECT.

      IF sy-subrc = 0.

* Populating the local work area for subject details.

        l_wa_subject_com-ref_guid        = is_orderadm_h_wrk-guid.

        l_wa_subject_com-ref_kind        = l_c_ref_kind.

        l_wa_subject_com-asp_id          = l_c_asp_value.

        l_wa_subject_com-cat_id          = l_v_catid.

        l_wa_subject_com-katalog_type    = ‘D’.

* Generating a random GUID

        TRY.

            l_wa_subject_com-guid = cl_system_uuid=>create_uuid_x16_static( ).

          CATCH cx_uuid_error.      “Catching an exception

* No need of further processing s required if an exception is caught while generating a

* random GUID, hence raising an exception

        ENDTRY.

        l_wa_subject_com-profile_type    = l_c_prof_type.

* Check whether the subject is already there. If yes, mode should be change. Otherwise create.

        CALL FUNCTION ‘CRM_SUBJECT_READ_OW’

          EXPORTING

            iv_ref_guid     = is_orderadm_h_wrk-guid

            iv_ref_kind     = ‘A’

            iv_profile_type = ‘A’

          IMPORTING

            et_subject_wrk  = l_i_subject

          EXCEPTIONS

            error_occurred  = 1

            OTHERS          = 2.

        IF sy-subrc <> 0.

* Implement suitable error handling here

        ENDIF.

        IF l_i_subject IS INITIAL.

          l_wa_subject_com-mode            = l_c_mode.

        ELSE.

          l_wa_subject_com-mode            = ‘B’.

        ENDIF.

        INSERT l_wa_subject_com INTO TABLE l_i_subject_com.

** Populating the field names of the fields that are to be updated

        l_wa_field_names-fieldname = l_c_asp_id.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_cat_id.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_profile.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_hand.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_header.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_ref_head.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_kind.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        l_wa_field_names-fieldname = l_c_prof_typ.

        INSERT l_wa_field_names INTO TABLE l_is_field_names.

        CALL FUNCTION ‘CRM_SERVICE_OS_GET_LOG_KEY’

          EXPORTING

            iv_guid           = l_wa_subject_com-guid

            iv_handle         = l_wa_subject_com-handle

          IMPORTING

            ev_logical_key    = l_v_logical_key

          EXCEPTIONS

            parameter_missing = 1

            OTHERS            = 2.

        IF sy-subrc <> 0.

        ENDIF.

*Updating hte internal table for input fields.

        l_wa_input_fields-ref_guid    = is_orderadm_h_wrk-guid.

        l_wa_input_fields-ref_kind    = l_c_ref_kind.

        l_wa_input_fields-objectname  = l_c_obj_name .

        l_wa_input_fields-logical_key = l_v_logical_key.

        l_wa_input_fields-field_names = l_is_field_names.

        INSERT l_wa_input_fields INTO TABLE l_is_input_fields.

* Maintain the subject values

        CALL FUNCTION ‘CRM_SUBJECT_MAINTAIN_OW’

          EXPORTING

            it_subject_com = l_i_subject_com

          CHANGING

            ct_input_field = l_is_input_fields

          EXCEPTIONS

            error_occurred = 1

            OTHERS         = 2.

        IF sy-subrc <> 0.

* Do nothing.

        ENDIF.

      ENDIF.

.

Now you can check the transaction for the category. It would be maintained with right values.

Please let me know if you have any queries.

Thanks,

Faisal

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply