Hey guys, I’m working on a custom handler for the issueUpdated event in Jira. I want to change how it works based on whether a specific custom field has been updated. The tricky part is I need to compare the old and new values of this field.
I know how to get the current value from the Issue, but I’m stumped on how to get the previous one. This is for a custom field, not a standard one. I’m using Jira 4.0.2 on Windows, if that matters.
I was thinking maybe I could look through the change history to find the last known value. Something like this:
As someone who’s worked extensively with Jira customization, I can tell you that retrieving old values of custom fields can be tricky. While the change history approach you mentioned can work, it’s not always the most efficient or reliable method, especially in high-volume environments.
I’ve found success using the IssueChangeHolder class in combination with the IssueEventListener. Here’s a rough idea of how you can implement this:
public void onIssueEvent(IssueEvent event) {
if (event.getEventTypeId().equals(EventType.ISSUE_UPDATED_ID)) {
Issue issue = event.getIssue();
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
CustomField customField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID);
Object oldValue = changeHolder.getChange(customField);
Object newValue = issue.getCustomFieldValue(customField);
if (oldValue != null && !oldValue.equals(newValue)) {
// Your logic here
}
}
}
This method is more direct and doesn’t rely on querying the change history. It’s also more performant for real-time tracking of field changes. Just make sure to properly initialize your CustomFieldManager and other necessary components.
hey markseeker91, i’ve dealt with this before. the changehistory approach can work, but it’s not always reliable. have you considered using the beforeFieldChange() method in your listener? it lets you grab the old value before it’s updated. just remember to implement the InitializingBean interface. hope this helps!
I’ve encountered this issue in my Jira development work. While the change history approach can work, it’s not ideal for real-time tracking. A more robust solution is to implement a custom FieldScreenRenderer for your custom field. This allows you to capture the old value before it’s changed.
Here’s a simplified example:
public class CustomFieldRenderer extends AbstractFieldScreenRenderer {
private String oldValue;
@Override
public String getEditHtml(FieldLayoutItem fieldLayoutItem, FieldScreen fieldScreen, Issue issue) {
this.oldValue = issue.getCustomFieldValue(fieldLayoutItem.getOrderableField());
// Rest of the implementation
}
// Implement other necessary methods
}
This approach ensures you always have access to the previous value when handling the issueUpdated event. It’s more efficient and reliable than querying the change history each time.