Forums

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

Listener to capture custom field value change

Venkat Krishnamoorthy
Contributor
October 10, 2017

Hello,

We have a custom field - "Deployed in Environment" which is checkbox field with multiple options (Dev, Test, Stage, Prod). We have corresponding date fields to capture deployment dates for these different environments. 

I am using the following script courtesy Thanos to capture change to this field. However, I am having trouble identifying which checkbox was actually checked during that particular event.

Example, if the Issue already has "Dev" and "Test" checkboxes checked and in the current edit event, if the user checks the "Stage" checkbox, I only want the current date to be set for field "Stage Deployment Date", the "Dev Deployment Date" and "Test Deployment Date" fields should not be disturbed. The below code is setting the current date to all three date fields since all 3 checkboxes are checked.

In commented if condition, I tried to check the old value of the customfield, but it doesnt seem to work right. 

What am I missing here? Thanks!!!

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import java.sql.Timestamp

Issue issue = issue

if (! issue.isSubTask())
return

if ((issue.getIssueTypeId() != "11601") && (issue.getIssueTypeId() != "11602") && (issue.getIssueTypeId() != "11603"))
return

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Deployed in Environment");
def selectedValues = customField.getValue(issue)*.value

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field=="Deployed in Environment"}
if (change) {
// if (("Dev" in selectedValues) && !("Dev" in change.oldstring)) {
if ("Dev" in selectedValues) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual Dev Deployment Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}
// if (("Test" in selectedValues) && !("Test" in change.oldstring)) {
if ("Test" in selectedValues) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual Test Deployment Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}
// if (("Stage" in selectedValues) && !("Stage" in change.oldstring)) {
if ("Stage" in selectedValues) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual Stage Deployment Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}
// if (("UAT" in selectedValues) && !("UAT" in change.oldstring)) {
if ("UAT" in selectedValues) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual UAT Deployment Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}
// if (("Production" in selectedValues) && !("Production" in change.oldstring)) {
if ("Production" in selectedValues) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual Prod Deployment Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}
}

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
PD Sheehan
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 Champions.
January 20, 2023

I don't think you need to convert the list of options to a map.

But also, the components formfield value method will return an array of ProjectComponentImpl objects. So simply applying "toString()" on it will not give you predictable results.

This should be better:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def comp = getFieldById("components")
def components = comp.value as List<ProjectComponent>
def category = getFieldById("customfield_20550")
def customField = customFieldManager.getCustomFieldObject(category.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (components.any { it.name == "01 - MAILFLOW" }) {
def optionsMap = options.findAll {
it.value in ["Domains management", "System Administration"]
}
category.setFieldOptions(optionsMap)
} else if (components.any { it.name == "02 - CLASSIFY" }) {
def optionsMap = options.findAll {
it.value in ["AWS Support", "Mail Format", "Data Model", "Bug Fixing"]
}
category.setFieldOptions(optionsMap)
}
Vasanta Kumaar
Contributor
January 22, 2023

Hi @PD Sheehan ,

 

Thanks for the answer, I updated and checked, and it's working fine. I completely forgot the component field will return an array.

Thanks,

Vasantakumaar

TAGS
AUG Leaders

Atlassian Community Events