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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,555,469
Community Members
 
Community Events
184
Community Groups

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 Kadam @Carmen Creswell [Adaptavist] 

 

I have this script he dupplicate the components between project and i would like when i change the name for the component , the name in the other project but actually when i change the name he create a new component with name changed

script_sync.png

You can help me ??

 

Thanks in advance

0 votes
Carmen Creswell [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.
May 15, 2018

 

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
AUG Leaders

Atlassian Community Events