Skip to Content
Author's profile photo flavio ciotola

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

/wp-content/uploads/2015/10/img20001_818142.png

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.

/wp-content/uploads/2015/10/img20002_818144.png

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.

/wp-content/uploads/2015/10/img20003_818145.png

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:

/wp-content/uploads/2015/10/img20004_818150.png

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:

/wp-content/uploads/2015/10/img20005_818151.png

We will add it to our configuration profile.

That’s all. Let’s go and give this a quick test inside CU50:

/wp-content/uploads/2015/10/img20006_818152.png

Here above, 4 values have been selected in the multiple values characteristic. Change them, and the counting characteristic will change accordingly:

/wp-content/uploads/2015/10/img20007_818156.png

Thanks.

Flavio

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ritesh Dube
      Ritesh Dube

      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

      Author's profile photo flavio ciotola
      flavio ciotola
      Blog Post Author

      Thank you, Ritesh.

      I'm very happy about it 🙂

      Flavio

      Author's profile photo Atul Keshav
      Atul Keshav

      Excellent Blog Flavio, Do keep writing. Your Knowledge on VC is very deep and remarkable.

      Kind Regards

      Atul

      Author's profile photo flavio ciotola
      flavio ciotola
      Blog Post Author

      Thank you, Atul, very kind of you.

      🙂

      Flavio

      Author's profile photo Former Member
      Former Member

      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

       

      Author's profile photo flavio ciotola
      flavio ciotola
      Blog Post Author

      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
       

      Author's profile photo Former Member
      Former Member

      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

       

      .

       


      Author's profile photo flavio ciotola
      flavio ciotola
      Blog Post Author

      Hello Former Member,

      Happy to hear that 😉

      Thank you and ciao,

      Flavio

      Author's profile photo Former Member
      Former Member

      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.

      *"----------------------------------------------------------------------
      *"*"Local Interface:
      *"  IMPORTING
      *"     REFERENCE(GLOBALS) LIKE  CUOV_00 STRUCTURE  CUOV_00
      *"  TABLES
      *"      QUERY STRUCTURE  CUOV_01
      *"      MATCH STRUCTURE  CUOV_01
      *"  EXCEPTIONS
      *"      FAIL
      *"      INTERNAL_ERROR
      *"----------------------------------------------------------------------
      
        DATA: w_instance TYPE cudbt_instance,
      
              i_char     TYPE cudbt_vallist,
      
      *        w_char     TYPE cudbt_key,
      
              l_val      TYPE cudbt_val.
      
      
        IF globals-self IS INITIAL.
      
          w_instance = 1.
      
        ELSE.
      
          w_instance = globals-self.
      
        ENDIF.
      
      *  w_char = ‘CLASSIFICATION_CSTC’.
      
        CALL FUNCTION 'CUPR_GET_VALLIST'
          EXPORTING
            instance               = w_instance
            characteristic         = 'CLASSIFICATION_CSTC'
          IMPORTING
            values                 = i_char
          EXCEPTIONS
            unknown_instance       = 1
            unknown_characteristic = 2
            not_multivalued        = 3
            not_found              = 4
            internal_error         = 5
            OTHERS                 = 6.
      
        IF sy-subrc <> 0.
      
      * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      
      *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      
        ENDIF.
      *  IF sy-subrc = 4.
      *
      *    CLEAR i_char.
      *
      *    REFRESH i_char.
      *
      *  ENDIF.
      
      
      *w_char = ‘menge’.
      
        LOOP AT i_char INTO l_val.
      
      
          CALL FUNCTION 'CUPR_SET_VAL'
            EXPORTING
              instance               = w_instance
              characteristic         = 'menge'
              val                    = l_val
            EXCEPTIONS
              unknown_instance       = 1
              unknown_characteristic = 2
              internal_error         = 3
              OTHERS                 = 4.
      
          IF sy-subrc <> 0.
      
      * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      
      *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      
          ENDIF.
      
        ENDLOOP.

       

      Thanks in advance,

      Jun

      Author's profile photo flavio ciotola
      flavio ciotola
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      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.

        call function 'CUOV_SET_FUNCTION_ARGUMENT'
          exporting
            argument                = 'PLPOCFMOD_VORNR'
            vtype                   = 'CHAR'
            SYM_VAL                 = '0040'
          tables
            match                   = match
          exceptions
            existing_value_replaced = 01.
      
        if sy-subrc <> 0.
          raise internal_error.
        endif.

      Best Regards,

      Jun

      Author's profile photo Former Member
      Former Member

      Hi, Flavio,

      I gotta close this issue which has been solved in BADI.

      Ciao,

      Jun