Use Case Scenario
- When a custom field (Date type) is older than current day, update the drop down (select list) field to 'Expired', else update it to 'Active'
- Script Runner Custom Script Listener is needed
If it helps anyone,
Found a way to do this
Create a Custom listener by browsing to Script Listeners page
Note: Update select list field checking Date custom field
Project Name: ABC
Events: All Issue Events
Inline Script: (see below)
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("Script to update customfield")
log.setLevel(Level.INFO)
def issue = event.issue as Issue
log.info(issue)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
// Get dropdownField
def customField = customFieldManager.getCustomFieldObject(20311)
log.info(customField)
// Get Date Field: Analyst Expiration Date
def aexpField = customFieldManager.getCustomFieldObject('customfield_20310')
log.info(aexpField)
def expirationDate = issue.getCustomFieldValue(aexpField) as Date
log.info(expirationDate)
def expirationVal = expirationDate.getTime()
log.info(expirationVal)
// Current Date
def currDate = new Date() as Date
log.info(currDate)
def currVal = currDate.getTime()
log.info(currVal)
def cfConfig = customField.getRelevantConfig(issue)
def act_value = optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Active'}
def exp_value = optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Expired'}
def changeHolder = new DefaultIssueChangeHolder()
if (currVal >= expirationVal) {
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), exp_value), changeHolder)
}
else {
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), act_value), changeHolder)
}
Hope this helps
Chander Inguva
this script helps me a lot, thank you so much for sharing!!
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.