Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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
Asked in Jira Service Management

JSM June ask me anything (AMA)

Hello Community members! We’re wrapping up the end of JSM June with an Ask Me Anything (AMA) with the Jira Service Management product team. This is your chance to ask all your ITSM questions to o...

292 views 13 14
View question

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