The below program demonstrate how to change the content of variant at run time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
REPORT ztestautoselectoption. *&--------------------------------------------------------------------&* *& Program Description: &* *& ----------------------- &* *& This demo program shows how to dynamically populate the data in a &* *& variant. &* *& &* *& Author: ABAPCOOKBOOK &* *& Website: www.abapcookbook.com &* ************************************************************************ ************************************************************************ * DATA DECLARATIONS * ************************************************************************ *Tables: TABLES: vbap. *Internal tables: DATA: gt_vbap TYPE STANDARD TABLE OF vbap, gt_vari TYPE STANDARD TABLE OF rsparams. *Field Symbols: FIELD-SYMBOLS: <fs_vbap> TYPE vbap. *Structures: DATA: gst_vari TYPE rsparams, gst_varidesc TYPE varid. *Variables: DATA: gv_subrc TYPE sy-subrc. *Constants: CONSTANTS: gc_write TYPE c VALUE 'W', gc_selmat TYPE rsparams-selname VALUE 'S_MATNR', gc_kind TYPE rsparams-kind VALUE 'S', gc_sign TYPE rsparams-sign VALUE 'I', gc_option TYPE rsparams-low VALUE 'EQ'. ************************************************************************ * SELECTION SCREEN * ************************************************************************ SELECT-OPTIONS: * Material Number. s_matnr FOR vbap-matnr OBLIGATORY. ************************************************************************ * CODE LOGIC * ************************************************************************ *Check if the variant used to run the program exists. CALL FUNCTION 'RS_VARIANT_EXISTS' EXPORTING report = sy-repid "Program Name variant = sy-slset "Variant Name IMPORTING r_c = gv_subrc EXCEPTIONS not_authorized = 01 no_report = 02 report_not_existent = 03 report_not_supplied = 04. IF gv_subrc EQ 0. * Select sales order data from table VBAP. SELECT * UP TO 10 ROWS FROM vbap INTO TABLE gt_vbap. IF sy-subrc EQ 0. * Retrieve the contents of the variant to the internal table 'GT_VARI'. CALL FUNCTION 'RS_VARIANT_CONTENTS' EXPORTING report = sy-repid "Program Name variant = sy-slset "Variant Name move_or_write = gc_write TABLES valutab = gt_vari "Variant Contents EXCEPTIONS variant_non_existent = 1 variant_obsolete = 2 OTHERS = 3. IF sy-subrc EQ 0. * Delete the current content of select option 'S_MATNR' from the variant. This is * done by deleting all 'S_MATNR' selname entries from material select option(GT_VARI). * We will then populate it with fresh new data later on. DELETE gt_vari WHERE selname EQ gc_selmat. * Building up and assigning new material numbers to * the table variant(GT_VARI). LOOP AT gt_vbap ASSIGNING <fs_vbap>. * Assigning the necessary values and parameters * to the select options via the table LT_VARI. gst_vari-selname = gc_selmat. gst_vari-kind = gc_kind. gst_vari-option = gc_option. gst_vari-sign = gc_sign. gst_vari-low = <fs_vbap>-matnr. * Appending the material number to the select option. APPEND gst_vari TO gt_vari. ENDLOOP. * Re-populating the variant description. gst_varidesc-mandt = sy-mandt. gst_varidesc-aename = sy-uname. gst_varidesc-aedat = sy-datum. gst_varidesc-aetime = sy-uzeit. * Creating a new variant with the new values. CALL FUNCTION 'RS_CHANGE_CREATED_VARIANT' EXPORTING curr_report = sy-repid "Program Name curr_variant = sy-slset "Variant Name vari_desc = gst_varidesc "Variant Description TABLES vari_contents = gt_vari "Variant Contents 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 EQ 0. * Variant has been populated with new data successfully. ENDIF. ENDIF. ENDIF. ENDIF. |
Please note that to test the above program, create a variant with dummy data.
Then, just select the variant, the program will automatically change the content of the select option ‘S_MATNR’ based on the data retrieved from VBAP-MATNR.