Domino Code Fragment

Code Name*
This script imports information from documents in another database.
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.13.58.216.18
Description*
This script imports information from documents in another database.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
The log db should be created using the 'Agent Log' template. I initially
tried to use the email log methods but never received any messages; the log
db is more convienient anyway. This agent was initially designed to import
the data from all six employee databases in one run, but it exceeded the
agent run time limit, so I broke it into six different agents. If you
schedule a bunch of agents like this for the same time (say 2AM) the server
will run them one or two at a time starting at 2. Note that to run a scheduled agent on a server the designer's name has to
be in the 'Run restricted agents' or 'Run unrestricted agents' entry in the
server's document in the GPU address book. Here you can also see the
restrictions and time limits placed on agents run on each server. On the server, this agent takes 3-4 minutes to import 1500-2000 hardware
records. It will take much longer to run on a workstation.
Files/Graphics attachments (if applicable): Code:
This script imports information from documents in another database. The example imports hardware information from a different
database for each employee document it has. It uses a log db to store debug & error messages. In real life this is used to
import data into the corporate employee database from the company databases, a temporary fix until NotesPump is up and running.


Formula
Sub Initialize
    ' Create hardware records from employee records stored in this db and
    ' hardware records stored in the GPU HW database. This info consists
    ' of PCs only.


     ' One or more HW records will be created for each employee in the db
    ' if records exist


     ' DEBUG True for success messages, False for error messages only
    Const DEBUG = True
    Const LOG_DB = "infosvc\utility\emplog.nsf"


    ' Define an error handler
    On Error Goto ErrHandler


     Dim session As New NotesSession
    Dim db As NotesDatabase ' This database
    Dim hdb As NotesDatabase ' Hardware database
    Dim doc As NotesDocument  ' Employee document
    Dim hDoc As NotesDocument  ' Hardware document
    Dim rDoc As NotesDocument ' Result document
    Dim view As Notesview ' To get employee list from this database
    Dim hCollection As NotesDocumentCollection ' Hardware collection
    Dim formula As String


     ' Create NotesDatabase instance for me
    Set db = session.CurrentDatabase
    Set hDb = session.GetDatabase("Notes Server2", "GPUNC\GPUNPCS.NSF")
    Set view = db.GetView("By Userid")


     ' Create link to log db
    Dim currentLog As New NotesLog( "Hardware record agent for " & db.Title)
    If DEBUG Then Call currentLog.OpenNotesLog( "Notes Server3",  LOG_DB )
    If DEBUG Then currentLog.LogAction "Start Update Hardware Records Agent"


     formula = "Form = 'Inventory' & DEPT_CO_ID = '095' & GPU_USERID != '' & ((SLP_CODE = 'PCLA') | (SLP_CODE = 'PCSA'))"
    Set hCollection = hDb.Search(formula, Nothing, 0)


     ' Step through each HW record, add records for each employee found
    Set hDoc = hCollection.GetFirstDocument()
    Do Until hDoc Is Nothing
         a = hDoc.GPU_USERID
         Set doc = view.GetDocumentByKey(a(0))
         If Not (doc Is Nothing) Then ' Only do hardware for real employees
              Set rDoc = db.CreateDocument()
              rDoc.Form = "Hardware"


               rDoc.TAG_NUMBER = hDoc.TAG_NUMBER
              rDoc.PROD_MFG = hDoc.PROD_MFG
              rDoc.PROD_NUMBER = hDoc.PROD_NUMBER
              rDoc.PROD_NAME = hDoc.PROD_NAME
              rDoc.SERIAL_NUM  = hDoc.SERIAL_NUM
              rDoc.INVENTORY_ID = hDoc.INVENTORY_ID


               rDoc.GPU_USERID = doc.GPU_USERID
              rDoc.ASGN_LOC_STATE = doc.ASGN_LOC_STATE
              rDoc.ASGN_LOC_CITY = doc.ASGN_LOC_CITY
              rDoc.ASGN_LOC_DESC = doc.ASGN_LOC_DESC
              rDoc.COMEC_HB = doc.COMEC_HB
              rDoc.DEPT_NAME = doc.DEPT_NAME
              rDoc.EMP_FIRST_NAME = doc.EMP_FIRST_NAME
              rDoc.EMP_LAST_NAME = doc.EMP_LAST_NAME
              rDoc.EMP_MIDDLE_INIT = doc.EMP_MIDDLE_INIT


               rDoc.Save 1,0
         End If
         Set hDoc = hCollection.GetNextDocument(hDoc)
    Loop


     ' Refresh all views, all views are manual refresh in original design
    If DEBUG Then currentLog.LogAction "Begin refreshing all views"
    Set view = db.GetView("Hardware\By Home Base")
    view.Refresh
    Set view = db.GetView("Hardware\By Location")
    view.Refresh
    Set view = db.GetView("Hardware\By Name")
    view.Refresh
    Set view = db.GetView("Hardware\By Userid")
    view.Refresh
    Set view = db.GetView("(HWLookup)")
    view.Refresh
    Set view = db.GetView("(I.S. Employees)")
    view.Refresh
    Set view = db.GetView("(LastNameChar)")
    view.Refresh


     If DEBUG Then currentLog.LogAction "That's all, folks!"
    Exit Sub


ErrHandler:
' Any errors log output and quit
    If Not DEBUG Then Call currentLog.OpenNotesLog( "Notes Server3", LOG_DB )
    currentLog.LogError 1, "Database: " & db.FileName
    currentLog.LogError 1, "Line No: " & Str$(Erl)
    currentLog.LogError Err, Error$
    Call currentLog.Close
    End


End Sub