Create and update user profile docs automatically for a database.

This code is used in the database Postopen event to lookup modify and/or create a user profile for any/all users who enter the database. (1) Gathers the current users common name. (2) Looks to see if an profile document exists for this person. (3) If no, then a new doc will be created with all required info. (4) If yes, then proceed with updating of this document with sesion information. (5) Adds 1 to the total number of sessions for this user. (6) Updates the last session date. (7) Extracts the values from the SessionHistory field and repopulates it again adding in this current session while maintaining only 10 values. (8) Saves the profile doc.
LotusScript


Sub Postopen(Source As Notesuidatabase)
	
	Dim workspace As New NotesUIWorkspace
	Dim session As New NotesSession
	Dim thisdb As NotesDatabase
	Dim profileview As NotesView
	Dim profiledoc As NotesDocument
	
	Dim User As String
	User = session.commonusername
	
	Set thisdb = session.CurrentDatabase
	Set profileView = thisdb.GetView("User Profiles")
	Set profileDoc = profileView.GetDocumentByKey(User, True)     
	
	If profiledoc Is Nothing Then
		'__________Create profile doc since none currently exists__________
		Set profileDoc = thisdb.CreateDocument
		profileDoc.form = "User Profile"
		profileDoc.up_username = User
		profileDoc.up_AddedDate = Now
		profileDoc.up_sessions = 1
		profileDoc.up_LastSession = Now
	Else
		'__________Profile exists, update session tracking info____________
		'<<<<<<<<<<<<Counter for Sessions, check if field exists<<<<<<<<<<<<<<<
		Dim itemcheck As NotesItem
		Set itemcheck = profileDoc.GetFirstItem( "up_sessions" )
		If itemcheck Is Nothing Then
			profileDoc.up_sessions = 1
		Else	
			profileDoc.up_sessions = profileDoc.up_sessions(0) + 1
		End If
		'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		' <<<<<<<<<<<<<<<<<<<<<<<<SESSION HISTORY<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
		Dim item As NotesItem
		Set item = profileDoc.GetFirstItem( "up_sessionhistory" )
		If item Is Nothing Then
			profileDoc.up_sessionhistory = ""
			Set item = profileDoc.GetFirstItem( "up_sessionhistory" )
		End If
		If Ubound(profileDoc.up_sessionhistory) >= 9 Then
			'<<<<<<<<<<<<retrieve, clear & repopulate last 10 values only<<<<<<<<<<<<<<<<<
			Dim J As Integer
			Dim X As Integer
	          '________STEP 1 - Gather Info from original field___________
			Dim sourcearray() As String
			Redim sourcearray((Ubound(profileDoc.up_sessionhistory)-8) To Ubound(profileDoc.up_sessionhistory)) As String
			For J = (Ubound(profileDoc.up_sessionhistory)-8) To Ubound(profileDoc.up_sessionhistory)
				sourceArray(J) = profileDoc.up_sessionhistory(J)	
			Next J
               '________STEP 2 - Clear out field______________________
			profileDoc.up_sessionhistory = ""
               '________STEP 3 - Repopulate field with new values_______
			For x = (Ubound(SourceArray)-8) To Ubound(SourceArray)
				If profileDoc.up_sessionhistory(0) = "" Then
					profileDoc.up_sessionhistory = SourceArray(x)	
				Else
					Set item = profileDoc.GetFirstItem( "up_sessionhistory" )
					Call item.AppendToTextList(Cstr(SourceArray(x)))
				End If	
			Next x
               '________STEP 4 - Append Last value for this session "Now"_______
			Call item.AppendToTextList(Cstr(Now))
			'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		Else
			'_______There are less than 9 values, just append__________
			Call item.AppendToTextList(Cstr(Now))
		End If
		'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	End If
	
	'_____________Update Last Session field and save__________________
	profileDoc.up_LastSession = Cstr(Now)
	Call profileDoc.Save( True, True )
	
End Sub

Posted by fbrefere001 on Tuesday February 5, 2002