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

Define component for a groovy script to assign the component

I'm attempting to write a workflow function script to automatically set a component on an issue at issue creation, anytime there isn't already a component set. Example: an end user fills out a generic IT request. Then based on our criteria we will set a component to the issue. 

The problem I run into is the documentation and forums I've read all point to listing one specific component to set. We want to set several. (secondary problem, my placeholder comment comments on all issues, not just where the component is blank)

 

Here is what I have so far:

{code:groovy}
import com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.jira.issue.fields.CustomFieldimport com.atlassian.jira.util.ImportUtilsimport com.atlassian.jira.user.util.DefaultUserManagerimport com.atlassian.jira.project.Projectimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.scriptrunner.runner.util.UserMessageUtilimport com.atlassian.jira.event.type.EventDispatchOption
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();MutableIssue myIssue = issueProject project = myIssue.getProjectObject()
def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'SH')
if (theComponent == null) {commentManager.create(    issue,        user,    "Generic placeholder comment", true)}
{code}

 

Thanks!

2 answers

1 accepted

0 votes
Answer accepted

The specific part I was wanting to do, find an issue where the component is blank and then do something (set component), I found the answer here:

https://community.atlassian.com/t5/Jira-questions/Test-if-Component-s-is-empty-with-Groovy-script/qaq-p/453138

 

I modified mine to be == 0 and it acted on an issue where the component was blank. 

I haven't tested this, but I changed it so that there were no errors. Let me know if that works.

 

 

 

import com.atlassian.jira.issue.CustomFieldManager
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.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.event.type.EventDispatchOption

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def myIssue = event.issue as MutableIssue
Project project = myIssue.getProjectObject()
def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'SH')
if (theComponent == null) {commentManager.create(myIssue, user, "Test Comment", true)}

 

In all my reading I was seeing that in this line you need to include the component that you want to set on the issue with the script. 

def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'SH')

 

The issue in our situation is we have several, 70 in our case, that we want to set automatically based on specific criteria. 

My question was is there a better way to do this then what we've attempted so far? I did attempt to put all 70 in place of where it says 'SH' above. That didn't work. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Desk

The Complete Guide to Atlassian for ITSM

Hi Atlassian Community! This is Teresa from the Atlassian team. My colleague Paul Buffington @Buff and I are excited to share a brand new ITSM resource we’ve created – "The Complete Guide to At...

2,151 views 15 22
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