Purpose:

In different business scenarios generally there is a need to maintain Counter Values in our data model. The general purpose is we create a Number field and maintain values for it.

But there can be cases where we have a need to maintain a counter value in a field where the maximum length can be 4. So maintaining a Number counter will allow maximum of 9999 values. but if we create a characters counter from AAAA to ZZZZ, the applicable range will increase from 9999 to (26^4 = 456976). Case that we encountered in the name of Analysis Auth that can be maximum of 12 char but we wanted to have 8 characters reserved for Application Area, Sub Area and Access purpose and 4 char for counter. The example shown below works till counter of length 10.

Maintaining Character Counter is NOT a direct way to just increase it with one. We can achieve this with using some coding. Have created a class for it with specific methods.

This class takes a character value as input and by default returns the next incremented value. Example if you pass AAAD as input, the value will be changed and updated to AAAE.

In case if we want to have value increamented with multiple differences, say a difference of 3 for each counter (may be required if on same object – 3 parallel runs are processing). The character input AAAD can be passed with difference value 3; and the method will update the value to AAAG.

Objects:

To achieve this one can create a class ZCL_CHAR_COUNTER with following 3 methods in it.

  1. CHAR_COUNTER_INCREMENT
  2. INCREMENT_INITIATE
  3. INCREMENT_RECURSION

    ZCL_CHAR_COUNTER.jpg

    ZCL_CHAR_COUNTER Attributs.jpg

    ZCL_CHAR_COUNTER Methods.jpg

CHAR_COUNTER_INCREMENT: This is the method that will be called outside of this class with having two parameters. One is the import of incremental difference that is defaulted to value 1 and second is the Character Field with option changing. With additional check added to have only characters coming along with converting to upper case.

For the times we want to increment the counter – a internal method INCREMENT_INITIATE is called then it calls INCREMENT_RECURSION.

    char_counter_increment parameters.jpg

Attached are the files with code.

Results:

The input parameter to class is character string and a counter value and update the same character string with increated value. Below are example outputs for use of this class/method.

    Input 1.jpg

    Output 1.jpg

    Input 2.jpg

    Output 2.jpg

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply