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

ScriptRunner: Post Function to update component value for given issue

Raju Kadam May 14, 2018

* 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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Raju Kadam May 15, 2018

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.

Salim Hammar February 10, 2022

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.

TAGS
AUG Leaders

Atlassian Community Events