Skip to Content

The Problem:

How many times has this happened to you: you’re sitting at your desk, quietly banging away at the keyboard, pumping out simple, elegant ABAP code, when you user sends you an e-mail saying “and oh, by the way, those two radio-buttons on the new screen – both should be de-selected.” The request, although unusual, is reasonable. Users must be forced to make decision about which option to choose. There should be no default button selected.

Never? Well, OK, it doesn’t happen very often, but I got a request like this a while ago, and it turned out not to be so simple.

INITIALizing both of them to blank didn’t work. The first radio-button was still selected.

I tried using three radio-buttons, selecting and hiding the first one. Still no luck – the first one was still selected.

Finally, I searched SCN, and found a few discussions about this, suggesting the same things that I had tried but found that didn’t work.

What to do?

The Solution:

This is the code that I came up with:

REPORT ztest.

DATAold_no,
      old_yes
.

SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN POSITION 1.
SELECTION-SCREEN COMMENT 1(21) text001.           “ Pick one

SELECTION-SCREEN POSITION 23.
PARAMETERS: p_no AS CHECKBOX USER-COMMAND ucom1.
SELECTION-SCREEN COMMENT 25(2) text002.           “ Yes

SELECTION-SCREEN POSITION 30.
PARAMETERS  p_yes AS CHECKBOX USER-COMMAND ucom2.
SELECTION-SCREEN COMMENT 32(3) text003.           “ No

SELECTION-SCREEN END   OF LINE.

AT SELECTIONSCREEN.

      IF syucomm = ‘UCOM1’.
          
IF p_no IS INITIAL.
               
IF old_no IS INITIAL.
                     p_yes  
= ‘X’.
                     old_yes
= ‘X’.
                     old_no 
= ‘ ‘.
               
ELSE.
                     p_no 
= ‘X’.
                     p_yes
= ‘ ‘.
               
ENDIF.
          
ELSE.
                p_yes  
= ‘ ‘.
                old_yes
= ‘ ‘.
                old_no 
= ‘X’.
          
ENDIF.
     
ELSEIF syucomm = ‘UCOM2’.
          
IF p_yes IS INITIAL.
               
IF old_yes IS INITIAL.
                     p_no   
= ‘X’.
                     old_no 
= ‘X’.
                     old_yes
= ‘ ‘.
               
ELSE.
                     p_yes
= ‘X’.
                     p_no 
= ‘ ‘.
               
ENDIF.
          
ELSE.
               p_no   
= ‘ ‘.
               old_no 
= ‘ ‘.
               old_yes
= ‘X’.
          
ENDIF.
     
ENDIF.

A couple of caveats:

The code was developed for a module pool screen; this is a report.

This code can be modified to handle multiple check-boxes.

To report this post you need to login first.

8 Comments

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

  1. Paul Bakker

    Very interesting Rob — I’ve never used the ‘USER-COMMAND’ extension.

    If I may make a suggestion,  some comments sprinkled through the code might make it a little easier to digest.

    thanks!

    Paul

    (0) 
    1. Rob Burbank Post author

      The USER-COMMAND extension is quite useful at times.

      And yes, some comments might help as well. Although, I’m not sure what they would say… Something like “remember the old value”, “switch it back” and “don’t switch it back” I guess.

      Rob

      (0) 
  2. Vamsi Nagabairu

    Hi Rob,

    Instead of   IF in IF loops (Not Recommended ) you can go with Case as it gives much good performance.

    Case sy-ucom.


    when ‘UCOM1’.

             p_no = ‘ ‘.


    when ‘UCOM2’.

            p_yes = ‘ ‘.

    end case.


    and also more Readability of code.

    Best Regards,

    Vamsi

    (0) 
    1. Rob Burbank Post author

      I don’t think performance would be much of an issue in deciding to go with IF/ELSE or CASE, But it would probably better for readability, particularly if there is more than one checkbox/radio button.

      Rob

      (0) 
  3. Chaouki AKIR

    Bonjour,

    in my point of view there is a problem : since a checkbox is “ON”, there is no more possibilities to have both checkbox cleared

    ==> it is no more possible to get back to the initial situation.  Am I wrong ?

    Cordialement.

    (0) 
    1. Rob Burbank Post author

      No, you’re right, but that is a design feature, not a bug. 😉

      With radio buttons, you cannot get all of them un-selected.

      I guess I should have given some more background.:

      We have a custom table with many entries and are adding a new field – a flag that must be set to either (y)es or (n)o. So when a user creates a new entry, there will be a validation that insists that the flag be set to either ‘Y’ or ‘N’.

      But if the user wants to change an existing entry, since the field is there but blank, he or she is forced to make a decision without a default value. We do not want a value of space to be allowed.

      But in your case, if you want to return to null values, you could come up with some code to handle that as well.

      Rob

      (0) 

Leave a Reply