Modify custom field with script.

 Hi,

 I try to modify a customField during it's creation in the workflow by a post function

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

GroupManager groupManager = ComponentAccessor.getGroupManager()
def groups = groupManager.getGroupNamesForUser(currentUser)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf= customFieldManager.getCustomFieldObjects(issue).find {it.getName() == "Field Name"}

if (! accessGroups)
{
  groups.each
  {
    if( it.startsWith("jira") )
    {
      issue.setCustomFieldValue(cf, it)
    }
  }
}
return true

 

but it doesn't work.

 

In the script console I tested

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
 
//get issue by key
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ASWTK-50")
//get current user
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()
 
GroupManager groupManager = ComponentAccessor.getGroupManager()
def groups = groupManager.getGroupNamesForUser(currentUser)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject("customfield_12703")
 
log.warn(customField.getValue(issue))

if (! customField)
{
  log.warn("Field found")
  groups.each
  {
    if( it.startsWith("jira-users") )
    {
      log.warn(it)
      issue.setCustomFieldValue(customField, it)
      log.warn(customField.getValue(issue))
    }
  }
}
 
log.warn(customField.getValue(issue))
 
return true

 But it doesn't get into the first if.

The Output of the warn is empty if the customField-Value is empty and [com.atlassian.crowd.embedded.impl.ImmutableGroup@976d6f29] if I fill it with a Group. BTW. the field is a GroupSelector-Field.

 

1 answer

0 vote
Vasiliy Zverev Community Champion Mar 10, 2016

Do you use this code into postfunction or you create your own plugin?

ALso you can test code via ScriptConsole provided by ScriptRunner plugin.

I use it in workflow postfunction.

For the ScriptConsole I don't know how to get the issue and the current User

Vasiliy Zverev Community Champion Mar 10, 2016

Here it is

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser

//get issue by key
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ZVER-3")

//get current user
ApplicationUser curUser = ComponentAccessor.getJiraAuthenticationContext().getUser()

Thanks. I updated the main post.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

195 views 4 8
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you