How Can I get user details by email property?
I know it is possible get the user using ##class(Security.Users).Get("username"), but I need to get a user by your email. How Can I get a user by email?
Product version: IRIS 2022.3
Discussion (1)1
Comments
Hey Yuri.
The users are held within the SQL table "Security.Users" in the %SYS namespace, so you could use embedded sql to return the information, however as you're unlikely to be executing your code directly from the %SYS namespace, I'd suggest creating a function that you pass the email address, and it returns the username.
Something like:
Class Demo.Utils.General.Users
{
ClassMethod UserFromEmail(Email As%String, Output Username As%String) As%Status
{
//Initially set this to null, as we want to return it empty when we get no resultsSet Username = ""//Hold the Namespace within a variable so we can use the variable to set the namespace back once the SQL has been run.Set CurrNamespace = $NAMESPACE//Change NameSpace to %SYSSet$NAMESPACE = "%SYS"//Run query to get the Username based on the email address - note the use of the UPPER function to remove issues with case sensitivity
&SQL(SelectIDinto :Username
FROM Security.Users
WHEREUPPER(EmailAddress) = UPPER(:Email)
)
//Set namespace back to the namespace the function was run fromSet$NAMESPACE = CurrNamespace
//Evaluate SQLCODE for result//Less than 0 is an error.If SQLCODE <0{
WRITE"SQLCODE="_$SYSTEM.SQL.Functions.SQLCODE(SQLCODE)
QUIT0
}
//Greater than 0 can really only mean Code 100, which is no results found.If SQLCODE > 0 {
QUIT1//No Result Found
}
Else {
QUIT1//Result Found
}
}
}
DEMO> WRITE Class(Demo.Utils.General.Users).UserFromEmail("YuriMarx@ACME.XYZ",.Output)1DEMO> WRITE Output
YMARXThis is by no means perfect as I have thrown it together for the example - please forgive the messy if/else's! ![]()