{"id":928,"date":"2014-02-10T09:46:42","date_gmt":"2014-02-10T08:46:42","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=928"},"modified":"2014-02-10T09:46:42","modified_gmt":"2014-02-10T08:46:42","slug":"abap-create-dynamic-variable-knowing-reference-tabname-and-fieldname","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=928","title":{"rendered":"ABAP &#8211; create dynamic variable knowing reference tabname and fieldname"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"alignleft size-full wp-image-358\" alt=\"SAP\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" width=\"44\" height=\"50\" \/>If you want to create a variable during runtime knowing only the reference table name and its field name (in string form), you can achieve it following the code presented in this article<!--more--><\/p>\n<pre lang=\"abap\">data:\r\n* Names of the table and its field\r\n  g_tabname type tabname,\r\n  g_fieldname type fieldname,\r\n\r\n* Data references to table and its field\r\n  gr_data_line type ref to data,\r\n  gr_data_field type ref to data,\r\n\r\n* Testing values\r\n  g_test_value1 type char255 value '2021_error',\r\n  g_test_value2 type char255 value '2021'.\r\n\r\nFIELD-SYMBOLS:\r\n  &lt;fs_table_line&gt; type any,\r\n  &lt;fs_table_line_field&gt; type any.\r\n\r\n* We want to create dynamic variable of type MARC-WERKS\r\ng_tabname = 'MARC'.\r\ng_fieldname = 'WERKS'.\r\n\r\n*Create dynamic line of given table name\r\nCREATE DATA gr_data_line TYPE (g_tabname).\r\nASSIGN gr_data_line-&gt;* to &lt;fs_table_line&gt;.\r\n\r\n*Get reference to the required field\r\nASSIGN COMPONENT g_fieldname  OF STRUCTURE &lt;fs_table_line&gt; to &lt;fs_table_line_field&gt;.\r\nCHECK sy-subrc = 0.\r\n\r\n*Create dynamic variable with the same \"structure\" like the table field\r\nCREATE DATA gr_data_field LIKE &lt;fs_table_line_field&gt;.\r\n\r\n*Validation of data entered (not using either check or value table)\r\n*just to make sure the value entered is accepted\r\n&lt;fs_table_line_field&gt; = g_test_value1.\r\nif &lt;fs_table_line_field&gt; = g_test_value1.\r\n  write:\/ 'OK'.\r\nelse.\r\n  write:\/ 'Error'.\r\nendif.\r\n\r\n&lt;fs_table_line_field&gt; = g_test_value2.\r\nif &lt;fs_table_line_field&gt; = g_test_value2.\r\n  write:\/ 'OK'.\r\nelse.\r\n  write:\/ 'Error'.\r\nendif.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to create a variable during runtime knowing only the reference table name and its field name (in string form), you can achieve it following the code presented in this article<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - create dynamic variable knowing reference tabname and fieldname","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[16,9],"tags":[446,29],"class_list":["post-928","post","type-post","status-publish","format-standard","hentry","category-abap","category-development","tag-abap","tag-dynamic"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-eY","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=928"}],"version-history":[{"count":1,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/928\/revisions"}],"predecessor-version":[{"id":929,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/928\/revisions\/929"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}