Auto activate Inactive user throug Grovey Script

Sukanta Jana December 10, 2018

Please suggest a script. Our requirement is following.

1) Inactive user name will get from access request ticket customfield.

2) Check the user if it is Inactive, make the user to active. 

3) If user not found update ticket with comment 

We are looking to use the script in Escalation Services or inside workflow transaction of access ticket.

1 answer

0 votes
Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 11, 2018

Hello,

Assuming your custom field type is text field, the code should be something like this:

import com.atlassian.jira.bc.user.ApplicationUserBuilder
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom field name")
String userName = issue.getCustomFieldValue(customField)
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(userName)

if(user){
if(!user.isActive()){
UserService userService = ComponentAccessor.getComponent(UserService.class)
ApplicationUserBuilder userBuilder = userService.newUserBuilder(user)
user = userBuilder
.active(true)
.build()
UserService.UpdateUserValidationResult updateUserValidationResult = userService.validateUpdateUser(user)
if(updateUserValidationResult.isValid()){
userService.updateUser(updateUserValidationResult)
}else{
log.error("User update is invalid. Error: ${updateUserValidationResult.getErrorCollection()}")
}
}
}else{
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String commentBody = "comment text"
ComponentAccessor.getCommentManager().create(issue, currentUser, commentBody, false)
}
Sukanta Jana December 11, 2018

Hi @Ivan Tovbin,

Thank Ivan! that's a nice code. One more pint I missed to add. Once user activated, ticket need to move close status else ticket need to move 'In Progress' status.

Also the customfield is number field.

Regards,

Sukanta

Ivan Tovbin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 11, 2018

Hmm, what data is stored in your custom field if its a number field? I though you have a user name there or something. Could you please clarify?

Sukanta Jana December 11, 2018

Hi @Ivan Tovbin,

This is user name only. We are using employee ID as user name. Also if user is inactive than only script will activate them. Otherwise if user have no jira account or user is already activated then secript will put a comment and move the ticket to In Progress.

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events