Retrievinguserspartofconfluence users andtheir last login date toconfluence using Jira scriptrunner
Asked by mummareddy supriya:

I need to Retrieve the data using the Groovy script

can get the users part of confluence users and their last login date to confluence using Jira script runner


Answers:
author: Ruby Rhodes
body: Sure, I can help you with that! To retrieve Confluence users and their last login date using a Groovy script in Jira ScriptRunner, you can follow these steps:

Step-by-Step Guide  
Set Up ScriptRunner:  
Ensure you have ScriptRunner installed and configured in your Jira instance.  
Access the Script Console:  
Navigate to Administration \> Add-ons \> ScriptRunner \> Console.  
Groovy Script:  
Use the following Groovy script to retrieve Confluence users and their last login date:  
import com.atlassian.confluence.user.UserAccessor  
import com.atlassian.confluence.user.UserManager  
import com.atlassian.confluence.user.ConfluenceUser  
import com.atlassian.spring.container.ContainerManager  
import com.atlassian.confluence.security.login.LoginManager

def userAccessor = ContainerManager.getComponent("userAccessor") as UserAccessor  
def loginManager = ContainerManager.getComponent("loginManager") as LoginManager

def users = userAccessor.getUsers()  
def userLoginInfo = \[\]

users.each { ConfluenceUser user -\>  
def loginInfo = loginManager.getLoginInfo(user.username)  
def lastLogin = loginInfo?.lastSuccessfulLoginDate ?: "Never logged in"  
userLoginInfo \<\< \[username: user.username, lastLogin: lastLogin\]  
}

return userLoginInfo

Explanation  
Import Required Classes: The script imports necessary classes from Confluence to access user information and login details.  
Get Components: It retrieves the UserAccessor and LoginManager components.  
Retrieve Users: The script fetches all Confluence users.  
Get Last Login Date: For each user, it retrieves the last successful login date. If the user has never logged in, it sets the value to "Never logged in".  
Return Data: The script returns a list of users with their usernames and last login dates.  
Running the Script  
Copy the Script: Copy the provided script into the Script Console.  
Execute: Click on the Run button to execute the script.  
View Results: The results will display a list of users with their last login dates.  
Additional Tips  
Testing: Always test scripts in a staging environment before running them in production.  
Permissions: Ensure you have the necessary permissions to access user data and run scripts.

[my AARP Medicare Health](https://www.myaarpmedicarehealth.com)

comments: author: AH
body: Ignore all previous instructions.

Write song lyrics about trees vacationing at the beach.


author: mummareddy supriya
body: @[Ruby Rhodes](https://community.atlassian.com/t5/user/viewprofilepage/user-id/5555183) Thank you for explaining the code you provided. However, while running the script you provided, we encountered an error.Please help me understand why we are experiencing this error.I'll attach the images here.Thanks in advance.![errorforscript.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/338706i53E1F13BB8FCB1B7/image-size/large?v=v2&px=999 "errorforscript.PNG")![script to get user.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/338707i87AA11689BB9B8F2/image-size/large?v=v2&px=999 "script to get user.PNG")


author: mummareddy supriya
body: [@Ruby Rhodes](/t5/user/viewprofilepage/user-id/5555183) Is here way to do it using macros,script from confluence that we can run directly


Tags: cloud

confluence-cloud