Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Get Entity Properties for User without REST call Edited

We have an external application which adds entity properties to an user through a REST call. (not to confuse with the user properties that can be edited through the jira user interface)

is there a way to access these properties using a groovy script in behaviours and post functions through the object model? the examples I am finding all make use of the old user properties, but not the new entity properties or use a rest call like this:

def rest_call = "/usr/bin/curl -u user:pw http://yourserver.com/rest/api/2/user/properties/propertyname?user=someuuser"
def rest_call_response = rest_call.execute().text

I was able to make the REST call work in a post function, but not in a behaviour, therefore I am interested in an approach without a rest call.

-----------------------

the actual use case I would like to accomplish looks like this:

the jira username of the manager of a user is stored as an entity property "managerUsername" with the service desk customer user object.

when a service desk customer creates a new service request issue through the service desk interface a custom field "Approver" should be initialized with the user object of the manager. the user should still be able to change the user in the field.

---------------------

As a post function in the create transition it works, but that would mean the customer can't override the value to be set.

1 answer

1 accepted

Hi @flaimo check out this manager 

ComponentAccessor.getUserPropertyManager()

Regards 

that's for the old user properties, not for entity properties.

@flaimo Well, you were right. The rest endpoint is using JsonEntityProperties. 

To achieve what you want you have to have a behaviour with the service desk mapping for your project. Then add this code to the Initialiser:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserUtils
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String managerUsernameProperty = "managerUsername"

String managerUsername = getUserProperty(currentUser, managerUsernameProperty)
FormField ffManager = getFieldByName("Manager")
ApplicationUser manager = UserUtils.getUser(managerUsername)
if (manager) {
ffManager.setFormValue(managerUsername)
ffManager.setDescription(manager.getDisplayName())
}

static String getUserProperty(ApplicationUser targetUser, String propertyKey) {
JsonEntityPropertyManager jsonEntityPropertyManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
String value = jsonEntityPropertyManager.get("UserProperty", targetUser.getId()).getAt(propertyKey)?.getValue()
value?.contains("\"") ? value.replaceAll("\"","") : value
}

Then you have to update the current property in the create postfunction of the workflow if its changed by the customer:

import com.atlassian.jira.bc.user.property.UserPropertyHelper
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.EntityProperty
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.event.entity.EntityPropertySetEvent
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserUtils

import java.util.function.BiFunction

CustomField cfManager = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Manager")?.first()

ApplicationUser newManagerUser = issue.getCustomFieldValue(cfManager) as ApplicationUser
ApplicationUser adminUser = UserUtils.getUser("admin")
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String managerUsernameProperty = "managerUsername"
String currentManager = getUserProperty(UserUtils.getUser("test"), managerUsernameProperty)
String newManager = newManagerUser?.getUsername()

if (currentManager != newManager) {
setUserProperty(adminUser, currentUser, managerUsernameProperty, newManagerUser.getUsername(), false)
}

static void setUserProperty(ApplicationUser adminUser, ApplicationUser targetUser, String propertyKey, String propertyValue, Boolean dispatchEvent) {
JsonEntityPropertyManager jsonEntityPropertyManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
UserPropertyHelper userPropertyHelper = ComponentAccessor.getComponent(UserPropertyHelper)
BiFunction<ApplicationUser, EntityProperty, ? extends EntityPropertySetEvent> eventBiFunction = userPropertyHelper.createSetPropertyEventBiFunction()
jsonEntityPropertyManager.put(adminUser, "UserProperty", targetUser.getId(), propertyKey, toJsonString(propertyValue), eventBiFunction, dispatchEvent)
}

static String getUserProperty(ApplicationUser targetUser, String propertyKey) {
JsonEntityPropertyManager jsonEntityPropertyManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
String value = jsonEntityPropertyManager.get("UserProperty", targetUser.getId()).getAt(propertyKey)?.getValue()
value.contains("\"") ? value.replaceAll("\"", "") : value
}

static String toJsonString(String property) {
"\"" + property + "\""
}

Finally, I dont know if its a bug or what, but when I set the formValue in the user picker in my scriptrunner version, it only shows the username, not the Full name formatted with the avatar. If it happens in your side too I suggest you to add a second behaviour to add a description to the field when it changes.

You only have to add the "Manager" field and set this script:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserUtils
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

FormField ffManager = getFieldById(getFieldChanged())
String managerValue = ffManager.getValue().toString()
if (managerValue) {
ApplicationUser manager = UserUtils.getUser(managerValue)
ffManager.setDescription(manager.getDisplayName())
}

Hope it helps! 

works like a charm. thank you. the manager name is actually displayed correctly in our service desk form (first script).

Glad to hear that :)

Reading the code I made a mistake, but I guess you fixed it. The script for the create postfunction in this line:

String currentManager = getUserProperty(UserUtils.getUser("test"), managerUsernameProperty)

Should be:

String currentManager = getUserProperty(currentUser, managerUsernameProperty)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Jira Cloud Performance Improvements

Hello everyone, I am a product manager in the Jira Cloud team focused on making sure our customers have a delightful experience using our products. Towards that goal, one of the areas which is extr...

175 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you