NCo 3: Fun with BAPIs, part 1
In my previous post, I discussed ways of using the .NET connector (NCo 3) to connect your client application with your R/3 back end. Let’s assume you’ve done that. Now what?
NCo 3 is designed to enable you to work with BAPIs. SAP delivers hundreds of these fancy function modules for us to use. They are handy for everything from getting a list of employee benefit elections out of HCM to creating a cash journal document in FI. In fact, by calling various BAPIs you could whip together powerful applications in your .NET environment, leveraging SAP’s own code for the hard stuff. Myself, I’ve been thinking about a custom open enrollment application that leverages the available benefits-related BAPIs in HCM.
But let’s discuss how you call a BAPI using NCo 3. Once you have a destination established, all BAPIs and RFC-enabled function modules in that SAP system become available to you. Just call it by name, and you get an instance of the BAPI in your .NET client app with all the import, export, changing, and tables parameters — all appropriately typed, all ready to use. Here’s how you do it. First, you need a variable to hold the BAPI.
Dim getBalances As IRfcFunction ‘ BAPI = function module + fancy stuff
Now, you call the CreateFunction method of the repository object, passing it the name of the BAPI you want. The repository object is a sub-object of the RfcDestination object, and you know all about RfcDestination objects from my previous post.
getBalances = myDest.Repository.CreateFunction(“BAPI_GL_GETGLACCPERIODBALANCES”)
Now, I know from studying this BAPI in SE37 that it has some simple field parameters, a structure (the RETURN parameter, where success and error messages come from,) and a table parameter. This particular BAPI works by passing it a GL account, fiscal year, and some other simple parameters, and in return, you get a table of account balances broken out by period as well as the fiscal year opening balance. So, I need to set some input parameters before I can do anything. Let’s do it this way.
.SetValue(“GLACCT”, “00000” & strGLAccount)
So, with the SetValue method, we identify which parameter we are setting, and giving it a value. Think of it like a key-value pair where the key is the SAP name of the parameter. One thing to remember about some of these parameters is that within SAP, often, conversion programs run automatically that, for instance, pad some fields with leading zeroes. In the case of the GL account, SAP stores that 10-character field with leading zeroes, and so I have make sure the value I pass is padded out to a length of 10 characters before I pass it to my BAPI. (I admit, my example isn’t very elegant.) If you find the BAPI doesn’t like what you are passing to it, make sure something like this isn’t going on, and you are passing what the BAPI expects.
Once we pass in our input parameters, we call the RfcFunction’s Invoke method. The Invoke method takes your destination object as its parameter. Behind the scenes, this is where NCo 3 logs into SAP, executes your BAPI with the parameters you provided, and returns whatever it is supposed to. How do you retrieve this output? The RfcFunction object comes with several Get____ methods, including GetTable, GetStructure, GetString, etc.
Dim tblAccountBalances As IRfcTable
Dim strucReturn As IRfcStructure
getBalances.Invoke(myDest) ‘ <—- where the magic happens
tblAccountBalances = getBalances.GetTable(“ACCOUNT_BALANCES”)
strBalanceCarryForward = getBalances.GetString(“BALANCE_CARRIED_FORWARD”)
strucReturn = getBalances.GetStructure(“RETURN”)
In the code above, after invoking the BAPI, I simply get the output by calling their SAP name. Grabbing values from simple fields is easy with methods like GetString or GetDecimal. GetTable returns an IRfcTable type. The IRfcTable type is fairly intuitive to use and behaves a bit like a .NET DataTable, and the IRfcStructure sort of behaves like a one-row table. That’s a gross oversimplification, however. Both objects have their peculiarities, and in my next post, I will discuss some of the things I learned about using them effectively.