Mar 30

SAP NCo 3.0: How-to Pass Table Parameters to SAP RFC

Since I posted step-by-step instructions on how to build an NCo RFC client, the  request I most often get is how to populate and pass a table of data as a parameter to an SAP RFC.  In this blog, I will walk through the steps to pass a table parameter to from a .NET program to SAP via NCo 3.0.  I will not be covering the basics of how to set up NCo 3.0 as a RFC client as I have already covered that.

Click here to request a .zip file containing a copy of the source code.

The BAPI

This sample program uses BAPI_MATERIAL_GETLIST since it uses table parameters.  In SAP, table parameters can act as both importing and exporting parameters.  BAPI_MATERIAL_GETLIST uses the following table parameters as import parameters:

MATNRSELECTION
MATERIALSHORTDESCSEL (not used in this sample)
MANUFACTURERPARTNUMB (not used in this sample)
PLANTSELECTION (not used in this sample)
STORAGELOCATIONSELECT (not used in this sample)
SALESORGANISATIONSELECTION (not used in this sample)
DISTRIBUTIONCHANNELSELECTION (not used in this sample)

BAPI_MATERIAL_GETLIST uses these table parameters as export parameters:

MATNRLIST
RETURN

All of the import table parameters are selection-table style table parameters.  SAP Help does a good job of explaining selection tables.  For sake of brevity, my sample utilizes only one of the import table parameters (MATNRSELECTION).

Design the Form

This solution consists of a group box with controls to build a selection table, a list view to display the results, and a HUGE “Go” button (all prototype programs need a huge “Go” button!).

SAP Connection Information in App.Config

The SAP connection information is stored in App.Config.  Modify the configuration file to point to your SAP application server with a valid username, password, and client.

The Code

The key piece of code to pass a table of data to a SAP RFC via NCo is below. Before you can add records to the importing table parameter, you need to instantiate a reference to the function using a IRfcFunction object.  Once you have a reference to the RFC, you can instantiate an IRfcTable object for the table parameter which you want to use.  Use the .Append() method to create a new row in the IRfcTable object.  When you call the .Append() method, the current index property of the table is set to the newly appended line.  At this time, you can use the IRfcTable.SetValue() method to populate the data fields.

Once the table data been added, you can invoke the RFC.  Since all of the objects are created with references to each other, the data table and all its rows are passed to SAP.

            //Processing Before SAP Function Call
            ...

            //Get destination
            RfcDestination SapRfcDestination =
              RfcDestinationManager.GetDestination("DEV");

            //Get SAP Repository
            RfcRepository SapRfcRepository = SapRfcDestination.Repository;

            //Set function
            IRfcFunction BapiMaterialGetList =
              SapRfcRepository.CreateFunction("BAPI_MATERIAL_GETLIST");

            //Get reference to table object
            IRfcTable So_MATNR = BapiMaterialGetList.GetTable("MATNRSELECTION");

            //Add select option values to MATNRSELECTION table
            foreach (ListViewItem lvi in listViewMATNR.Items)
            {
                //Create new MATNRSELECTION row
                So_MATNR.Append();

                //Populate current MATNRSELECTION row with data from list
                So_MATNR.SetValue("SIGN", lvi.SubItems[0].Text);
                So_MATNR.SetValue("OPTION", lvi.SubItems[1].Text);
                So_MATNR.SetValue("MATNR_LOW", lvi.SubItems[2].Text);
                So_MATNR.SetValue("MATNR_HIGH", lvi.SubItems[3].Text);

            }

            //Set Maximum number of rows
            BapiMaterialGetList.SetValue("MAXROWS", txtMaxRows.Text);

            //Call BAPI on SAP system
            BapiMaterialGetList.Invoke(SapRfcDestination);

            //Process RFC return
            ...

Usage

I am assuming that, by now, you have familiarized yourself with SAP selection tables.  Therefore, I will not go into too much detail on the usage of this sample program.  BAPI_MATERIAL_GETLIST is a function that returns a list of materials based on the selection criteria provided.  To add entries to the selection table, select a sign and option, then add a low and high (optional) value.  Once complete, click the “+” button to add the entry to the selection table.  To remove an entry from the selection table, highlight the entry in the list view and press the “-” button.

I entered the following entries to the selection table:

NOTE: If a SAP material number consists solely of numerical digits, it will be left padded with zeroes.  In order to ensure proper data selection, I implemented a private method ConvertMatnr() to ensure the proper formatting of material numbers.

Results

Here are the results from the above data selection set when run against our development SAP instance.  Your mileage may vary:

As you can see, passing table parameters to a SAP RFC via SAP NCo 3.0 is relatively easy and straight-forward.  Happy coding!

Additional Information

Follow these links for more information about NCo 3.0 programming:

Avatar

About The Author

Former Partner and Senior Consultant at DataXstream, Craig Stasila played a key role in developing DataXstream. For current information regarding Craig Stasila please see his Linkedin profile.

9 Comments

  1. Avatar
    Eduardo
    June 14, 2012 at 1:45 pm · Reply

    Congratulations for this example.
    I have a doubt: When many users use an application for only view the sap table , we must get additional license for each user ?

    Thanks.

    • Avatar
      Craig Stasila
      June 14, 2012 at 1:52 pm · Reply

      While I cannot speak authoritatively on SAP licensing, you should be covered as long as you have a license for each named user that logs in to the SAP environment. For more information, please contact you SAP sales representative.

  2. Avatar
    vicky
    October 5, 2012 at 3:32 am · Reply

    hi ,
    i’m trying create material from .Net front end using SAP .Net Connector .while building the application i’m getting this error ..please help me on this

    “The referenced assembly “sapnco” could not be resolved because it has a dependency on “System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project.”

    thanks in advance

    vicky

  3. Avatar
    ts
    August 14, 2013 at 6:03 am · Reply

    Hi,

    Craig, thank you for your excelent presentation.
    I’m working on BAPI_FIXEDASSET_GETLIST and have one problem only:

    //Set input parameters
    BapiAssetGetList.SetValue(“COMPANYCODE”, “SI10”);
    BapiAssetGetList.SetValue(“MAXENTRIES”, “10”);
    BapiAssetGetList.SetValue(“REQUESTEDTABLESX”, ” X”);

    In last line I get following error:

    “PARAMETER REQUESTEDTABLESX of FUNCTION BAPI_FIXEDASSET_GETLIST (SETTER): cannot convert String into STRUCTURE BAPI1022_REQUESTEDTABLESX”

    Can you please help me how to specify this tsructure in c#? I have searched all around the net and didn’t find solution.

    Thanks in advance and good day.

    Tomaz

  4. Avatar
    ts
    August 14, 2013 at 6:35 am · Reply

    Hello Craig,

    I found solution my self:

    SAP.Middleware.Connector.IRfcStructure requestedTablesX = BapiAssetGetList.GetStructure(“REQUESTEDTABLESX”);
    requestedTablesX.SetValue(“TIMEDEPENDENTDATA”, “X”);
    BapiAssetGetList.SetValue(“REQUESTEDTABLESX”, requestedTablesX);

    Best regards,
    Tomaz

  5. Avatar
    Henry
    November 18, 2015 at 12:47 pm · Reply

    Hi, I am struggling to get a code sample thta uses the functions “RSDRI_CUBE_WRITE_PACKAGE_RFC” and “RSDRI_ODSO_MODIFY_RFC” with the .Net connector.

    Please can you help?

    Thanks.

  6. Avatar
    Jonathan
    June 14, 2016 at 12:07 pm · Reply

    Excelent!!

    Thanks.

  7. Avatar
    Ton Slijpen
    December 1, 2017 at 6:46 am · Reply

    Hi Graig

    I looking for an example to downdload table T512W data but I don’t know the BAPI yo use.

    Could you please help?

    Thanks

    Ton

Leave a reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.