For specific reasons, it may be necessary to count the number of values that the user has selected, in a multiple-value characteristic.
The task could easily be performed if the characteristic values are well defined, that is, they are available in the characteristic ‘Values’ tab: this can be accomplished, for instance, by means of an object dependency that will go through the values, counting the selected ones.
But what about if the values are dynamically set?
In such a case, we will anyway use an object dependency that will call a variant function (user-defined function), that in turn will count the selected values.
The multiple values characteristic will not be part of the function interface, as it is not possible to use this kind of characteristic in variant function. But this is not blocking us to read anyway the characteristic selected values, and return their count to another characteristic.
Let’s try to implement a simple trial about it.
So, we will have a multiple values characteristic, here I called it TEST_FLAVIO
and a single value characteristic, TEST_FLAVIO_COUNT, that will contain the number of values that have been selected in the previous characteristic. This will be the output of the user defined function.
We have to also define an input characteristic for the same function: we could use a dummy one, with a default value ‘X’, for example.
All these characteristics will be part of our class.
As anticipated, we need an user defined function, here called ZTEST_FLAVIO. It’s ‘Characteristics’ section will look like the following:
The function module code itself, will be the following:
*” REFERENCE(GLOBALS) TYPE CUOV_00
*” MATCH STRUCTURE CUOV_01
*” QUERY STRUCTURE CUOV_01
DATA: i_used_values TYPE cudbt_vallist,
w_char TYPE cudbt_key,
w_instance TYPE cudbt_instance,
w_counter TYPE i,
w_value TYPE cuov_01-atflv.
* Initializing the instance number
IF globals-self IS INITIAL.
w_instance = 1.
w_instance = globals-self.
* Reading the chosen values in the multiple value characteristic
w_char = ‘TEST_FLAVIO’.
CALL FUNCTION ‘CUPR_GET_VALLIST’
instance = w_instance
characteristic = w_char
values = i_used_values
unknown_instance = 1
unknown_characteristic = 2
not_multivalued = 3
not_found = 4
internal_error = 5
OTHERS = 6.
IF sy-subrc = 4.
* Counting the values, and send back the number
IF i_used_values IS NOT INITIAL.
DESCRIBE TABLE i_used_values LINES w_counter.
IF w_counter > 0.
MOVE w_counter TO w_value.
CALL FUNCTION ‘CUOV_SET_FUNCTION_ARGUMENT’
argument = ‘TEST_FLAVIO_COUNT’
vtype = ‘NUM’
num_val = w_value
match = match
existing_value_replaced = 1
OTHERS = 2.
IF sy-subrc <> 0.
What the code is doing is quickly explained: first, thanks to the FM ‘CUPR_GET_VALLIST’, we read the multiple values characteristic and save them in a local variable. Then, we just count them and return this value to the output characteristic by means of the FM ‘CUOV_SET_FUNCTION_ARGUMENT’.
Now, we need an object dependency (Procedure) that will call the user defined function. Here it is:
We will add it to our configuration profile.
That’s all. Let’s go and give this a quick test inside CU50:
Here above, 4 values have been selected in the multiple values characteristic. Change them, and the counting characteristic will change accordingly: