Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

81 views 0 9
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you