Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Get username from CustomField in "" ScriptRunner Groovy

Deleted user March 21, 2018

Hi,

I working with transition which creating custom projects with ScriptRunner. I trying to get username from Multi User Picker and add this people to role "Administrators". I have problem with passing datas. My code:

def Key = customFieldManager.getCustomFieldObject("customfield_17306")
def KeyValue = issue.getCustomFieldValue(Key)


def project = projectManager.getProjectObjByKey("$KeyValue")
def projectRole = projectRoleManager.getProjectRole("Administrators")
def actors = ["174373","email-tasks"]

projectRoleService.addActorsToProjectRole(actors,
projectRole,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)

 and this working, beacuse actors are in "". When i trying get values from custom fields, like this:

def Admin = customFieldManager.getCustomFieldObject("customfield_17303")
def AdminValue = issue.getCustomFieldValue(Admin).username

I have this this format: [abc, test123, test], so without "". I must pass this to actors variable with "". Do you have idea what i can take data with "" or add this later? For all characters in username's.

1 answer

1 accepted

1 vote
Answer accepted
Nic Brough -Adaptavist-
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.
March 21, 2018

You've not accounted for the field being a multi-user picker.  When you get a value from a multiple-entry field like this, the value returned is an array of content objects.  It may be an empty array (field is empty), or only have one entry, but it's still an array.

So, you'll want to iterate over that, and work out which user(s) to add from the field.  Start with something like:

def cfValue = issue.getCustomFieldValue(multi-user-picker-field)

cfValue.each { ApplicationUser userToAdd ->

  actors.add(userToAdd)

}

Deleted user March 22, 2018

Good idea, but i have problem with method... 

  • cfValue - usernames from customfields
  • actors - usernames from definition in code
  • for each value in customfield is ok

but actors in each AdminValue? I don't understand... how you define "actors"?

Nic Brough -Adaptavist-
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.
March 22, 2018

I took it from your code in the question.

RichardA July 9, 2020

Hi @Nic Brough -Adaptavist- ,

my requirement is need to send the notification mail to all users available in 1,2 & 3 user picker fields.Thanks.

Nic Brough -Adaptavist-
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.
July 10, 2020

It's hard to read unformatted code.  I understand what you're trying to do, but could you tell us where it is going wrong?

RichardA July 10, 2020

Hi @Nic Brough -Adaptavist- , can you help to get the solution for this? Thanks.

RichardA July 13, 2020

Hi @Nic Brough -Adaptavist- , thanks for the reply. now this issue has been resolved Thanks.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events