Use a ClearSCADA script to read Server.UserName, locate that user in the database, and return one of four configured email addresses. The function returns the selected address as a variant, -1 when the query fails, or -2 when the query returns no user row.
Define the lookup behavior
The script joins CDBUSER to CDBUSERCONTACTCONFIG through their ID fields. It retrieves EMAILADDRESS through EMAILADDRESS4 for the active username. A zero-row result means the active account has no matching contact configuration; the supplied script notes that this may occur for the super user.
emailNum |
Selected field | Result column index |
|---|---|---|
| 1 or any other value | EMAILADDRESS |
2 |
| 2 | EMAILADDRESS2 |
3 |
| 3 | EMAILADDRESS3 |
4 |
| 4 | EMAILADDRESS4 |
5 |
Implement the active-user query
The imported query contains a damaged username token. The implementation below restores it as currentUser, matching the variable assigned from Server.UserName and the stated requirement to search by the current username.
Function ObtainUserEmail(emailNum)
currentUser = Server.UserName
query = "SELECT U.ID, U.NAME, C.EMAILADDRESS, " & _
"C.EMAILADDRESS2, C.EMAILADDRESS3, C.EMAILADDRESS4 " & _
"FROM CDBUSER AS U " & _
"JOIN CDBUSERCONTACTCONFIG AS C ON U.ID = C.ID " & _
"WHERE NAME='" & currentUser & "'"
Set results = Server.Query(query)
If results.Error Then
MsgBox "Whoops, we had an error." & Chr(10) & results.ErrorMessage
ObtainUserEmail = -1
ElseIf results.RowCount = 0 Then
MsgBox "Your user doesn't have an email. You may be the super user"
ObtainUserEmail = -2
Else
Select Case emailNum
Case 1: column = 2
Case 2: column = 3
Case 3: column = 4
Case 4: column = 5
Case Else: column = 2
End Select
queryRows = results.Rows
ObtainUserEmail = queryRows(0, column)
End If
End Function
Handle query and user errors
Check results.Error before accessing rows and expose results.ErrorMessage during diagnosis. Then check results.RowCount; return -2 when it is zero. Only read results.Rows after both checks pass.
Verify each return path
- Run the function as an active user with configured contact data and confirm that selections
1through4return the corresponding email fields. - Pass a value outside
1through4and confirm that the function selects the first email field. - Run with an account that produces no matching row and confirm the
-2return. For a query failure, confirm the message containsresults.ErrorMessageand the function returns-1.
FAQ
How do I get the active ClearSCADA username in a script?
Read Server.UserName and store it in currentUser before building the user query.
How do I retrieve the second ClearSCADA user email address?
Call ObtainUserEmail with emailNum set to 2. The function returns EMAILADDRESS2 from result column index 3.
What do ClearSCADA email lookup return values -1 and -2 mean?
-1 means Server.Query reported an error. -2 means the query succeeded but returned zero rows, which may occur for the super user or an account without matching contact data.