goenning / SharpSapRfc

Making SAP RFC calls even easier with .NET
MIT License
84 stars 27 forks source link

Cannot create invoices when using BAPI transactions #39

Open kawazoe opened 8 years ago

kawazoe commented 8 years ago

I am trying to create an invoice in SAP using the BAPI_ACC_DOCUMENT_POST function. The call goes through correctly and I get a valid document id from the response but the invoice never appears in SAP. This is expected since we use BAPI transaction. This mean that I need to call BAPI_TRANSACTION_COMMIT once I am done creating my document. The final code looks something like this:

var result = _connection.ExecuteFunction(new CreateInvoiceFunction(invoiceDto));
_connection.ExecuteFunction("BAPI_TRANSACTION_COMMIT");

return result;

Even then, the invoice does not appear in SAP. We looked at the SAP traces and we see both functions called correctly. This works when using the SAPNCo library directly. I also tried to wrap both calls in a BeginContext and EndContext using the RfcSessionManager class but it also didn't changed anything.

Any idea of what might be causing this?

goenning commented 8 years ago

Hi @kawazoe. Could you share how did you try to use RfcSessionManager with SharpSapRfc?

I usually don't need it because I wrap both functions into a single Z function, but RfcSessionManager should work as well.

kawazoe commented 8 years ago

Sure! Basically, I am using a PlainSapRfcConnection behind the scenes so using RfcSessionManager with the previous code sample will look like this:

RfcSessionManager.BeginContext(_connection.Destination);

var result = _connection.ExecuteFunction(new CreateInvoiceFunction(invoiceDto));
_connection.ExecuteFunction("BAPI_TRANSACTION_COMMIT");

RfcSessionManager.EndContext(_connection.Destination);
return result;

Since the same destination should be used for both function calls, this should work flawlessly. Also, obviously, this will only work with plain connections, not soap ones.

I have to say, I haven't tried to use it without the manual call to BAPI_TRANSACTION_COMMIT and I am not sure if I should keep it there.

goenning commented 8 years ago

Try comparing both traces (SAPNCo and SAPNCo + SharpSapRfc) on SAP side, there should be a difference. I don't have a SAP instance here, but try to check if both functions are running under the same user session.

This is a good ref for SAP NCo http://logosworld.com/docs/SAP/Connector/SAP%20Connector%20for%20Microsoft%20.NET%20%20NCo_30_Overview.pdf

Just saw that there is a ISessionProvider, but never tried.