Hello,
We would like to set a value in a date field, based on the sprint end date.
So if the sprint end date is 2/26/2019, that field would get this value.
Would this be possible using scriptrunner? If so, would anyone know how to do that?
Thank you!
Hi @Nir Haimov
When looking for it in custom fields, it appears as: Date Picker .
Example of values from an issue that has it filled in: 1/Apr/19 .
Thank you!
Hi @Mihai Mihai
Here is the complete code:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.sql.Timestamp;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
def changeHolder = new DefaultIssueChangeHolder();
def myCustomField = customFieldManager.getCustomFieldObject("customfield_12000")
//End Date
Date endDate = event.issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()
if (endDate != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(endDate);
cal.set(Calendar.MILLISECOND, 0);
log.error(new Timestamp(endDate.getTime()));
log.error(new Timestamp(cal.getTimeInMillis()));
myCustomField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(myCustomField), new Timestamp(cal.getTimeInMillis())),changeHolder);
} else {
myCustomField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(myCustomField), null),changeHolder);
}
Just change "customfield_12000" to your custom date field.
And it will work perfectly.
Put it in Listener for any "issue update" event
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am trying to copy Sprint Enddate to issue customField of type DateTime picker field, I am using below two ways it's not working, could you please correct me
Approach 1:
def latestSprintEndDate = customFieldManager.getCustomFieldObject("Latest Sprint End Date")
Timestamp tsFormat = (Timestamp) issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()
Date csNewDateValue = new Date(tsFormat.getTime() + 15*24*60*60*1000);
latestSprintEndDate.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(latestSprintEndDate), (Object)csNewDateValue),changeHolder)
Approach 2: your approach
Date endDate = issue.getCustomFieldValue(sprint)?.endDate?.last()?.toDate()
log.error "Sprint endDate ${endDate} Sprint Name : ${sprintName.toString()}"
if (endDate != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(endDate);
cal.set(Calendar.MILLISECOND, 0);
log.error(new Timestamp(endDate.getTime()));
log.error(new Timestamp(cal.getTimeInMillis()));
latestSprintEndDate.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(latestSprintEndDate), new Timestamp(cal.getTimeInMillis())),changeHolder);
} else {
latestSprintEndDate.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(latestSprintEndDate), null),changeHolder);
}
2020-06-23 08:31:26,741 ERROR [runner.ScriptBindingsManager]: Sprint endDate Tue Jun 23 10:46:00 PDT 2020 Sprint Name : TPK Sprint 1 2020-06-23 08:31:26,745 ERROR [runner.ScriptBindingsManager]: 2020-06-23 10:46:00.0 2020-06-23 08:31:26,745 ERROR [runner.ScriptBindingsManager]: 2020-06-23 10:46:00.0 2020-06-23 08:31:26,747 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2020-06-23 08:31:26,747 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896) at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source) at Script1884.run(Script1884.groovy:92)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm trying to do the same using the provided script, but I'm getting an error:
Not sure what to change in order to make the script work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Berry KerstenWere you ever able to get this working?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Mihai Mihai
You want to update a customfield of type "date" with the value of "sprint end date"?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Mihai Mihai
Instead of creating customfield and than a script to update that customfield.
Simply create scripted field with script runner, in the scripted field template, choose "Date Time".
Copy paste this script:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
//End Date
Date endDate = issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()
ignore the errors if ther are any, it should work any way.
Put the scripted field in your "view issue" screen.
If your issue have sprint, the scripted field will show you the "end date" as you wanted.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Nir Haimov
Thank you for the suggestion. I should have mentioned earlier that we already have a date field created, and we want that existing one to get values based on the sprint date.
I know this complicates things.
Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Mihai Mihai
OK, you will have to tell me if this customfield is of type "date" or "date time" this is a big difference.
Please check to be sure and et me know, i will help you
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Nir Haimov ,
I am able to get sprint end date using below code you shared in this page. Similar to Sprint start, i am looking for code sprint start date. i tried to put "startDate" in "endDate" place. But it showing as sprint started 4 days ago instead of date 8-july-2022.
Can you help with code for sprint start similar to sprint end date as below. what needs to be modified for getting sprint start date?
Sprint End Date:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
//End Date
Date endDate = issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I know there have been several responses and answers on this. I tested the March 02, 2019 response and used it as a base, though there were a few errors on some functions. I wanted to set a custom date field called Start Date using the Sprint Start Date and also set a custom date field called End Date using the Sprint End Date. Got it working using the listener script below:
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.event.issue.IssueEvent import com.atlassian.greenhopper.service.sprint.Sprint import java.sql.Timestamp; // Define functions needed
def customFieldManager = ComponentAccessor.getCustomFieldManager() def changeHolder = new DefaultIssueChangeHolder(); def issue = event.issue // Grab and define the Start Date Custom Field
def StartDateField = customFieldManager.getCustomFieldObject("customfield_00001") // Grab and define the End Date Custom Field
def EndDateField = customFieldManager.getCustomFieldObject("customfield_00002") // Check to see if the Sprint field was changed
def changeItems = ComponentAccessor.changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans if (!changeItems.any { it.field == 'Sprint' }) { //Skip this listener if the sprint field wasn't changed
return } // Get the current list of sprints on the issue
def sprintCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Sprint")[0] def sprints = issue.getCustomFieldValue(sprintCf) as List<Sprint> if (!sprints) { //if sprint field is empty, then we need to exit
return } // Determine the last sprint
def sprint = sprints.last() // Grab the Start Date from the Last Sprint.
def startDate = sprint.startDate.toDate() // Grab the End Date from the Last Sprint.
def endDate = sprint.endDate.toDate() // Log errors where needed against Start Date
if (startDate != null) { Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal.set(Calendar.MILLISECOND, 0); log.error(new Timestamp(startDate.getTime())); log.error(new Timestamp(cal.getTimeInMillis())); // Set the Start Date Field Value
StartDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(StartDateField), new Timestamp(cal.getTimeInMillis())),changeHolder); } else { StartDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(StartDateField), null),changeHolder); } // Log errors where needed against End Date
if (endDate != null) { Calendar cal = Calendar.getInstance(); cal.setTime(endDate); cal.set(Calendar.MILLISECOND, 0); log.error(new Timestamp(endDate.getTime())); log.error(new Timestamp(cal.getTimeInMillis())); // Set the End Date Field Value
EndDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(EndDateField), new Timestamp(cal.getTimeInMillis())),changeHolder); } else { EndDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(EndDateField), null),changeHolder); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.