Solved

Recursive LS for readers field

Posted on 2006-11-20
19
370 Views
Last Modified: 2013-12-18
I have this Lotus Script:
Sub Postopen(Source As Notesuidocument)
      Const ELUREPID = "8525712D005C8115"
      'Const ELUREPID = "85256A6C004C8E88"
      
      Dim session As New notessession
      Dim db As New notesdatabase("","")
      Dim thisdb As notesdatabase
      Dim doc As notesdocument
      Dim view As notesview
      Dim entry As notesviewentry
      
      Dim uname As String
      Dim sdept As String      
      Dim smanager As String
      Dim sdb As String
      Dim sserver As String
      Dim s1 As String
      Dim s2 As String
      Dim stat As String
      Dim sroles As String
      
      Set thisdoc = source.Document
      uname = session.username
      Set thisdb = session.currentdatabase
      sserver = thisdb.server
      
      
      If (source.isnewdoc) Then
            
            If db.OpenByReplicaID(  sserver, ELUREPID ) Then
                  Set view = db.getview("Lookup")
                  Set entry = view.getentrybykey(uname)
                  If (entry Is Nothing) Then
                        Set view = db.getview("Lookup2")
                        uname = session.commonUserName
                        Set entry = view.getentrybykey(uname)
                  End If
                  If Not(entry Is Nothing) Then
                        Set doc = entry.document
                        smanager = doc.Supervisor(0)
                        If Instr(smanager, "(") > 0 Then
                              s1 = Left$(smanager, Instr(smanager,",")-1)
                              s2 = Trim$(Mid$(smanager,Len(s1)+2))
                              smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
                        Else
                              s1 = Left$(smanager, Instr(smanager,",")-1)
                              's2 = Right$(smanager,Instr(smanager,",")-2)
                              s2 = Trim$(Mid$(smanager,Len(s1)+2))
                              smanager = s2+" "+s1
                        End If
                        
                        Call source.fieldsettext("NET60_Manager",smanager)
                  End If
            End If
            Call source.collapseAllSections
            Call source.refresh
            Call source.gotofield("Net60_TypeReq")
      End If
      
      If source.isnewdoc Then
            Call source.collapseAllSections
      Else
            Call source.expandAllSections
      End If
      
End Sub

I need this script to continue collecting names until it can't go no higher, so it should for example:

Pick my manager, then go back and pick my managers manager, then go back and pick my managers, manager manager etc etc etc until it can't get any higher. I need all those names put into the readers filed of the form.....I'm having trouble making the code recursive......
0
Comment
Question by:padillrr
19 Comments
 
LVL 63

Expert Comment

by:SysExpert
ID: 17984645
1) Have you looked at any sample recursive code ?

2) If you know the maximum limit , then it does not have to be recursive.

3) You need to make just the subroutine that finds the next level recursive ( if needed ) and called as a subroutine .

I hope this helps !


0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 17984998
You may want to create a recursive function that will abort if the function returns nothing

So it will have code like this in it Grossly oversimplified.. let me see if I can whip it up

If GetManager(User) = "" Then
  Exit Function
Else
  ....
  ....
End If
0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 17985146
This part of the code can be recursive you would want to pass in the uname as smanagers and mgrName

Function AddEntryManagerString( sManagers As String, mgrName As String, db As NotesDatabase, view As NotesView ) As String
      Dim sManager As String
      Dim entry As NotesViewEntry
      Dim doc As NotesDocument
      Dim s1 As String
      Dim s2 As String
      Dim sReturn As String
      Dim sSep As String
      If sManagers <> "" Then strSep = "; "
      Set entry = view.getentrybykey(mgrName)
      If Not(entry Is Nothing) Then
            Set doc = entry.document
            smanager = doc.Supervisor(0)
            If Instr(smanager, "(") > 0 Then
                  s1 = Left$(smanager, Instr(smanager,",")-1)
                  s2 = Trim$(Mid$(smanager,Len(s1)+2))
                  smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
            Else
                  s1 = Left$(smanager, Instr(smanager,",")-1)
                  s2 = Trim$(Mid$(smanager,Len(s1)+2))
                  smanager = s2+" "+s1
            End If
            If smanager <> "" Then sReturn = AddEntryManagerToList( sManagers, smanager, db, view)
            If sReturn <> "" Then
                  AddEntryManagerToList = sManagers & sSep & sReturn
            Else
                  AddEntryManagerToList = sManagers
            End If
      End If
End Function
0
 
LVL 8

Expert Comment

by:behenderson
ID: 17986519
A few small details...  I am sure that padillrr would have caught it or andy could have updated it but the variable AddEntryManagerToList should be AddEntryManagerString Also have you considered using a while loop instead of recusrsion?  That is not the question that was answered and I don't want to take anything away from the example but unfortunately recursion is actually not very efficient because it causes multiple copies of the function to be held in memory until the last copy of the function closes the loop.  You may want to do your own research on the internet and find out what other people are saying about recursion... But I think a while loop might be better.  Recursion is kind of cool, and that was the question that was asked, I am not trying to be critical I just thought I would bring up the question of the efficiency of recursion as a side discussion.
0
 

Author Comment

by:padillrr
ID: 17986796
As long as I can get all the data I'm looking for into one field (ManagerList)  on the form, if a loop works and is more efficient can you post some code I can look at. This is for a QuerySave Event on a form, it will populate a readers field. I need it to run everytime the form is saved, this will insure that if a manager changes along the process it will get that name and repopulate the field. I'm prepared to max this out on points...If I can get the code to work before the end of the day today......
0
 

Author Comment

by:padillrr
ID: 17986871
This is what I have but I am getting an error see *** for line where code is erroring out:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
      Dim AddEntryManagerString  As String
      Dim sManager As String
      Dim entry As NotesViewEntry
      Dim doc As NotesDocument
      Dim s1 As String
      Dim s2 As String
      Dim sReturn As String
      Dim sSep As String
      
      Dim uname As String
      Dim sdept As String      
      Dim sdb As String
      Dim sserver As String
      Dim stat As String
      Dim sroles As String
      
      Set thisdoc = source.Document
      uname = session.username
      Set thisdb = session.currentdatabase
      sserver = thisdb.server
      
      If sManagers <> "" Then strSep = "; "
      Set entry = view.getentrybykey(mgrName)
      If Not(entry Is Nothing) Then
            Set doc = entry.document
            smanager = doc.Supervisor(0)
            If Instr(smanager, "(") > 0 Then
                  s1 = Left$(smanager, Instr(smanager,",")-1)
                  s2 = Trim$(Mid$(smanager,Len(s1)+2))
                  smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
            Else
                  s1 = Left$(smanager, Instr(smanager,",")-1)
                  s2 = Trim$(Mid$(smanager,Len(s1)+2))
                  smanager = s2+" "+s1
            End If
***      If smanager <> "" Then sReturn = AddEntryManagerString( sManagers, smanager, db, view)
            If sReturn <> "" Then
                  AddEntryManagerToList = sManagers & sSep & sReturn
            Else
                  AddEntryManagerToList = sManagers
            End If
      End If
      
End Sub
0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 17987349
I don't have the same set up so I cannot really test this so there may be some back and forth before it works like a charm ;-)  A while loop may be more efficient if someone else posts one go ahead and use that, but I think this will work.  Recursion is necessary in languages like lisp but not script.

behenderson thanks for catching the typo.

Function AddEntryManagerString( sManagers As String, mgrName As String, db As NotesDatabase, view As NotesView ) As String
      Dim sManager As String
      Dim entry As NotesViewEntry
      Dim doc As NotesDocument
      Dim s1 As String
      Dim s2 As String
      Dim sReturn As String
      Dim sSep As String
     If sManagers <> "" Then strSep = "; "
     Set entry = view.getentrybykey(mgrName)
     If Not(entry Is Nothing) Then
          Set doc = entry.document
          smanager = doc.Supervisor(0)
          If Instr(smanager, "(") > 0 Then
               s1 = Left$(smanager, Instr(smanager,",")-1)
               s2 = Trim$(Mid$(smanager,Len(s1)+2))
               smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
          Else
               s1 = Left$(smanager, Instr(smanager,",")-1)
               s2 = Trim$(Mid$(smanager,Len(s1)+2))
               smanager = s2+" "+s1
          End If
          If smanager <> "" Then sReturn = AddEntryManagerString( sManagers, smanager, db, view)
          If sReturn <> "" Then
               AddEntryManagerString = sManagers & sSep & sReturn
          Else
               AddEntryManagerString = sManagers
          End If
     End If
End Function

Sub Postopen(Source As Notesuidocument)
     Const ELUREPID = "8525712D005C8115"
     'Const ELUREPID = "85256A6C004C8E88"
     
     Dim session As New notessession
     Dim db As New notesdatabase("","")
     Dim thisdb As notesdatabase
     Dim doc As notesdocument
     Dim view As notesview
     Dim entry As notesviewentry
     
     Dim uname As String
     Dim sdept As String    
     Dim smanager As String
     Dim sdb As String
     Dim sserver As String
     Dim s1 As String
     Dim s2 As String
     Dim stat As String
     Dim sroles As String
     
     Set thisdoc = source.Document
     uname = session.username
     Set thisdb = session.currentdatabase
     sserver = thisdb.server
     If (source.isnewdoc) Then
         
          If db.OpenByReplicaID(  sserver, ELUREPID ) Then
               Set view = db.getview("Lookup")
               Set entry = view.getentrybykey(uname)
               If (entry Is Nothing) Then
                    Set view = db.getview("Lookup2")
                    uname = session.commonUserName
                    Set entry = view.getentrybykey(uname)
               End If
                    smanager = AddEntryManagerString( sManagers, smanager, db, view)
                    Call source.fieldsettext("NET60_Manager",smanager)
               End If
          End If
          Call source.collapseAllSections
          Call source.refresh
          Call source.gotofield("Net60_TypeReq")
     End If
     
     If source.isnewdoc Then
          Call source.collapseAllSections
     Else
          Call source.expandAllSections
     End If
     
End Sub
0
 

Author Comment

by:padillrr
ID: 17987445
Copied and pasted that on my Querysave and had a few errors:  Function errored out...Subpost open errored out as well....
0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 17987945
Sorry more typos.. without a chance to debug it is harder than I thought to do.

Function AddEntryManagerString( sManagers As String, mgrName As String, db As NotesDatabase, view As NotesView ) As String
      Dim sManager As String
      Dim entry As NotesViewEntry
      Dim doc As NotesDocument
      Dim s1 As String
      Dim s2 As String
      Dim sReturn As String
      Dim sSep As String
     If sManagers <> "" Then sSep = "; "
     Set entry = view.getentrybykey(mgrName)
     If Not(entry Is Nothing) Then
          Set doc = entry.document
          smanager = doc.Supervisor(0)
          If Instr(smanager, "(") > 0 Then
               s1 = Left$(smanager, Instr(smanager,",")-1)
               s2 = Trim$(Mid$(smanager,Len(s1)+2))
               smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
          Else
               s1 = Left$(smanager, Instr(smanager,",")-1)
               s2 = Trim$(Mid$(smanager,Len(s1)+2))
               smanager = s2+" "+s1
          End If
          If smanager <> "" Then sReturn = AddEntryManagerToList( sManagers, smanager, db, view)
          If sReturn <> "" Then
               AddEntryManagerToList = sManagers & sSep & sReturn
          Else
               AddEntryManagerToList = sManagers
          End If
     End If
End Function

Sub Postopen(Source As Notesuidocument)
     Const ELUREPID = "8525712D005C8115"
     'Const ELUREPID = "85256A6C004C8E88"
     
     Dim session As New notessession
     Dim db As New notesdatabase("","")
     Dim thisdb As notesdatabase
     Dim doc As notesdocument
     Dim view As notesview
     Dim entry As notesviewentry
     
     Dim uname As String
     Dim sdept As String    
     Dim smanager As String
     Dim sdb As String
     Dim sserver As String
     Dim s1 As String
     Dim s2 As String
     Dim stat As String
     Dim sroles As String
     
     Set thisdoc = source.Document
     uname = session.username
     Set thisdb = session.currentdatabase
     sserver = thisdb.server
     If (source.isnewdoc) Then
         
          If db.OpenByReplicaID(  sserver, ELUREPID ) Then
               Set view = db.getview("Lookup")
               Set entry = view.getentrybykey(uname)
               If (entry Is Nothing) Then
                    Set view = db.getview("Lookup2")
                    uname = session.commonUserName
                    Set entry = view.getentrybykey(uname)
               End If
                    smanager = AddEntryManagerString( uname, uname, db, view)
                    Call source.fieldsettext("NET60_Manager",smanager)
               End If
          End If
          Call source.collapseAllSections
          Call source.refresh
          Call source.gotofield("Net60_TypeReq")
     End If
     
     If source.isnewdoc Then
          Call source.collapseAllSections
     Else
          Call source.expandAllSections
     End If
     
End Sub

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:padillrr
ID: 17988442
still having errors....
changed addentrymanagerto list to addentry manager to string
If smanager <> "" Then sReturn = AddEntryManagerToList( sManagers, smanager, db, view)
          If sReturn <> "" Then
Removed the following Line and put into querysave option:
Sub Postopen(Source As Notesuidocument)


It compiled but not data was retrieved
0
 
LVL 1

Accepted Solution

by:
AndySheepleton earned 500 total points
ID: 17989299
Sub Click(Source As Button)
      Const ELUREPID = "8525712D005C8115"
     'Const ELUREPID = "85256A6C004C8E88"
      Dim session As New notessession
      Dim db As New notesdatabase("","")
      Dim thisdb As notesdatabase
      Dim doc As notesdocument
      Dim view As notesview
      Dim entry As notesviewentry
      
      Dim uname As String
      Dim sdept As String    
      Dim smanager As String
      Dim sdb As String
      Dim sserver As String
      Dim s1 As String
      Dim s2 As String
      Dim stat As String
      Dim sroles As String
      Set thisdoc = source.Document
      uname = session.username
      Set thisdb = session.currentdatabase
      sserver = thisdb.server
      If (Source.isnewdoc) Then
            If db.OpenByReplicaID(  sserver, ELUREPID ) Then
                  Set view = db.getview("Lookup")
                  Set entry = view.getentrybykey(uname)
                  If (entry Is Nothing) Then
                        Set view = db.getview("Lookup2")
                        uname = session.commonUserName
                        Set entry = view.getentrybykey(uname)
                  End If
                  smanager = AddEntryManagerString( "", uname, db, view)
                  Call Source.fieldsettext("NET60_Manager",smanager)
            End If
      End If
      Call Source.collapseAllSections
      Call Source.refresh
      Call Source.gotofield("Net60_TypeReq")
      
      If Source.isnewdoc Then
            Call Source.collapseAllSections
      Else
            Call Source.expandAllSections
      End If
End Sub

Function AddEntryManagerString( sManagers As String, mgrName As String, db As NotesDatabase, view As NotesView ) As String
      Dim sManager As String
      Dim entry As NotesViewEntry
      Dim doc As NotesDocument
      Dim s1 As String
      Dim s2 As String
      Dim sReturn As String
      Dim sSep As String
      If sManagers <> "" Then sSep = "; "
      Set entry = view.getentrybykey(mgrName)
      If Not(entry Is Nothing) Then
            Set doc = entry.document
            smanager = doc.Supervisor(0)
            If Instr(smanager, "(") > 0 Then
                  s1 = Left$(smanager, Instr(smanager,"(")-1)
                  s2 = Trim$(Mid$(smanager,Len(s1)+2))
                  smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
            Else
                  If Instr(smanager, ",") > 0 Then
                        s1 = Left$(smanager, Instr(smanager,",")-1)
                        s2 = Trim$(Mid$(smanager,Len(s1)+2))
                        smanager = s2+" "+s1
                  End If
            End If
            If smanager <> "" Then
                  sReturn = AddEntryManagerString( sManagers, sManager, db, view)
            End If
            If sReturn <> "" Then
                  AddEntryManagerString = sManagers & sSep & mgrName & sSep & sReturn
            Else
                  AddEntryManagerString = sManagers & sSep & mgrName
            End If
      End If
End Function
0
 

Author Comment

by:padillrr
ID: 18020607
OK guys and gals this has become a serious issue, I am using the code sent by Andy but it's working right now...check out the points I really need this no later then Thursday morning...
0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 18022253
Padilrr.. What happens if you debug this code?  What do the Usernames look like in the view that you are referencing in doc.Supervisor?  If you debug and you look at the variable smanager after the line smanager = doc.Supervisor(0) what does it look like.. Is it the same as what is in the view.  You may need to add a declaration and a line of code.

Dim nNm as NotesName

and after smanager = doc.Supervisor(0) add a few lines

If smanager <> "" Then
nNm = New NotesName( smanager )
smanager = nNm.Abbreviated    OR smanager = nNm.Common    depending on what is in the view and what is in the supervisor field.
end if

You have to put a little effort into this.  The Code Does Not work is really not specific enough.

That code does work if there is an entry in the doc.Supervisor(0) field that matches the value in the view

Maybe you need to add a second view to the function call  
Function AddEntryManagerString( sManagers As String, mgrName As String, db As NotesDatabase, view As NotesView, view2 as NotesView ) As String

and a line after

Set entry = view.getentrybykey(mgrName)
If entry is nothing then set entry = view2.getentrybykey(mgrName)

But you need to debug the code and figure out where it is failing...
0
 

Author Comment

by:padillrr
ID: 18022322
did that throughout the weekend....and could not pick up a single name the code runs with no errors, just no data is populated
0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 18023086
Padillrr,

1 When you debug the code what ends up being the value in the smanager variable the first time the function is called??
0
 

Author Comment

by:padillrr
ID: 18028645
Now I am getting my name populating the ManagerList field, it should be my manager and the managers above that...
0
 

Author Comment

by:padillrr
ID: 18029096
This is the Code I am using my reults are a little better now I am picking up my name (should not be in there) my boss and her bos then it stops.....it should continue till there are no more bosses....A few additional things, the sript should always run and repopulate the MAnagerList Field everytime it's saved (just in case a manager changes) there are no spaces between the names and it is adding my name to the field it should not.....

QUERY SAVE:
Sub Querysave(Source As Notesuidocument, Continue As Variant)
      Const ELUREPID = "8525712D005C8115"
     'Const ELUREPID = "85256A6C004C8E88"
      Dim session As New notessession
      Dim db As New notesdatabase("","")
      Dim thisdb As notesdatabase
      Dim doc As notesdocument
      Dim view As notesview
      Dim entry As notesviewentry
      
      Dim uname As String
      Dim sdept As String    
      Dim smanager As String
      Dim sdb As String
      Dim sserver As String
      Dim s1 As String
      Dim s2 As String
      Dim stat As String
      Dim sroles As String
      Set thisdoc = source.Document
      uname = session.username
      Set thisdb = session.currentdatabase
      sserver = thisdb.server
      If (Source.isnewdoc) Then
            If db.OpenByReplicaID(  sserver, ELUREPID ) Then
                  Set view = db.getview("Lookup")
                  Set entry = view.getentrybykey(uname)
                  If (entry Is Nothing) Then
                        Set view = db.getview("Lookup2")
                        uname = session.commonUserName
                        Set entry = view.getentrybykey(uname)
                  End If
                  smanager = AddEntryManagerString( "", uname, db, view)
                  Call Source.fieldsettext("ManagerList",smanager)
            End If
      End If
      Call Source.collapseAllSections
      Call Source.refresh
      Call Source.gotofield("MBOYrs")
      
      If Source.isnewdoc Then
            Call Source.collapseAllSections
      Else
            Call Source.expandAllSections
      End If
End Sub

FUNCTION:

Function AddEntryManagerString( sManagers As String, mgrName As String, db As NotesDatabase, view As NotesView ) As String
      Dim sManager As String
      Dim entry As NotesViewEntry
      Dim doc As NotesDocument
      Dim s1 As String
      Dim s2 As String
      Dim sReturn As String
      Dim sSep As String
      If sManagers <> "" Then sSep = "; "
      Set entry = view.getentrybykey(mgrName)
      If Not(entry Is Nothing) Then
            Set doc = entry.document
            smanager = doc.Supervisor(0)
            If Instr(smanager, "(") > 0 Then
                  s1 = Left$(smanager, Instr(smanager,"(")-1)
                  s2 = Trim$(Mid$(smanager,Len(s1)+2))
                  smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
            Else
                  If Instr(smanager, ",") > 0 Then
                        s1 = Left$(smanager, Instr(smanager,",")-1)
                        s2 = Trim$(Mid$(smanager,Len(s1)+2))
                        smanager = s2+" "+s1
                  End If
            End If
            If smanager <> "" Then
                  sReturn = AddEntryManagerString( sManagers, sManager, db, view)
            End If
            If sReturn <> "" Then
                  AddEntryManagerString = sManagers & sSep & mgrName & sSep & sReturn
            Else
                  AddEntryManagerString = sManagers & sSep & mgrName
            End If
      End If
End Function
0
 
LVL 1

Expert Comment

by:AndySheepleton
ID: 18034356
Add This:
     If Not(entry Is Nothing) Then
          Set doc = entry.document
          smanager = doc.Supervisor(0)
          If Instr(smanager, "(") > 0 Then
               s1 = Left$(smanager, Instr(smanager,"(")-1)
               s2 = Trim$(Mid$(smanager,Len(s1)+2))
               smanager = Trim$(Left$(s2,Instr(s2,"(")-1)) +" "+Trim$(s1)
          Else
               If Instr(smanager, ",") > 0 Then
                    s1 = Left$(smanager, Instr(smanager,",")-1)
                    s2 = Trim$(Mid$(smanager,Len(s1)+2))
                    smanager = s2+" "+s1
               End If
          End If
          If smanager <> "" Then

In the querysave before
smanager = AddEntryManagerString( "", uname, db, view)

To get rid of your name being returned

And you really have to look at your boss's boss's name to figure out why it is not being found in the view.  You may need to add a Trim$(  )   around the variable name in the view.. And you have to make sure that you don't have multiple entries in a single entry field (or multiple entries at all really)  Or a period or a dash or a spelling issue or any one of a number of things that could cause it to be different.  You really need to watch it closely in the debugger.
0
 

Author Comment

by:padillrr
ID: 18068460
Andy I will grant you the points, your solution is viable but I went with a recursive script instead. I've included the Code for those that may need it......

Script Library:  
Sub RecursivelyGetManagerList(EmployeeNotesName As String, EmployeeCommonName As String, ManagerList As Variant)
      
%REM
      This function accepts an employee name and recursively retrieve a list of the managers above that employee.
      It accepts three inputs:
            EmployeeNotesName
                  This is the name of the employee in standard Lotus Notes format.  
                  For example:  John Doe/US/CompanyName                  This is the preferred mechanism for defining an employee.

            EmployeeCommonName
                  The function is initially called using this name.  When there is a manager, the function will call itself
                  using the managers name, until there is no longer a manager found.

            ManagerList
                  The manager list is a dynamically sized array.  It should be empty on the initial call.  The list will be passed
                  up and down recursive call stack.  When the last return occurs, the list will contain all of the manager names.
                  The ManagerList should be passed in as an empty variant, initially.

            You must submit either an EmployeeNotesName or an EmployeeCommonName.  
            If both are submitted, the EmployeeNotesName will be used first in attempts to locate the employee record.
            If the EmployeeCommonName is submitted and contains a comma, the function will assume that it was submitted
            in a Last Name, First Name format.

      Sample Calls:

            Dim ManagerList as Variant
            Call RecursivelyGetManagerList("John Doe/US/CompanyName", "", ManagerList)
                  OR
            Call RecursivelyGetManagerList("", "John Doe", ManagerList)
                  OR
            Call RecursivelyGetManagerList("", "Doe, Joe", ManagerList)

      
%END REM
      
'      Print "Retrieving mangager for " & EmployeeNotesName & "/" & EmployeeCommonName
      
      'Define the lookup database that will be used to retrieve the manager names.
      Dim session As New notessession
      Dim db As notesdatabase
      Dim view As notesview
      Dim entry As notesviewentry
      Dim server As String
      Dim item As NotesItem
      
'Rep ID of the DB used for the Lookups      
Const ELUREPID = "your DB repID"
          Dim LUdb As New NotesDatabase("","")
      Dim LUByCommonName As NotesView
      Dim LUByNotesName As NotesView
      Dim docEmployeeCol As NotesDocumentCollection
      Dim docEmployee As notesdocument
      Dim CommaPosition As Integer
      Dim NameFirst As String
      Dim NameLast As String
      Dim Manager As String
      Dim docManagerCol As NotesDocumentCollection
      Dim docManager As NotesDocument
      Dim FoundManagerNotesName As Boolean
      
      
      Set db = session.CurrentDatabase
      server = db.Server
      
      'If the EmployeeCommonName contains a comma, attempt to reverse the format.
      EmployeeCommonName = StandardizeName(EmployeeCommonName)
      
      
      'Try to open the Lookup Database, which is required to proceed.
      If LUdb.OpenByReplicaID(server,ELUREPID) Then
            'The database was found, so we can proceed.
            Set LUByNotesName = LUdb.GetView("VIewName")
            Set LUByCommonName= LUdb.getview("ViewName")
            
            'Try to find the employee document using the Notes Name first.            
            If EmployeeNotesName <> "" Then
                  Set docEmployeeCol = LUByNotesName.GetAllDocumentsByKey(EmployeeNotesName)
            End If
            'If the Notes Name wasn't useful, try to find the employee document using the Employee Name
            If docEmployeeCol Is Nothing Then
                  Set docEmployeeCol = LUByCommonName.GetAllDocumentsByKey(EmployeeCommonName,False)
            End If
            'If the document still wasn't found, try to use the Common Name in the Notes Lookup view
            If docEmployeeCol.Count = 0 Then
                  Set docEmployeeCol = LUByNotesName.GetAllDocumentsByKey(EmployeeCommonName,False)
            End If
            
            
            
            'If the employee document was found, then we can proceed.  If not, use the second lookup view.
            If (docEmployeeCol.Count > 0) Then
                  Set docEmployee = docEmployeeCol.GetFirstDocument
                  'This section should be retrieving a Notes name, not these names.  
                  'It should be rewritten when the database is repopulated.
                  
                  'Determin who the manager is.  
                  'Start by looking at the "DirectReport" field.  If available, use it.
                  'If it is not available, look at the "Supervisor" field.
                  'If neither field is populated, we have no manager.
                  Manager = ""
                  Set item = docEmployee.GetFirstItem("directreport")
                  If Not(item Is Nothing) Then                  
                        Manager = StandardizeName(Trim$(item.Text))
                  End If
                  If Manager = "" Then
                        Set item = docEmployee.GetFirstItem("Supervisor")
                        Manager = StandardizeName(Trim$(item.Text))
                  End If
                  
                  Manager = Trim$(Manager)
                  
                  'If there is a manager, then try to locate the manager's document to get the best name.
                  If Manager <> "" Then
                        
                        'Find the manager's document using their common name
                        Set docManagerCol = LUByCommonName.GetAllDocumentsByKey(Manager)
                        'If the Common Name didn't work, try using the manager name to look them up as a notes name.
                        If docManagerCol.Count = 0 Then
                              Set docManagerCol = LUByNotesName.GetAllDocumentsByKey(Manager,False)
                        End If
                        
                        If docManagerCol.Count >0 Then
                        'Get the best name for the manager from the document
                              Set docManager = docManagerCol.GetFirstDocument
                              
                              Set item = docManager.GetFirstItem("AbbUserName")
                              If item.Text <> "" Then
                                    'Use the Notes name if it is available.
                                    Manager = item.text
                                    FoundManagerNotesName = True
                                    
                              Else
                                    'Use the common name if it is not available.
                                    FoundManagerNotesName = False
                              End If
                        End If
                  End If
                  
                  'If there is a manager, Add them to the list      
                  If Manager <> "" Then
                        
                        'Add the Manager to the list.
'                        Print "Adding Manager to List: " & Manager
                        If Isempty(ManagerList) Then
                                    'If the ManagerList variable has not been initialized, initialize it.
                              Redim ManagerList(0) As String
                        Else
                                    'If it is not empty, then add another value to the array.
                              Redim Preserve ManagerList(Ubound(ManagerList) + 1) As String
                        End If
                        ManagerList(Ubound(ManagerList)) = Manager
                        
                        'If this person has a manager, call this function recursively
                        If FoundManagerNotesName Then
                              Call RecursivelyGetManagerList(Manager,"",ManagerList)
                        Else
                              Call RecursivelyGetManagerList("",Manager,ManagerList)
                        End If
                  End If
                  
            End If
            
'            smanager = AddEntryManagerString( "", uname, db, view)
            
      Else
            'The database was not available, so we need to end and give an error message.
            Messagebox "Unable to open the Employee Lookup Database",16, "Error"
            End            
      End If
      
      
End Sub

Function StandardizeName(EmployeeCommonName) As String
      
      'If the EmployeeCommonName contains a comma, attempt to reverse the format.
      CommaPosition = Instr ( EmployeeCommonName , "," )
      If CommaPosition > 0 Then
            NameLast = Trim(Left(EmployeeCommonName, (CommaPosition -1)))
            NameFirst = Trim(Right(EmployeeCommonName, Len(EmployeeCommonName) - (CommaPosition)))
            StandardizeName = NameFirst & " " & NameLast
      Else
            StandardizeName = EmployeeCommonName
      End If
      
End Function

Query Save:

Sub Querysave(Source As Notesuidocument, Continue As Variant)
      
      Dim ui As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim doc As NotesDocument
      Dim item As NotesItem
      Dim NameCommon As String
      Dim NameNotes As String
      Dim ManagerList As Variant
      
      Set uidoc = ui.CurrentDocument
      Set doc = uidoc.Document
'      NameNotes = uidoc.FieldGetText("NameNotes")
      NameCommon = uidoc.FieldGetText("Disp_Net60_Requestor")
      
      'Get the list of managers
      Call RecursivelyGetManagerList("", NameCommon, ManagerList)
      
      'Put the list of managers into the current document.
      Set item = doc.GetFirstItem("ManagerList")
      item.Values = ManagerList
      
      'Show the updated values in the UI.
'      uidoc.Reload
      'Stop
      
      
End Sub
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now