The picture below shows the required result:Expected Output - ABC, TEST1, NE*****, TEST2, TEST3, and TEXT
are customer groups and they have their respective customers
underneath them.
In source, select Object Type = InfoObject, Name = 0CUSTOMER, and
Subtype = Attributes.Once the transformation is created, create an expert routine for it.
From menu bar select: Edit -> Routines -> Expert Routine.
See the picture below for reference:Segments in a Hierarchy Transformation. Highlighted are the ones that
are mandatory.
IF SOURCE_PACKAGE IS NOT INITIAL.
" Constants Declaration
CONSTANTS: lc_hier TYPE rshienm VALUE 'HIER_CUST_GRP',
lc_act TYPE rsobjvers VALUE 'A',
lc_hier_node TYPE rsnodename VALUE '0HIER_NODE',
lc_customer TYPE rsiobjnm VALUE '0CUSTOMER',
lc_en TYPE c LENGTH 2 VALUE 'EN'.
" Data Declaration
DATA: lv_prnt_id TYPE rshienodid,
lv_max_nodeid TYPE rshienodid.
" Macro to increment a number by 1.
DEFINE increment.
&1 = &1 + 1.
END-OF-DEFINITION.
" Get technical ID (hieid) of customer group hierarchy
SELECT SINGLE hieid
FROM rshiedir
INTO @DATA(lv_hieid_hier_cust_grp)
WHERE hienm = @lc_hier.
" Get the MAX node id. This is important. Simply put,
" always take the max node id and start creating
" your nodes by adding 1 to it to make sure you
" don't end up using a node id that is already
" being used.
" Also, never use the "WHERE hieid =" clause here
" as we want maximum node ID across all hierarchies
" for 0CUSTOMER.
SELECT SINGLE MAX( nodeid )
FROM /bi0/hcustomer
INTO lv_max_nodeid.
" Get parents that already exist. This includes
" everything - the root node & the leaves/leaf.
" This needs to be selected only from our custom
" hierarchy. So, use the "WHERE hieid =" clause.
" In the case of multiple hierarchies, add all of
" them here, separated by "AND".
SELECT hieid, nodeid, nodename
FROM /bi0/hcustomer
INTO TABLE @DATA(lt_existing_parents)
WHERE hieid = @lv_hieid_hier_cust_grp
AND objvers = @lc_act.
SORT lt_existing_parents BY hieid nodename.
SORT SOURCE_PACKAGE BY customer.
" lv_index will be used to increment the node ID.
DATA(lv_index) = 00000000.
LOOP AT SOURCE_PACKAGE ASSIGNING <source_fields>.
*--------------------------------------------------------------------*
*~~~~~~~~~~~~~~~~~~Process Hierarchy - HIER_CUST_GRP~~~~~~~~~~~~~~~~~*
*--------------------------------------------------------------------*
IF <source_fields>-/bic/zccst_grp <> space.
CLEAR RESULT_FIELDS_3.
" Fill result_package_1 with the technical name
" for the hierarchy.
RESULT_PACKAGE_1 = VALUE #( BASE RESULT_PACKAGE_1 (
objectid = lv_hieid_hier_cust_grp
h_hienm = CONV char30( lc_hier )
) ).
" Reading result_package_3 to check if we already have a
" root node (customer group) added. If we have it already,
" then it's a case of multiple customers per customer group.
" So skip adding customer group again and keep adding the
" customers for it.
" ***(Don't use binary search here.)***
" Sy-subrc = 0 means customer group already exists.
READ TABLE RESULT_PACKAGE_3 INTO RESULT_FIELDS_3
WITH KEY h_hiernode = <source_fields>-/bic/zccst_grp.
IF sy-subrc <> 0.
"See if we already have this customer group in hierarchy.
READ TABLE lt_existing_parents ASSIGNING
FIELD-SYMBOL(<ep_cust_grp>)
WITH KEY hieid = lv_hieid_hier_cust_grp
nodename = <source_fields>-/bic/zccst_grp
BINARY SEARCH.
IF sy-subrc = 0.
" We have the customer group already. So write its node_id
" from lt_existing_parents table to result_package_3.
" T_level will be 1 as it's root node.
RESULT_PACKAGE_3 = VALUE #( BASE RESULT_PACKAGE_3 (
objectid = lv_hieid_hier_cust_grp
h_nodeid = <ep_cust_grp>-nodeid
h_hiernode = <source_fields>-/bic/zccst_grp
h_iobjnm = lc_hier_node
h_tlevel = 01 "Always 01
) ).
" Store parent_id. Will be used when adding customers.
lv_prnt_id = <ep_cust_grp>-nodeid.
ELSE.
" Increment lv_index before (manually) adding any node.
increment lv_index.
" Customer grp neither already added to result_package_3,
" nor exists in backend. So, it's a new customer group.
RESULT_PACKAGE_3 = VALUE #( BASE RESULT_PACKAGE_3 (
objectid = lv_hieid_hier_cust_grp
h_nodeid = lv_max_nodeid + lv_index
h_hiernode = <source_fields>-/bic/zccst_grp
h_iobjnm = lc_hier_node
h_tlevel = 01 "Always 01
) ).
" Store parent_id. Will be used when adding customers.
lv_prnt_id = lv_max_nodeid + lv_index.
ENDIF.
ELSE.
" Store parent_id. Will be used when adding customers.
lv_prnt_id = RESULT_FIELDS_3-h_nodeid.
ENDIF.
" Increment lv_index before (manually) adding any node.
increment lv_index.
" Check if customer already exists.
" If it does, copy the node_id from lt_existing_parents to
" result_package_3.
" *** But do not copy the parent_id. ***
" Parent_ID must be entered from lv_prnt_id var.
" This will take care of the case where a customer is moved
" from one customer group to another customer group.
READ TABLE lt_existing_parents ASSIGNING
FIELD-SYMBOL(<ep_cust_grp_1>)
WITH KEY hieid = lv_hieid_hier_cust_grp
nodename = <source_fields>-customer
BINARY SEARCH.
IF sy-subrc = 0.
RESULT_PACKAGE_3 = VALUE #( BASE RESULT_PACKAGE_3 (
objectid = lv_hieid_hier_cust_grp
h_nodeid = <ep_cust_grp_1>-nodeid
h_iobjnm = lc_customer
customer = <source_fields>-customer
h_parentid = lv_prnt_id "IMP!
h_tlevel = 02 "Always 02
) ).
ELSE.
" New customer. Add to result_package_3.
RESULT_PACKAGE_3 = VALUE #( BASE RESULT_PACKAGE_3 (
objectid = lv_hieid_hier_cust_grp
h_nodeid = lv_max_nodeid + lv_index
h_iobjnm = lc_customer
customer = <source_fields>-customer
h_parentid = lv_prnt_id
h_tlevel = 02 "Always 02
) ).
ENDIF.
" Fill the Text in result_package_4 table.
" Key is used as text here.
" Always give value for langu. Texts only work
" if a language is supplied.
RESULT_PACKAGE_4 = VALUE #( BASE RESULT_PACKAGE_4 (
objectid = lv_hieid_hier_cust_grp
langu = lc_en
h_hiernode = <source_fields>-/bic/zccst_grp
txtlg = <source_fields>-/bic/zccst_grp
txtmd = <source_fields>-/bic/zccst_grp
txtsh = <source_fields>-/bic/zccst_grp
) ).
ENDIF.
ENDLOOP.
ENDIF.
A hierarchy with around 25,000 nodes and the DTP loads within
4 seconds.
lv_objectid_hier_zcustomer TYPE /bi0/oiobjectid.
READ TABLE RESULT_PACKAGE_3 INTO RESULT_FIELDS_3
WITH KEY objectid = lv_objectid_hier_zcustomer
/bic/zcustomer = <source_fields>-/bic/zcustomer
h_parentid = lv_prnt_id.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |