I am working on trying some code that access a particular view multiple profile documents; I then want the code to loop until it finds a specific value within a field called (admintype).
Basically, I have profile documents with a specific site administrator. When that site administrator creates a new user account, I want the Postopen code to update some generic fields (address, company, etc.). A sort of a preload action.
Here is my code.
Dim ns As New NotesSession
Dim view As NotesView
Dim sdoc As NotesDocument
Dim User as NotesName
Dim db As NotesDatabase
Dim user As String
'Get the correct site profile document.
user = ns.Username
Set db = session.CurrentDatabase
Set view = db.GetView( "Site Profile" )
Set sdoc = view.GetFirstDocument
While Not (sdoc Is Nothing)
sdoc.AdminType = sdoc.getitemvalue ("AdminType")
If user = sdoc.admintype Then
Call Updatefields
Else
Set sdoc = view.getnextdocument(sdoc)
Endif
Wend
I do not think this is the right coding as I am not a strong lotuscript person. Any help is greatly appreciated.
Subject: While Not and searching for an item in a document
first… when you are finished with your while loop, if your user is equal to sdoc.admintype, you are not moving on to the next document (unless this is done in the updatefields subroutine.
Change this structure as follows:
Set sdoc = view.GetFirstDocument
While Not (sdoc Is Nothing)
sdoc.AdminType = sdoc.getitemvalue (“AdminType”)
If user = sdoc.admintype Then
Call Updatefields 'if this contains a getnextdocument call, remove it. also, you should psas references that are being used to this document if they are needed. For instance updatefields(sdoc) would pass a handle to the profile document. Look at functions in the help for more on the syntax
Endif
Set sdoc = view.getnextdocument(sdoc)
Wend
Next. Turn on the LS debugger and check your data types. user is a String, but sdoc.admintype will not be a string. You want to get sdoc.admintype(0) if there is only one entry in the admintype field. If this is a multi-value field, then you want to loop through all of the values and compare each one to the value of user:
while not (sdoc is nothing)
forall g in sdoc.admintype
if user = g then
call update fields 'This function would probably need to change to handle the multiple values… I don’t know what you’re doing with it, so no guesses there
Subject: RE: While Not and searching for an item in a document
Joel, first thank you for your response. I have modified my code and I thought it was fine. The script runs, but it does not preload the requessted fields in my code. I use the ‘Call Source.FieldSetText( “SiteName”, sdoc.SiteName(0) )’ lines to place the value of the sdoc into the user registration field (“sitename”). here is the code:
Sub Postopen(Source As Notesuidocument)
Dim ns As New NotesSession
Dim view As NotesView
Dim sdoc As NotesDocument
Dim db As NotesDatabase
Dim user As String
' Get the correct site profile document.
Set db = ns.CurrentDatabase
Set view = db.GetView( "SiteProfiles" )
Set sdoc = view.GetFirstDocument ()
user = ns.Username
While Not (sdoc Is Nothing)
If user = sdoc.admintype(0) Then
' Set the default fields.
If Source.EditMode And Source.IsNewDoc Then
' Import standard defaults.
Call Source.FieldSetText( "SiteName", sdoc.SiteName(0) )
Call Source.FieldSetText( "Administrator", sdoc.AdminName(0) )
Call Source.FieldSetText( "dsp_NameCompany", sdoc.SiteName(0) )
Call Source.FieldSetText( "dsp_telephone", sdoc.Telephone(0) )
Call Source.FieldSetText( "dsp_address_1", sdoc.address_1(0) )
Call Source.FieldSetText( "dsp_address_2", sdoc.address_2(0) )
Call Source.FieldSetText( "dsp_city", sdoc.city(0) )
Call Source.FieldSetText( "dsp_stateprovince", sdoc.stateprovince(0) )
Call Source.FieldSetText( "dsp_zip_postal", sdoc.zip_postal(0) )
Call Source.FieldSetText( "dsp_country", sdoc.country(0) )
Call Source.FieldSetText( "dsp_region", sdoc.region(0) )
Call Source.FieldSetText( "dsp_fax", sdoc.fax(0) )
End If
End If
Set sdoc = view.getnextdocument(sdoc)
Wend
Subject: RE: While Not and searching for an item in a document
What do you see for each of the values (sdoc, as well as the fields that are being used on sdoc) when you loop through the code with the debugger on? Do this to test your logic flow and make sure it is running as you intend and inspect the variables.
Subject: RE: While Not and searching for an item in a document
Hi Joel, I got it to work finally. The code didn’t generate any errors as it was doing exactly what I asked it to do. Only, I was asking it to do was evaluate a value in a field against the current user name. Because of that, it didn’t update the fields as expected.
I guess what I wanted to do is to evaluate the user clicking on the create button against the profile documents created. Yet, i didn’t need to really do that. I could just evaluate the administrator field for that information. When I changed the field to be evaluated, it worked.