Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Listener to Update Select List field JIRA

Chander Inguva
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2019

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

1 answer

3 votes
Chander Inguva
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2019

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

Jose Luis Casarrubias July 29, 2020

this script helps me a lot,  thank you so much for sharing!!

Suggest an answer

Log in or Sign up to answer