LO-VC: counting the number of selected values in multiple-value characteristic
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:
FUNCTION ZTEST_FLAVIO.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” REFERENCE(GLOBALS) TYPE CUOV_00
*” TABLES
*” MATCH STRUCTURE CUOV_01
*” QUERY STRUCTURE CUOV_01
*” EXCEPTIONS
*” FAIL
*” INTERNAL_ERROR
*”———————————————————————-
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.
ELSE.
w_instance = globals-self.
ENDIF.
* Reading the chosen values in the multiple value characteristic
w_char = ‘TEST_FLAVIO’.
CALL FUNCTION ‘CUPR_GET_VALLIST’
EXPORTING
instance = w_instance
characteristic = w_char
IMPORTING
values = i_used_values
EXCEPTIONS
unknown_instance = 1
unknown_characteristic = 2
not_multivalued = 3
not_found = 4
internal_error = 5
OTHERS = 6.
IF sy-subrc = 4.
CLEAR i_used_values.
REFRESH i_used_values.
ENDIF.
* 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’
EXPORTING
argument = ‘TEST_FLAVIO_COUNT’
vtype = ‘NUM’
num_val = w_value
TABLES
match = match
EXCEPTIONS
existing_value_replaced = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
ENDIF.
ENDIF.
ENDFUNCTION.
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:
Thanks.
Flavio
flavio ciotola,
Thanks for writeup on such an useful topic, Your replies and knowledge sharing help all of us a lot.
Will wait for few more, keep going. 🙂
Regards
Ritesh
Thank you, Ritesh.
I'm very happy about it 🙂
Flavio
Excellent Blog Flavio, Do keep writing. Your Knowledge on VC is very deep and remarkable.
Kind Regards
Atul
Thank you, Atul, very kind of you.
🙂
Flavio
Hello Flavio,
I followed you interesting suggestion trying to solve my issue. A characteristic was erroneously defined how multivalues but used as single value. The characteristic is intensively used and I cannot change its definition. So I was looking your suggestion; to read the “multi value” characteristic and export its single value in a second characteristic. I have created the function code as you suggest but my problem is that the instance value is always initial during the configuration. So my question is: in your example have you saved the configuration to generate the instance or have you used any other tricks to obtain it?
In my example "globals-self" is always initial during the configuration so the function execution is terminated.
Thank you in advance.
Nicola Molinaroli
Hello Nicola,
Indeed, globals-self is initial also in my case.
Could you please check what is the exception thrown by the 'CUPR_GET_VALLIST' function module?
Thanks and regards,
Flavio
Hello Flavio,
Sorry for my delay with whom I answer to you. I apologize with you. I did the test quickly too.
Today, with more, patience, all works correctly. Big idea. Big trick.
Best regards.
Nicola
.
Hello Former Member,
Happy to hear that 😉
Thank you and ciao,
Flavio
Hello, Flavio,
I try to adopt your solution on my issue, which is to reset classified materials’ MENGE respectively according to multiple values from a characteristic. I just wanna see if the quantity of classified materials can be different via function module or user exit/badi. However, it gets stuck as follows.
Thanks in advance,
Jun
Hello Jun,
From the above debugging, sy-subrc = 2 leads to unknown characteristic (CLASSIFICATION_CSTC).
Could you please share your configuration screen, with the involved characteristic?
Thanks and regards,
Flavio
Hi, Flavio,
Thanks for your quick answer, I figured it out that the import characteristic has to be upper-case sensitive. Nevertheless, I’m still wondering how the function module could overwrite STPO-MENGE by classified material respectively, or how to manipulate the menge in each loop of classified material replacement.
Alternatively, Is there any way to reassign activity number(RESBD-VORNR) in the material list under process order? So far, the reference plpo_cfmod-vornr only can affect afvgd-vornr.
Best Regards,
Jun
Hi, Flavio,
I gotta close this issue which has been solved in BADI.
Ciao,
Jun