{"id":770,"date":"2013-10-29T22:56:42","date_gmt":"2013-10-29T21:56:42","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=770"},"modified":"2013-10-29T23:36:59","modified_gmt":"2013-10-29T22:36:59","slug":"abap-fill-bapi-structures-from-sap-table-structures-dynamically-2","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=770","title":{"rendered":"ABAP &#8211; Fill BAPI structures from SAP table structures dynamically"},"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=\"size-full wp-image-358 alignleft\" alt=\"SAP\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" width=\"44\" height=\"50\" \/>Due to fact that BAPI structures uses different field names than standard SAP tables I used to apply a trick I&#8217;m going to present in this article &#8211; map BAPI fileds to SAP fields in a Z-table and fill BAPI structures dynamically during runtime<!--more--><\/p>\n<p>In this article we will use as an example BAPI structure and SAP table for material master global data<\/p>\n<ul>\n<li>SAP table MARA<\/li>\n<li>BAPI structures called BAPI_MARA and BAPI_MARAX (save flags)<\/li>\n<\/ul>\n<p>At first we create a mapping table in the following format with some example data &#8211; it can be of course extended with further info you might find usefull, but we will use it in this simple format just to demonstrate the approach. Let&#8217;s call it <em>Z_BAPIMAP<\/em><\/p>\n<table>\n<thead>\n<tr>\n<th>TABNAME<\/th>\n<th>FIELDNAME<\/th>\n<th>BAPIFIELD<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>MARA<\/td>\n<td>MSTAE<\/td>\n<td>PURCH_STATUS<\/td>\n<\/tr>\n<tr>\n<td>MARA<\/td>\n<td>MATKL<\/td>\n<td>MATL_GROUP<\/td>\n<\/tr>\n<tr>\n<td>MARA<\/td>\n<td>NTGEW<\/td>\n<td>NET_WEIGHT<\/td>\n<\/tr>\n<tr>\n<td>MARA<\/td>\n<td>GEWEI<\/td>\n<td>UNIT_OF_WT<\/td>\n<\/tr>\n<tr>\n<td>MARC<\/td>\n<td>MINBE<\/td>\n<td>REORDER_PT<\/td>\n<\/tr>\n<tr>\n<td>MARC<\/td>\n<td>EISBE<\/td>\n<td>SAFETY_STK<\/td>\n<\/tr>\n<tr>\n<td>MARC<\/td>\n<td>BSTMI<\/td>\n<td>MIN_LOT_SIZE<\/td>\n<\/tr>\n<tr>\n<td>MARC<\/td>\n<td>BSTMA<\/td>\n<td>MAX_LOT_SIZE<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>When it comes to code filling the BAPI from SAP structures, you can encapsulate it to a nice class method, function module or a form &#8211; I&#8217;ll present a form\/perform code.<\/p>\n<pre lang=\"abap\" gutter=\"false\">FORM fill_bapi \r\n  USING i_source_line TYPE any\r\n        i_tabname     TYPE tabname16\r\n  CHANGING\r\n        c_bapi        TYPE any\r\n        c_bapix       TYPE any.\r\n\r\n  DATA:\r\n    lt_mapping TYPE TABLE OF z_bapimap.\r\n\r\n  FIELD-SYMBOLS:\r\n    &lt;fs_bapimap&gt;      TYPE z_bapimap,\r\n    &lt;fs_source_field&gt; TYPE any,\r\n    &lt;fs_bapi_field&gt;   TYPE any,\r\n    &lt;fs_bapi_fieldx&gt;  TYPE any.\r\n\r\n* The following SELECT command can be done \r\n* on global scope and here only global itab would be accessed\r\n  SELECT * \r\n    INTO TABLE lt_mapping\r\n    FROM z_bapimap\r\n    WHERE tabname = i_tabname.\r\n\r\n  LOOP AT lt_mapping ASSIGNING &lt;fs_bapimap&gt;.\r\n\r\n    ASSIGN COMPONENT &lt;fs_bapimap&gt;-fieldname OF STRUCTURE i_source_line TO &lt;fs_source_field&gt;.\r\n    CHECK &lt;fs_source_field&gt; IS ASSIGNED.\r\n\r\n*   Uncomment the following line in case you don't want blank \r\n*   values to be sent to SAP\r\n*   CHECK &lt;fs_source_field&gt; IS NOT INITIAL.   \r\n\r\n    ASSIGN COMPONENT &lt;fs_bapimap&gt;-bapifield OF STRUCTURE c_bapi TO &lt;fs_bapi_field&gt;.\r\n    ASSIGN COMPONENT &lt;fs_bapimap&gt;-bapifield OF STRUCTURE c_bapix TO &lt;fs_bapi_fieldx&gt;.\r\n    CHECK &lt;fs_bapi_field&gt;  IS ASSIGNED AND\r\n          &lt;fs_bapi_fieldx&gt; IS ASSIGNED.\r\n\r\n    &lt;fs_bapi_field&gt;  = &lt;fs_source_field&gt;.\r\n    &lt;fs_bapi_fieldx&gt; = 'X'.    \r\n\r\n  ENDLOOP.\r\n\r\nENDFORM.<\/pre>\n<p>Now we can call our new FORM to fill a BAPI structure from a SAP structure:<\/p>\n<pre lang=\"abap\" gutter=\"false\">FORM update_material\r\n  USING i_mara TYPE mara.\r\n\r\n  DATA:\r\n    l_bapi_mara  TYPE bapi_mara,\r\n    l_bapi_marax TYPE bapi_marax.\r\n\r\n  PERFORM fill_bapi\r\n    USING    i_mara\r\n             'MARA'\r\n    CHANGING l_bapi_bara\r\n             l_bapi_marax.      \r\n\r\n*  ... The rest of code for update material\r\n*  i.e. Call the BAPI function module ...\r\n*  ...and Rollback\/Commit work :-)\r\n\r\nENDFORM.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Due to fact that BAPI structures uses different field names than standard SAP tables I used to apply a trick I&#8217;m going to present in this article &#8211; map BAPI fileds to SAP fields in a Z-table and fill BAPI &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=770\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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 - Fill BAPI structures from SAP table structures dynamically","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}},"categories":[16,15,9,5],"tags":[446,100,179],"class_list":["post-770","post","type-post","status-publish","format-standard","hentry","category-abap","category-algorithms","category-development","category-tools","tag-abap","tag-bapi","tag-dynamic-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-cq","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/770","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=770"}],"version-history":[{"count":2,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/770\/revisions"}],"predecessor-version":[{"id":775,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/770\/revisions\/775"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}