I need to set epic link using groovy if a condition is true. I'm using the following script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_1000")
issue.setCustomFieldValue(epiclink,"MAR-1518")
But im getting this error:
I tried this code from this question but it didn't worked for me.
You just have a typo. In line
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_1000")
you are calling the CustomFieldManager class (inital is uppercase)
However, you need to call your variable customFieldManager which is from ComponentAccessor.customFieldManager
So, the above line should be:
def epiclink = customFieldManager.getCustomFieldObject("customfield_1000")
Related
I am trying to retrieve the value of the Summary system field in Jira using ScriptRunner. I am using the following code in scriptrunner but the problem is that the variable cf returned by the line def cf = customFieldManager.getCustomFieldObject("Summary") is null. How can I fix this and retrieve the value of the summary field in ScriptRunner?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Logger
def log = Logger.getLogger("atlassian-jira.log")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def cf = customFieldManager.getCustomFieldObject("Summary")
log.warn("MOUNA 1: "+cf)
issue.setCustomFieldValue(cf, "mouna")
log.warn("MOUNA 2: "+issue)
"Summary" field in Jira is not a custom field.
You can access the Summary field (and other system fields) directly from issue:
log.warn(issue.summary)
But for updating it in Post Function, you need to use MutableIssue class:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
def mIssue = (MutableIssue) issue
mIssue.setSummary("New Summary")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // Or you can get any user by using UserManager
ComponentAccessor.getIssueManager().updateIssue(user, mIssue, EventDispatchOption.ISSUE_UPDATED, false)
Of course, don't forget to import required classes at beginning of your code:
My epics contain the system field Rank, How can I add this field to the tasks of my epic using the script runner or other tools ?
You need com.atlassian.greenhopper.api.rank.RankService class
Try this to make rank of issue1 after issue2:
import com.atlassian.greenhopper.api.rank.RankService
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.atlassian.jira.component.ComponentAccessor
#WithPlugin('com.pyxis.greenhopper.jira')
#JiraAgileBean
RankService rankService
def user = ComponentAccessor.getUserManager().getUserByKey('userName')
def issue1 = ComponentAccessor.getIssueManager().getIssueByCurrentKey("KEY")
def issue2 = ComponentAccessor.getIssueManager().getIssueByCurrentKey("KEY")
def rank = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(rankFieldId)
rankService.rankAfter(user, rankFieldId, issue1, issue2)
Currently, we have routine issues where the status resets each week. This escalation service is working as intended. However, we are trying to incorporate the following script to adjust the system field 'DueDate' to be set for 7 days from the date when the escalation fires. The following code updates the date and returns the correct value but it doesn't appear to be committing the date of the provided ISSUEKEY.
Here is our code:
import java.util.Date
import java.sql.Timestamp
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager im=ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject('ISSUEKEY')
issue.setDueDate(new Timestamp((new Date() + 7).time))
issue.getDueDate()
What you are doing is correct but you need to commit your changes by calling IssueManager.updateIssue
import com.atlassian.jira.event.type.EventDispatchOption //add-this-line
import java.util.Date
import java.sql.Timestamp
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager im=ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject('ISSUEKEY')
issue.setDueDate(new Timestamp((new Date() + 7).time))
issue.getDueDate()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()//add-this-line
im.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH , false) //add-this-line
You are setting the issue value correctly but you are not saving your changes
IssueManager.updateIssue will save any changes
I have a piece of code. I am able to view all the values, but when it comes to updating the newly linked issue's Tempo "Account" field, nothing happens. I am using Scriptrunner For Jira's custom listener
I would expect the newly linked issue (issueInEpic) to have it's Tempo "Account" field (linkedField) value (oldCfValue) updated with the value (newCfValue) of the Epic's "Account" field value.
log.info() shows all of the values properly. The only issue is updating the linked issue.
import com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.IssueChangeHolder
def log = Logger.getLogger(com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent)
log.setLevel(Level.INFO)
if ([IssueLinkCreatedEvent].contains(event.getClass())) {
if (event.issueLink.issueLinkType.name == "Epic-Story Link") {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueManager = ComponentAccessor.getIssueManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
def accountField = cfManager.getCustomFieldObjects(event.issueLink.sourceObject)?.find{it.name == "Account"}
def linkedField = cfManager.getCustomFieldObjects(event.issueLink.destinationObject)?.find{it.name == "Account"}
MutableIssue issueInEpic = event.issueLink.destinationObject as MutableIssue
MutableIssue epic = event.issueLink.sourceObject as MutableIssue
def newCfValue = accountField.getValue(epic)
def oldCfValue = linkedField.getValue(issueInEpic)
issueInEpic.setCustomFieldValue(linkedField, newCfValue)
issueManager.updateIssue(currentUser, issueInEpic, EventDispatchOption.DO_NOT_DISPATCH, false)
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(event.issueLink.destinationObject)
ImportUtils.setIndexIssues(wasIndexing)
log.info(accountField)
log.info(newCfValue)
log.info(oldCfValue)
log.info(issueInEpic)
log.info(epic)
log.info(linkedField)
}
}
I need to set epic link using groovy if a condition is true.
I'm using the following script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_10101")
issue.setCustomFieldValue(epiclink,"TS-14")
If I set the epic link to null it works,
issue.setCustomFieldValue(epiclink, null)
but setting it to an issue key doesn't work.
Any help is appreciated.
I'm not sure why you aren't using ComponentAccessor.getIssueManager(), but that is a different question.
Epic Link is an Issue Object, not the Key
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_10101")
issue.setCustomFieldValue(epiclink,IssueManager.getIssueObject("TS-14"))