[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6710
  • Last Modified:

SAP login script session count

This code below works nicely.  It logs me into SAP and I have some code beyond this that does some table reading, exporting and so on in SAP.  But this code kills the current login and simply logs right back into SAP.  So if I want to do a nested macro with multiple steps that include this code, it kills SAP and logs in again each time.  Instead, I'd like for the code to first count how many sessions are open for the current login.  If there are fewer than 5, I just want it to open a new session.  If the maximum of 5 has been reached, however, then I am okay with killing the current SAP login completely and logging back in.  There is a commented section in this code that might be close to what I want.   Any thoughts on how to tweak this code to accomplish my goal?
Dim Application As Variant

Set SapGuiAuto = GetObject("SAPGUI")
   Set Application = SapGuiAuto.GetScriptingEngine
   Set Connection = Application.OpenConnection("PRD")
   Set SapSession = Connection.Children(0)

If IsObject(WScript) Then
   WScript.ConnectObject SapSession, "on"
   WScript.ConnectObject Application, "on"
End If

SapSession.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "XXXXXXX"
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "YYYYYYY"
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
SapSession.findById("wnd[0]").sendVKey 0

'In case you are already logged in...
If SapSession.Children.Count > 1 Then
    SapSession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
    SapSession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").SetFocus
    SapSession.findById("wnd[1]/tbar[0]/btn[0]").press
End If

Open in new window

0
bsncp
Asked:
bsncp
  • 8
  • 7
1 Solution
 
Gustav BrockCIOCommented:
Open the connection first in your top function.
Then run the loop calling the nested function using the already opened connection.
Close your top function, thus closing the connection as well.

/gustav
0
 
bsncpAuthor Commented:
Thank you...but I don't understand what you mean by "Open the connection first in your top function"
0
 
Gustav BrockCIOCommented:
The top or calling function is the function that calls the nested "macros" or (sub)functions.

/gustav
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
bsncpAuthor Commented:
Okay, thank you. I see what you mean by that. However, I have tried moving sections of this code around and try to run the steps in an order that would accomplish my goal, but can't seem to get the right combination. If my code has the right elements but are perhaps not in the proper order, is it bad form for me to request that you make the modification to the code and post it here?
0
 
Gustav BrockCIOCommented:
Well, I can't, as I haven't SAP installed to test with.
But it would be this block that should somehow be moved upwards:

Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
Set Connection = Application.OpenConnection("PRD")
Set SapSession = Connection.Children(0)

If IsObject(WScript) Then
   WScript.ConnectObject SapSession, "on"
   WScript.ConnectObject Application, "on"
End If

Then pass SapSession and other variables you may need as parameters to the nested functions:

Call MyNestedFunction1(SapSession)
Call MyNestedFunction2(SapSession)
etc.

Set SapSession = Nothing
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing

/gustav
0
 
bsncpAuthor Commented:
I will give that a shot and reply. Thank you. My nested function works great after it logs in. I am just trying to solve the issue of killing a session when I could just add a session instead.  Once I figure that out...I'm good.
0
 
Gustav BrockCIOCommented:
Great!

/gustav
0
 
bsncpAuthor Commented:
I'm confused.  The block you suggest moving up is already at the very top of my code...can you elaborate?
0
 
Gustav BrockCIOCommented:
> .. it kills SAP and logs in again each time.  

Thus, if you want to run similar code five times, run the top code once only my moving it to a top/parent/outer function which calls your now reduced code five times:

Call MyNestedFunction1(SapSession)
Call MyNestedFunction2(SapSession)
Call MyNestedFunction3(SapSession)
Call MyNestedFunction4(SapSession)
Call MyNestedFunction5(SapSession)

Then is closes SapSession. This way SapSession it opened once only.
/gustav
0
 
bsncpAuthor Commented:
Sorry.  I should have been more clear.  I have jobs scheduled at different times throughout the day that run this code, mostly requiring just one session.  In other words, I am not running all 5 routines at the same time.

The annoyance with this code is if I happen to be logged into SAP already during my normal course of business at the time one of my scheduled routines runs.  It kills my current session and I lose whatever I was doing.  Whenever the code runs, I want it to look to see if a session is already open.  If so...open a new session.  If not, start SAP and open a session.

Right now it starts SAP and a new session every time it runs.
0
 
Gustav BrockCIOCommented:
Oh, I see.
But aren't you already performing such a check:

'In case you are already logged in...
If SapSession.Children.Count > 1 Then

Couldn't you invoke a similar check before running the essential code and - if connected - skip opening a new session?

/gustav
0
 
bsncpAuthor Commented:
Would love to see how that code looks because I've tried different variations of that with no luck.  the line that says "In case you are already logged in..." doesn't seem to work.  again, every time this code runs it kills any open current sessions.
0
 
Gustav BrockCIOCommented:
But that is all controlled by:

   Set SapGuiAuto = GetObject("SAPGUI")
   Set Application = SapGuiAuto.GetScriptingEngine
   Set Connection = Application.OpenConnection("PRD")
   Set SapSession = Connection.Children(0)

I guess the comment line should read:

' In case you got logged in ...

Doesn't SAP provide a forum that covers the SAPGUI and VBscript?

I don't run SAP so I have no option for checking or investigating this further.
However, I can locate the parameter "reuse":

http://help.sap.com/saphelp_nwes70/helpdata/EN/cd/38dff9732f441a8026a6af3f9a0c77/content.htm

which indicates that it should be possible to reuse an existing connection.

/gustav
0
 
bsncpAuthor Commented:
Thank you, but I am stalling out because I don't know how to insert that reuse parameter into my code.
0
 
Gustav BrockCIOCommented:
Worse is that that property should be 1 as default.

Did you try to get help at SCN?

http://scn.sap.com/welcome

/gustav
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now