Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,472
Community Members
 
Community Events
168
Community Groups

Listener to capture custom field value change

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

0 votes
Answer accepted

I think I figured out the syntax for checking fields' original value. Here is my working script,

 

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
Issue issue = event.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) && !(change.getString("oldstring").contains("Dev"))) {
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) && !(change.getString("oldstring").contains("Test"))) {
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) && !(change.getString("oldstring").contains("Stage"))) {
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) && !(change.getString("oldstring").contains("UAT"))) {
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 (("Prod" in selectedValues) && !(change.getString("oldstring").contains("Prod"))) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Actual Production Deployment Date")
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}
}

 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

208 views 1 6
Read article

Atlassian Community Events