Forums

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

How to avoid updating a customfield via listener if it is already populated?

Shane Labonte July 29, 2020

I have written a listener to fire on the "issue updated" event which will populate a date/time custom field with the current time stamp, IF the issue has a specific label.

The script updates the customField as intended when the issue is updated, however I only want this timestamp to be entered when the label is initially added; I don't want the field to EVER update after that. How can I have this listener not update the field if it is already populated?

I have added my current script below...

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

def issueLabels = issue.getLabels();
boolean containsLabel = false;

for (String label : issueLabels) {
if (label.contains("examplelabel")) {
containsLabel = true;
}
}

if (containsLabel == true) {
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("myCustomField")
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
Shane Labonte July 29, 2020

I re-worked things a bit and figured this one out on my own.

Just a big ol' dum-dum momenbt on my end lol.

Final working version is below in case anyone stumbles across this and finds it useful.

 

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

def issueLabels = issue.getLabels();
boolean containsLabel = false;

for (String label : issueLabels) {
if (label.contains("exampleLabel")) {
containsLabel = true;
}
}

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("exampleCustomField")
def cfValue = issue.getCustomFieldValue(cf)
boolean hasDate = false;

for (Object object : cfValue) {
if (cfValue != null) {
hasDate = true;
}
}

def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().getTime())

if ((hasDate == false) && (containsLabel == true)) {
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), now),changeHolder)
}

 

Suggest an answer

Log in or Sign up to answer