ALV Tree – How to change Totals (AVG, SUM, MIN, MAX) – Step by step Tutorial
I noticed that loads of people has problems to change the ALV tree total as I had as well. It was my very first time working with ALV Tree and I like to write exactly when I am not used to. It gets more accurate steps. Please fell free to send a suggestion.
Our useful and old friend Fieldcat has an parameter to do total for ALV Tree, it is fieldcat-h_ftype = ‘AVG’, or ‘SUM’, or ‘MIN’, or ‘MAX’. And you can use BCALV_TEST_SIMPLE_TREE
That’s great and helps a lot for sure. But what to do when you need to change it.
As you can see in the follow image the Average is being calculated as ( the two lines values summarized) / two items line.
But I want to customize this value ( I changed the values during debug just to show that you can o whatever you want with those values. No logic in here. Crazy numbers)
To change the values we need to add nodes ‘Manually’. OK, let’s go to the code now. No more blablabla.
After tree1->set_table_for_first_display …. (If you are not here yet use SAP sample BCALV_TREE_DEMO)
TIP – Do not use the table with data in set_table_for_first_display . The metod will delete every data in there. Use an empty table using exactly the same structure that contains your data ( E.g.: it_outtab = it_data_empty )
Next step: perform create_hierarchy.
I’m using field symbol for it but you can see the other way in here: BCALV_TREE_DEMO same perform name.
Ok, so the first perform will create the 1ST hierarchy and the second one will create the child.
Debbuging ADD_HIERARCHY, that is almost the same code that you will have in ADD_CARRID_LINE in BCALV_TREE_DEMO, you will notice that the
parameter is_outtab_line in call method tree1->add_node is EMPTY!!!! And what we do here? We mess up! In a really good way. Go there and change everything that you need.
And that is all, you just fill the empty fields with the values that you want to show up and delete the update calculations
* calculate totals
* CALL METHOD tree1->update_calculations.
It will overwrite the values that you changed.
Hope it helps,