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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

41 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you