Friday, March 10, 2017

Pull SAP Client Options from saplogin.ini

Here is some code that allows you to pull the client options from the saplogin.ini file. This in itself isn't really all that useful but I'll show you a session selection module in a future post and in that I use this "getlogon" module to allow me to provide the user with a list of SAP client options such as PRD, QAS, or DEV.


def getsaplogonoptions():
    import ConfigParser
    Config = ConfigParser.ConfigParser()
    Config.read("C:\\SAP\saplogon.ini")

    def ConfigSectionMap(section):
        dict1 = {}
        options = Config.options(section)
        for option in options:
            try:
                dict1[option] = Config.get(section, option)
                if dict1[option] == -1:
                    DebugPrint("skip: %s" % option)
            except:
                print("exception on %s!" % option)
                dict1[option] = None
        return dict1

    loginlist=[]
    j=0
    Descriptions = ConfigSectionMap("Description")
    for i in Descriptions:
        try:
            tmp=''
            name=str('item' + str(j))
            j+=1
            loginlist.append(Descriptions[name])
        except:
            j+=1
            continue
    return loginlist

Note that ConfigParser can be installed via pip install configparser. Documentation for configparser is available here.

Wednesday, March 8, 2017

Verifying a SAP Object Exist

Good Morning All

Here's a little example of how you can verify if an SAP control exist before calling it. You can get creative with this but this is the base. Credit to my coworker Todd for initially coming up with this, a while back.


def verify(session=None, control=None):
    while True:
        try:
            session.FindById(control)
            return session.FindById(control)
        except:
            # Do something else here if needed
            return


You could of course write this directly into your main script but having this as a separate module allows you to import it into your main script and then call it for each control as needed. Something like this.


from verify import verify
import win32com.client

SapGui = win32com.client.GetObject("SAPGUI").GetScriptingEngine
ses = SapGui.FindById("ses[0]")

material = 1234

ses.StartTransaction(Transaction="MM02")  # Start MM02 transaction
verify(ses, 'wnd[0]/usr/ctxtRMMG1-MATNR').text = material
verify(ses, 'wnd[0]/tbar[1]/btn[6]').Press()


The last two lines use the verify module. This will check if the control passed exist and if so preform the call on that control.