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
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:
I am trying to copy the value of one date field to another. But, the field is not getting set. When I hardcode the date value it works.
Original field is Scheduled Start Time and the field I am trying to copy the value to is: Planned Scheduled Start Time. These are both date type fields.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.datetime.DateTimeFormatter as JiraDateTimeFormatter
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.TemporalAccessor
import java.time.format.DateTimeFormatter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*
#BaseScript FieldBehaviours fieldBehaviours
// The date & date time field names to set a value
final plannedScheduledStart = "Planned Scheduled Start Time"
final plannedScheduledCompletion = "Planned Scheduled Completion Time"
final scheduledStart = "Scheduled Start Time"
final scheduledCompletion = "Scheduled Completion Time"
// Get the components
def customFieldManager = ComponentAccessor.customFieldManager
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
// Get the date & date time field objects
//def dateFieldStart = customFieldManager.getCustomFieldObjectsByName(plannedScheduledStart).first()
def dateFieldStart = getFieldByName("Planned Scheduled Start Time")
def dateFieldCompletion = customFieldManager.getCustomFieldObjectsByName(plannedScheduledCompletion).first()
def dateScheduledStart = customFieldManager.getCustomFieldObjectsByName(scheduledStart).first()
def dateScheduledCompletion = customFieldManager.getCustomFieldObjectsByName(scheduledCompletion).first()
def dataValue = dateFieldStart.getValue().toString()
getFieldByName("Scheduled Start Time").setFormValue(dataValue)
I also tried def dataValue = dateFieldStart.getValue() and it failed
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")
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"))
I want to set the Assignee based on Reporter. I wrote a script for that but doesn't work.
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.event.type.EventDispatchOption
CustomField srcField = ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObjectByName("Reporter")
cfwt = issue.getCustomFieldValue(sccField)
if(cfwt == "User Name"){
MutableIssue myIssue = issue
IssueManager issueManager = ComponentAccessor.getIssueManager()
UserManager userManager = ComponentAccessor.getUserManager()
myIssue.setCustomFieldValue(reviewerCustomField, assignee)
issueManager.updateIssue(userManager.getUser("User Name"), myIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
When run, I get the following error:
error:
startup failed: Script6.groovy: 12: unable to resolve class CustomField # line 12, column 13. CustomField srcField = ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObjectByName("Reporter") ^ 1 error
You are missing an import:
import com.atlassian.jira.issue.fields.CustomField
cfwt = issue.getCustomFieldValue(sccField)
sccField - here
CustomField srcField
srcField - here
You use this script as post-function?