I have the following script to create the central user account with FirstInitial and Last Name. If firstNAme and LastName already exist add middle initial to the central user account. Please advise:
Now, I would like to add the PreferredName if it exists. Here is the logic:
Create Central User Account with FirstInitial and LastName BUT, If the PreferredName exists create the Central User Account with the first initial of the PrefrredName and LastName.
Here is my current script:
#If Not SCRIPTDEBUGGER Then
Imports System.Collections.Generic
Imports System.Data
#End If
Public Function AFO_BuildCentralAccountGlobalUnique(ByVal uid_person As String, ByVal Lastname As String, ByVal Firstname As String, ByVal MiddleName As String) As String
Dim account As String
Dim accountPrefix As String
Dim f As ISqlFormatter = Connection.SqlFormatter
AFO_BuildCentralAccountGlobalUnique = String.Empty
If Firstname.Length > 0 And Lastname.Length > 0 Then
account = Firstname.Substring(0, 1) & Lastname
ElseIf Firstname.Length > 0 Then
account = Firstname.Substring(0, 1)
Else
account = Lastname
End If
account = VI_AE_FormatConvertUmlaut_Sonderzeichen(account)
' we cut it after 12 characters
account = Left(account, 12)
accountPrefix = account
'fill existing addresses in a dictionary
Dim existing As New Dictionary(Of String, Object)(StringComparer.OrdinalIgnoreCase)
Dim dummy As New Object()
Dim dummyPerson As ISingleDbObject
dummyPerson = Connection.CreateSingle("Person")
Dim pattern As String = accountPrefix & "%"
Dim myObjectKey As New DbObjectKey("Person", uid_person)
Using rd As IDataReader = CType(dummyPerson.Custom.CallMethod("SearchCentralAccount", pattern), IDataReader)
While rd.Read()
Dim accountName As String
Dim objectKeyString As String
Dim objectKey As DbObjectKey
accountName = rd.GetString(rd.GetOrdinal("AccountName"))
objectKeyString = rd.GetString(rd.GetOrdinal("ObjectKeyPerson"))
If Not String.IsNullOrEmpty(objectKeyString) Then
objectKey = New DbObjectKey(objectKeyString)
'only addresses which not belong to the actual employee will be considered
If myObjectKey.Equals(objectKey) Then
Continue While
End If
End If
existing(accountName) = dummy
End While
End Using
Dim UseMiddle As Boolean = False
While True
Dim centralAccount As String = account
' Does not exists?
If Not existing.ContainsKey(centralAccount) Then
Return centralAccount.ToLowerInvariant()
End If
' next trial
If Not UseMiddle Then
If MiddleName.Length > 0 Then
account = Firstname.Substring(0, 1) & MiddleName.Substring(0, 1) & LastName
UseMiddle = True
Else
Throw New ViException("Duplicate Name Alert. No middlename available.")
End If
Else
Throw New ViException("Duplicate Name Alert. Middlename already in use by another Identity.")
End If
End While
End Function