5 1 1 1 1 1 Rating 100% (1 Vote)
When building a program with multiple selection screens sometimes it may be needed to save the variant with entries only for one of the subscreens and not for the others. If you want to do that you have to firstly use FM 'RS_REFRESH_FROM_SELECTOPTIONS' to get all fields and values for all select-options, then you need to use FM 'RS_ISOLATE_1_SELSCREEN' to get the fieldnames for selected subscreen. When you have this data you can easily filter fileds and it's values to this which you have on your subscreen. After that run FM 'RS_CREATE_VARIANT' to create a new variant, if variant exists and you want to overwrite it then you have to use 'RS_CHANGE_CREATED_VARIANT'.  Few lines and you can use this in many developments.
 
Importing parameters:

I_DYNNR TYPE SY-DYNNR - Current Screen Number
I_VARNAME TYPE RSVAR-VARIANT - Variant Name
I_VARTEXT TYPE VARIT-VTEXT - Program variant short text

Implementation:

method create_variant_for_ss.
  dataft_params type table of rsparams.
  dataft_sscr type table of rsscr.
  datafs_sscr type rsscr.
  datafr_selopt type range of rsscr-name.
  datafs_selopt like line of fr_selopt.
  dataf_vari_desc type varid.
  dataft_vari_text type table of varit.
  datafs_vari_text type varit.
  dataft_vscreens type table of rsdynnr.
  datafs_vscreens type rsdynnr.
  dataf_retcode type c.



*get all selection screen parameters and values
  call function 'RS_REFRESH_FROM_SELECTOPTIONS'
    exporting
      curr_report           sy-repid
* IMPORTING
*   SP                    = SP
    tables
      selection_table       ft_params
   
exceptions
     not_found             1
     no_report             2
     others                3
            .
  if sy-subrc eq 0.
  endif.



*get fields of our subscreen
  call function 'RS_ISOLATE_1_SELSCREEN'
    exporting
      program           sy-repid
      dynnr             
i_dynnr
*     TABIX             = 0
*   IMPORTING
*     LAST_TABIX        = LAST_TABIX
    tables
      screen_sscr       ft_sscr
*     GLOBAL_SSCR       = GLOBAL_SSCR
   exceptions
     no_objects        1
     others            2
            .
  if sy-subrc eq 0.
    loop at ft_sscr into fs_sscr.
      fs_selopt-sign 'I'.
      fs_selopt-option 'EQ'.
      fs_selopt-low fs_sscr-name.
      append fs_selopt to fr_selopt.
    endloop.
*delete parameters and values not used on our subscreen
    delete ft_params where selname not in fr_selopt.
  endif.

*set variant global data
  move sy-mandt             to f_vari_desc-mandt.
  move sy-repid             to f_vari_desc-report.
  move i_varname            to f_vari_desc-variant.
  move sy-uname             to f_vari_desc-ename.
  move sy-datum             to f_vari_desc-edat .
  move sy-uzeit             to f_vari_desc-etime.
  move 'X'                  to f_vari_desc-environmnt.

*set description of variant
  move sy-mandt             to fs_vari_text-mandt.
  move sy-langu             to fs_vari_text-langu.
  move sy-repid             to fs_vari_text-report .
  move i_varname            to fs_vari_text-variant.
  move i_vartext            to fs_vari_text-vtext.
  append  fs_vari_text to ft_vari_text.

*set subscreen number
  fs_vscreens-dynnr i_dynnr.
  fs_vscreens-kind ''.
  append fs_vscreens to ft_vscreens.
*create variant
  call function 'RS_CREATE_VARIANT'
    exporting
      curr_report               = sy-repid
      curr_variant              
i_varname
      vari_desc                 
f_vari_desc
    
tables
      vari_contents             ft_params
      vari_text                 
ft_vari_text
      vscreens                  
ft_vscreens
    
exceptions
      illegal_report_or_variant 1
      illegal_variantname       2
      not_authorized            3
      not_executed              4
      report_not_existent       5
      report_not_supplied       6
      variant_exists            7
      variant_locked            8
      others                    9.
  if sy-subrc eq 7.
*variant already exists so ask if user wants to overwrite it
    call function 'POPUP_TO_CONFIRM_WITH_MESSAGE'
      exporting
        diagnosetext1  'Variant exists, do you want to overwrite it?'(001)
        textline1      ' '(002)
        titel          'Variant exists, do you want to overwrite it?'(001)
        cancel_display space
      
importing
        answer         f_retcode
      
exceptions
        others         1.
    if sy-subrc <> 0.
      exit.
    endif.
    if f_retcode eq 'J'.

*user agreed so change existing variant
      call function 'RS_CHANGE_CREATED_VARIANT'
        exporting
          curr_report                     sy-repid
          curr_variant                    
i_varname
          vari_desc                       
f_vari_desc
*   ONLY_CONTENTS                   = ONLY_CONTENTS
        tables
         vari_contents                   ft_params
         vari_text                       
ft_vari_text
*   VARI_SEL_DESC                   = VARI_SEL_DESC
*   OBJECTS                         = OBJECTS
   exceptions
     illegal_report_or_variant       1
     illegal_variantname             2
     not_authorized                  3
     not_executed                    4
     report_not_existent             5
     report_not_supplied             6
     variant_doesnt_exist            7
     variant_locked                  8
     selections_no_match             9
     others                          10
                .
      if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      endif.

    endif.

  endif.
endmethod.

Enjoy!