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 votes
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
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

20,744 views 2 7
Join discussion

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