Hi,
I am trying to update thecomponents field using a post function in Create issue, where is looks for the summary keywords and acoording to what is in the keywords, it will update the componenets field. This is what I have done so far but it is not working:
import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.comments.CommentManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.user.util.DefaultUserManager import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.project.Project if(issue.getSummary().equals("test this issue")) { ComponentManager componentManager = ComponentManager.getInstance() IssueManager issueManager = componentManager.getIssueManager() MutableIssue issue = componentManager.getIssueManager().getIssueObject(issue.id) Project project = issue.getProjectObject() ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "BITON") issue.setComponents([component.getGenericValue()]) User user = componentManager.jiraAuthenticationContext.getLoggedInUser() issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false) } else { return false }
Also tried
{code}
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.fields.CustomField
jqlSearch = "summary ~ test"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
//UserUtil userUtil = ComponentAccessor.getUserUtil()
//User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
SearchService.ParseResult parseResult = searchService.parseQuery(jqlSearch)
if (parseResult.isValid()) {
ComponentManager componentManager = ComponentManager.getInstance()
IssueManager issueManager = componentManager.getIssueManager()
MutableIssue issue = componentManager.getIssueManager().getIssueObject()
Project project = issue.getProjectObject()
ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
issue.setComponents([component.getGenericValue()])
User user = componentManager.jiraAuthenticationContext.getLoggedInUser()
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
} else {
log.error("Invalid JQL: " + jqlSearch);
}
{code}
error I get is
{code}
2013-10-08 19:28:03,968 http-bio-8080-exec-2 ERROR valiantys 1168x2806x1 1wl1gbg 80.87.25.98 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getProjectObject() on null object
2013-10-08 19:28:03,999 http-bio-8080-exec-2 ERROR valiantys 1168x2806x1 1wl1gbg 80.87.25.98 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getProjectObject() on null object
{code}
I changed the code quite a few times now.
You help will be really appreciated.
Regards
Anand
Community moderators have prevented the ability to post new answers.
Hi Henning,
Thats great, I got it working. So the final code for both is as follows (may be helpful for other viewers):
import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.Project import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.comments.CommentManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.user.util.DefaultUserManager import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.project.Project CustomFieldManager customFieldManager = componentManager.getCustomFieldManager(); CustomField cfEMCRequiredId = customFieldManager.getCustomFieldObjectByName("cf name"); String[] words = issue.getSummary().split("keywords") if(issue.getCustomFieldObjectByName("cf name").contains("cf value"){ Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name") issue.setComponentObjects([component]) } else { return false } for(int i = 0 ; i < words.length ; i++) { if(words[i].equals("keyword")) { Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name") issue.setComponentObjects([component]) }else { return false }
While creating the issue you cannot get the issue object from the issue manager because it doesn't exist at this moment. And if, you will get trouble with the index.
Just set the component on the already existing issue object (using issue.setComponentObjects(), maybe you should add the component to the existing components on the issue?) and don't do anything further (like updating the issue object or something similar). Make sure your post function is placed befor the "Creates the issue originally" post function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning,
Thanks for you answer. I don't want to add the componenet to an existing issue, I want it to auto populate. Are you saying leave the below blank:
{code}
MutableIssue issue = componentManager.getIssueManager().getIssueObject(issue.id);
{code}
The error that I am gteting is the following:
{code}
/secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getProjectObject() on null object
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, I understand that, but maybe the user already choose a component? If you are sure this could not be possible (because Component/s is not on the create screen), you can simply set the component.
Try this
import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.Project if(issue.getSummary().equals("test this issue")) { Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON") issue.setComponentObjects([component]) }
as your post function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning,
Thats great, I got it working. So the final code for both is as follows (may be helpful for other viewers):
import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.Project import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.comments.CommentManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.user.util.DefaultUserManager import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.project.Project CustomFieldManager customFieldManager = componentManager.getCustomFieldManager(); CustomField cfEMCRequiredId = customFieldManager.getCustomFieldObjectByName("cf name"); String[] words = issue.getSummary().split("keywords") if(issue.getCustomFieldObjectByName("cf name").contains("cf value"){ Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name") issue.setComponentObjects([component]) } else { return false } for(int i = 0 ; i < words.length ; i++) { if(words[i].equals("keyword")) { Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name") issue.setComponentObjects([component]) }else { return false }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anand, I'm glad it's working for you. Maybe you could also mark my answer as correct?
Thanks, Henning
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning,
Thats great, I got it working. So the final code for both is as follows (may be helpful for other viewers):
import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.Project import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.comments.CommentManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.user.util.DefaultUserManager import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.project.Project CustomFieldManager customFieldManager = componentManager.getCustomFieldManager(); CustomField cfEMCRequiredId = customFieldManager.getCustomFieldObjectByName("cf name"); String[] words = issue.getSummary().split("keywords") if(issue.getCustomFieldObjectByName("cf name").contains("cf value"){ Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name") issue.setComponentObjects([component]) } else { return false } for(int i = 0 ; i < words.length ; i++) { if(words[i].equals("keyword")) { Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name") issue.setComponentObjects([component]) }else { return false }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning,
So I am using:
import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.Project import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.comments.CommentManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.user.util.DefaultUserManager import com.atlassian.crowd.embedded.api.User import com.atlassian.jira.project.Project cf = customFieldManager.getCustomFieldObjectByName('cf Name') val = issue.getCustomFieldValue(cf) if(val.contains("name.surname")){ Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON") }
That doesn't work for me. Any ideas?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
To get the customfield you have to use an existing customFieldManager. So try
cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('cf Name')
And than, your code does nothing... you only find the component, you don't set it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning,
I don't understand when you say find it and not set it. I want it to be able to set the component if the customfield value contains a specific string. Hence
if(val.contains("name.surname")){ Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
which is not working.
Thanks for you help!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
As you can see in my previous comments
issue.setComponentObjects([component])
is missing at the end of your if statement to set the components.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Henning,
That worked perfectly!! Thanks. One other question thoug, can you look for a jql query rather than
if
(issue.getSummary().equals(
"test this issue"
))
Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),
"BITON"
)
issue.setComponentObjects([component])
Thanks once again!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You have to do the search after parsing the JQL.
Try this
import com.atlassian.jira.bc.issue.search.SearchService import com.atlassian.jira.bc.project.component.ProjectComponent import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.Project import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.user.ApplicationUsers import com.atlassian.jira.web.bean.PagerFilter jqlSearch = "summary ~ test this issue" SearchService searchService = ComponentAccessor.getComponent(SearchService.class) ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser() SearchService.ParseResult parseResult = searchService.parseQuery(ApplicationUsers.toDirectoryUser(user), jqlSearch) if (parseResult.isValid()) { searchResult = searchService.search(ApplicationUsers.toDirectoryUser(user), parseResult.getQuery(), PagerFilter.getUnlimitedFilter()) if (searchResult?.total > 0) { Project project = issue.getProjectObject() ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON") issue.setComponentObjects([component]) } }
This is not tested by me, so take care :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning, It works apart from, the JQL query. It should in theory look for 'testt', 'this', issue' in keywords jqlSearch =
"summary ~ 'test this issue'"
but right now, even if the summary includes just the word 'other', it will still map the component when it shouln't. Is there another way out in either one of the scripts tthat you provided?
Many thanks for your help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I though your JQL is only an example... Because it's used while creating the issue the issue will never be found, it could only be used to check for other issues. For matching against the summary of the issue itself you should use issue.summary ==~ /some regexp/ in you if statement. See here.
Maybe you could match against some more specific critieria like a checkbox or similar?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Henning,
Thanks for your help so far. I got it working with the for loop and searching for each word. Another question I have is how to I update the Component using customfield value? Right now I am using this and it is not working:
CustomFieldManaer customFieldManager = componentManager.gerCustomFieldManager();
if(issue.getCustomFieldObjectByName("cf Name").contains("cf value")}{
Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),
"BITON"
)
}
This doesn't work. Also how can I ask groovy to ignore case sensitive String?
Thanks!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can get a customfield value through
cfValues['cf Name']
Please be aware that if it's a selct field or similar you have to use
cfValues['cf Name'].getValue()
to get the String value.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I want to get is through groovy script. That is through workflow validator. Could you please show me how to di it in a custom groovy script. I tried the way shown in my previous comment.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
cf = customFieldManager.getCustomFieldObjectByName('cf Name')
val = issue.getCustomFieldValue(cf)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't know what is wrong with your code, but, Conditioned Workflow Functions for JIRA does some of this type of thing including running a JQL query and conditioning to determine whether to make a change. This may solve your problem in an easier way.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.