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

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

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,323 views 14 20
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot