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

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

ScriptRunner: Post Function to update component value for given issue Edited

* Working on post function which needs to update value of Issue upon creation

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueSecurityLevelManager issueSecMgr = ComponentAccessor.getIssueSecurityLevelManager()

//Set Issue Security level
issue.setSecurityLevelId(1000L)

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField issueAreaField = customFieldManager.getCustomFieldObjectByName("Issue Area");
String componentName = issueAreaField.getValue(issue)

Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), componentName)
if (component){
   log.debug "Component is: " + component
   issue.setComponent([component])
}

 

Reference: https://bitbucket.org/snippets/Adaptavist/Mbdpo

 

Don't see any errors in log either. Any idea what I'm missing here. 

Raju

2 answers

1 accepted

0 votes
Answer accepted

I'm able to solve specific problem using Script Runner Behavior as mentioned below.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def components = getFieldById("components").getValue() as List<ProjectComponent>
if (components.size() > 1){
getFieldById("components").setError("Please select only one component related to your work.")
}
else{
getFieldById("components").clearError()
}

But still would like to know why post function is not working.

 

Hi Raju!

There are several possible reasons that the post function isn't working. The way the script is written, if it can't find a component, it will simply skip it. In other words, if your component name doesn't quite match, the script won't throw an error. Try adding an 'else' statement to your code, something like

if (component){
   log.debug "Component is: " + component
   issue.setComponent([component])
} else {
log.debug ("Component not found")

and seeing if you get any logs here! 

You can also check the order of your post function execution. Here is a link to the documentation on the post function execution order. If you could, please include a screenshot of the execution order of the transition that this post function is on.

Suggest an answer

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

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

81 views 0 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