Hi,
On the Create screen "when should it be restored?" I have a field called I have 1 week, 2 week and 1 month options in this field.
I also have a field called "End Date" on the View screen.
When I select the '1 week' option on the Create screen, I want the date in the 'End Date' field to be automatically set one week after the date I created the record, two weeks when I select the '2 week' option, and one month later when I select the '1 month' option.
The script below works correctly for the 1 week and 2 week options but incorrectly for the 1 month option.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.CustomFieldType
import java.sql.Timestamp;
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.time.LocalDate
def issueManager = ComponentAccessor.getIssueManager()
//def issue = issueManager.getIssueObject("DEMO-48")
def mutableIssue = (MutableIssue) issue
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def user = authenticationContext.getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def Calendar cal = Calendar.getInstance();
def endDateCF = customFieldManager.getCustomFieldObject("customfield_11334")
def whenShouldRestoreCF = customFieldManager.getCustomFieldObject("customfield_11335")
def fieldConfig = whenShouldRestoreCF.getRelevantConfig(issue)
def option1 = optionsManager.getOptions(fieldConfig).find {it.value == "1 week"}
def option2 = optionsManager.getOptions(fieldConfig).find {it.value == "2 week"}
def option3 = optionsManager.getOptions(fieldConfig).find {it.value == "3 week"}
if( issue.getCustomFieldValue(whenShouldRestoreCF) == option1){
def newUpdate = new Timestamp(cal.getTimeInMillis()+ 7*1000*24*60*60);
mutableIssue.setCustomFieldValue(endDateCF, newUpdate)
}
if( issue.getCustomFieldValue(whenShouldRestoreCF) == option2 ){
def newUpdate = new Timestamp(cal.getTimeInMillis()+ 14*1000*24*60*60);
mutableIssue.setCustomFieldValue(endDateCF, newUpdate)
}
if( issue.getCustomFieldValue(whenShouldRestoreCF) == option3 ){
LocalDate futureDate = LocalDate.now().plusMonths(1);
def newUpdate = futureDate
mutableIssue.setCustomFieldValue(endDateCF, newUpdate)
}
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
//return issue.getDueDate()
What is the problem, how can I edit it?
Thanks for your help,
Have a nice day :)
Where are you trying to do this?
In a listener, post function or in a behaviour?
In a behaviour, you could have the data automatically populated on-screen and either give the user the option to change it or lock the field as read-only.
But since you called the updateIssue manually, I'll assume you are trying to do this in a listener.
Here is a sample that I think will work for you:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.index.ha.IndexUtils
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def indexingService = ComponentAccessor.getComponent(IssueIndexingService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def optionDayMap = [
'1 week': TimeCategory.getWeek(1),
'2 week': TimeCategory.getWeek(2),
'3 week': TimeCategory.getMonth(1)
]
def issue = issueManager.getIssueObject('DEMO-1')
def selectFieldId = 'customfield_11335'
def endDateFieldId = 'customfield_11334'
def selectCf = customFieldManager.getCustomFieldObject(selectFieldId)
def endDateCf = customFieldManager.getCustomFieldObject(endDateFieldId)
def selectedOption = issue.getCustomFieldValue(selectCf) as Option
def duration = optionDayMap[selectedOption.value]
def endDate = TimeCategory.plus(new Date() , duration)
issue.setCustomFieldValue(endDateCf, endDate.toTimestamp())
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH,false)
def wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
indexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
You have to remember that in a Listener and any other script other than post-function, you have to manually tell the system to re-index the issue.
This version of the script removes some complexity.
All the options are handled with the same code, we just have to fetch the appropriate duration based on the selected option.
With TimeCategory there is no need to bother with local date or calendar instances.
Just the plain date object can be augmented with appropriate duration and then converted to a timestamp.
I am getting error from timecategory definition, how can I fix it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah, sorry, my fault.
I forgot to include an import.
Add
import groovy.time.TimeCategory
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much again Peter. You always support me.
Have a good day :)
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.