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.

No comments:

Post a Comment