Lorenzo
Lorenzo

Reputation: 187

Set query parameters of RFC_READ_TABLE using win32com module?

I'm trying to port to Python a SAP table download script, that already works on Excel VBA, but I want a command line version and I would prefer to avoid VBScript for a number of reasons that go beyond the goal of this post.

I'm stuck at the moment in which I need to fill the values in a table

from win32com.client import Dispatch
Functions = Dispatch("SAP.Functions")

Functions.Connection.Client = "400"
Functions.Connection.ApplicationServer = "myserver"
Functions.Connection.Language = "EN"
Functions.Connection.User = "myuser"
Functions.Connection.Password = "mypwd"
Functions.Connection.SystemNumber = "00"
Functions.Connection.UseSAPLogonIni = False
if (Functions.Connection.Logon (0,True) == True):
    print("Logon OK")
    RFC = Functions.Add("RFC_READ_TABLE")
    RFC.exports("QUERY_TABLE").Value = "USR02"
    RFC.exports("DELIMITER").Value = "~"
    #RFC.exports("ROWSKIPS").Value = 2000
    #RFC.exports("ROWCOUNT").Value = 10

    tblOptions = RFC.Tables("OPTIONS")
    #RETURNED DATA
    tblData = RFC.Tables("DATA")
    tblFields = RFC.Tables("FIELDS")

    tblFields.AppendRow ()

    print(tblFields.RowCount)
    print(tblFields(1,"FIELDNAME"))
    # the 2 lines above print 1 and an empty string, so the row in the table exists

Until here it is basically copied from VBA adapting the syntax. In VBA at this point I'm able to do

tblFields(1,"FIELDNAME") = "BNAME"

if I do the same I get an error because the left part is a function and written that way it returns a string. In VBA it is probably a bi-dimensional array.

I unsuccessfully tried various approaches like

tblFields.setValue([{"FIELDNAME":"BNAME"}])
tblFields(1,"FIELDNAME").Value = "BNAME"
tblFields(1,"FIELDNAME").setValue("BNAME")
tblFields.FieldName = "BNAME" ##kinda desperate 

The script works, without setting the FIELDS table, for outputs that produce rows shorter than 500 chars. This is a SAP limit in the function.

I know that this is not the best way, but I can't use the SAPNWRFC library and I can't use librfc32.dll. I must be able to solve this way, or revert to the VB version.

Thanks to anyone who will provide a hint

Upvotes: 1

Views: 1226

Answers (1)

un1e4shed
un1e4shed

Reputation: 11

After a lot of trial and error, i found a solution. Instead of adding row by row to the "OPTIONS" or "FIELDS" tables, you can just submit a prefilled table. This should work:

tblFields.Data = (('VBELN', '000000', '000000', '', ''),
                  ('POSNR', '000000', '000000', '', ''))

same here:

tblOptions.Data = (("VBELN EQ '2557788'",),)

Upvotes: 1

Related Questions