Forums

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

Fire an event in case a custom field changed value

Sandra Meessen
Contributor
July 22, 2018

Hello, I want to fire an event in case a custom field "employee type" changed value. When I look at the examples for the condition at the Script Listener "Fires an event when a condition is true", I see 2 examples which I need to combine but I don't know how:

  1. "Assignee changed" with coding "issue.assignee != originalIssue.assignee". 
  2. "Has select list value equal to" with coding "cfValues['My Select List']?.value == 'My value'".

Example 1 gives me the check for the change of value of the field. Example 2 gives me the check a specific value of a 2-word custom field. And now: how do I combine these 2 to get my check on changed value of my custom field "Employee type"? Thanks.

1 answer

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2018

Hello,

I guess it would be like this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldName =  "your custom field name"
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)

if (issue.getCustomFieldValue(cf).toString().equals(originalIssue.getCustomFieldValue(cf).toString)) {
return false;
} else {
return true
}
Sandra Meessen
Contributor
July 23, 2018

Hello Alexy, thanks for the help. Unfortunately I get an errormessage (see screenprint attached). Errormessage on Listener coding.JPG"tostring" seems to be the problem. How to correct? 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2018
import com.atlassian.jira.component.ComponentAccessor

def customFieldName =  "your custom field name"
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)

if (issue.getCustomFieldValue(cf).toString().equals(originalIssue.getCustomFieldValue(cf).toString())) {
return false;
} else {
return true
}
Sandra Meessen
Contributor
July 23, 2018

Hello Alexey, that seems to work fine! Thank you. The only thing that doesn't work now is the notification I setup in the Notification Scheme based on the new event.

  1. So, in my Listener, I defined above mentioned coding to check if my custom field value changed.
  2. If that is true, I fire a new event.
  3. This event is triggered in the Notifications Scheme to send a notification to a group. But this doesn't happen.  I don't see my expected notification in the Jira mail queue and don't receive an email. What did I not do? What did I miss?

Thanks.

Sandra Meessen
Contributor
July 23, 2018

Hello Alexey, the code is syntax correct, but it doesn't send a notification to my email address. What I did:

  1. define an event
  2. defined the listener with your coding and fire the new event.
  3. Based on the new event send a notification to myself.

As I can't preview this based on an issue, I put the same coding as above in an other new listener who sends a custom email. In that option I can preview what happens based on a real issue. If I do that review, I get the message "the condition evaluated to false". So, the coding above says my custom field "employee type" didn't change? And I know for sure it did! My custom field "employee type" is a select list single choice. Can that be the problem? Does this type of custom field need other coding?

Thank you. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2018

Could you add logging to your script and have a look in the logs, what the values are:

def customFieldName =  "your custom field name"
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)
log.error("issue.getCustomFieldValue(cf).toString(): ${issue.getCustomFieldValue(cf).toString()}")
log.error("originalIssue.getCustomFieldValue(cf).toString(): ${originalIssue.getCustomFieldValue(cf).toString()}")
if (issue.getCustomFieldValue(cf).toString().equals(originalIssue.getCustomFieldValue(cf).toString())) {
return false;
} else {
return true
}

You can find logs in the atlassian-jira.log file.

Sandra Meessen
Contributor
July 23, 2018

Hello Alexey, Thanks for thinking with me. I added the log coding and when I execute now I get following information: Errormessage with log.JPG

The 2 log lines tell me that new and original value of my custom field "employee type" are both value "other" and that's not true. I changed value "Internal" to value "Other". So my original value is not correct. What's of in my coding making Jira think that both new and original are the same as they are not? Thank you for your help again.

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2018

As far as I understand originalIssue does not take the current data of the issue. Let s try to do it antoher way:

import com.atlassian.jira.component.ComponentAccessor

def originalIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(issue.getKey())
def customFieldName =  "your custom field name"

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(customFieldName)

if (issue.getCustomFieldValue(cf).toString().equals(originalIssue.getCustomFieldValue(cf).toString())) {
return false;
} else {
return true
}
Sandra Meessen
Contributor
July 24, 2018

Sorry Alexey, doesn't make a difference. Is it a problem that my custom field is of type "select list (single choice)" and not a text field?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 24, 2018

It should not make a difference. Do you put this script to a validator?

Sandra Meessen
Contributor
July 24, 2018

What do you mean by "do you put this script to a validator"? No, at the moment it is not part of my workflow, I created it as a listener in case my custom field "employee type" changes, independent from which workflow part the issue is in. Should it be connected to a validator?

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 24, 2018

Ah, I see. that is why it does not work

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 24, 2018

To validate if a field was change in a listener, you should do like this:

 

def customFieldName =  "your custom field name"
log.debug("changeItems: " + changeItems.toString())
while (i < changeItems?.size()) {
log.debug(changeItems.getAt(i).get("field"))
if (customFieldName.equals(changeItems.getAt(i).get("field"))) {
return true;
}
i++;
}
return false
Sandra Meessen
Contributor
August 9, 2018

Hello Alexey, sorry for my late response, busy with a lot of other Jira stuff. I'm sorry, but I don't understand your peace of code. I get to this point:

Listener with Alexey's code.JPG

but why the red "x"? What in the syntax is wrong? I'm sorry, I'm not trained in this coding, all learning by myself, thanks for your help

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 9, 2018

It could be static errors. You can execute this script

Richard Duffy
Contributor
July 3, 2019

@Alexey Matveev 

Hi @Sandra Meessen 

Did you get this working? Im trying the very same thing with a custom field (Type - User picker multi) but i can seem to get the condition working?

 

My goal is when a user add's an assessor (user) on the edit screen the listener will run and create a sub-task for that assessor

 

Thanks 

Suggest an answer

Log in or Sign up to answer