JQL Search Compare Scripted Field Values

Hi,

 

I have two scripted fields ("Chargeable Work" and "Invoiced") I'm trying to create a JQL search filter that will only display results where "Chargeable Work" doesn't equal "invoiced" this will allow our accounts team to know what will still require invoicing.

The filter I've got is this:

"Chargeable Work" !~ "Invoiced" AND "Invoiced" is not empty

The problem is that this returns issues where the "Chargeable Work" and "Invoiced" values match.

 

I've also tried to write another scripted field that returns "yes" if the field values match, but I can't get that to work either:

import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField customField_name1 = customFieldManager.getCustomFieldObjectByName("Chargeable Work");
CustomField customField_name2 = customFieldManager.getCustomFieldObjectByName("Invoiced");

if ( issue.getCustomFieldValue(customField_name1) = ( issue.getCustomFieldValue(customField_name2)
return "Yes"
} return "No"

 

Any help would be greatly appreciated.

Thanks,

Rich


10 answers

1 accepted

1 vote
Accepted answer

If anyone is interested here is the final code:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import org.apache.log4j.Logger;
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
log.setLevel(org.apache.log4j.Level.DEBUG)
 
log.debug "setting custom fields"
CustomField customField_chargableWork = customFieldManager.getCustomFieldObjectByName("Chargeable Work");
CustomField customField_invoiced = customFieldManager.getCustomFieldObjectByName("Invoiced");
 
log.debug "running logic"
log.debug "customField_chargableWork"
log.debug (customField_chargableWork)
log.debug "customField_invoiced"
log.debug (customField_invoiced)
if (issue.getCustomFieldValue(customField_invoiced) != null){
    if (issue.getCustomFieldValue(customField_chargableWork) == (issue.getCustomFieldValue(customField_invoiced))) {
    log.debug "custom fields match"
    return "Customer Invoiced"
}} else {
    log.debug "custom fields do not match"
    return null;
}

This will also return the new custom field 'Customer Invoiced' as null if both the other fields are empty.

Hi Richard 

Did you set up a searcher, which should be compatible with your scripted field return value - which I suppose in your case should be a free text searcher, and also did you perform a re-index ?

Hi Thanos,

 

Yes I set the fields up with free text searchers. The problem is, when I search with that string it returns all the issues that have both the "invoiced" and "Chargeable Work" fields, it doesn't show up the issues who's "Chargeable Work" and "Invoiced" Field values match/do not match.

 

Thanks,

Richard

JQL doesn't have this concept of "if values match", or don't match or whatever. Or let me phrase this another way... if you were using standard text fields, could you write a query that does this?

Ah OK. Do you have any advice on getting my script to work?

 

Thanks,

Richard

Using a single equal sign is used to set variables. You'd use == or === to make a comparison.

Thanks,

I tried changing to:

if ( issue.getCustomFieldValue(customField_name1) == ( issue.getCustomFieldValue(customField_name2)

 

But it's still failing, do you have any other advice on getting the script to work?

 

Thanks,

Richard

if ( issue.getCustomFieldValue(customField_name1) == ( issue.getCustomFieldValue(customField_name2)
return "Yes"
} return "No"

 

This is your code, with the = operator changed to a == operator. Foremost, I do see a syntax error – The IF statement is missing a bracket {.

I'm going to give you some advice – You cannot be scripting in JAVA or Groovy blind. You need to be able to see what you're doing. This means you should at least log your inputs so you know what you're actually comparing.

 

import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import org.apache.log4j.Logger;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

log.setLevel(org.apache.log4j.Level.DEBUG)

log.debug "setting custom fields"
CustomField customField_chargableWork = customFieldManager.getCustomFieldObjectByName("Chargeable Work");
CustomField customField_invoiced = customFieldManager.getCustomFieldObjectByName("Invoiced");

log.debug "running logic"
log.debug "customField_chargableWork"
log.debug (customField_chargableWork)
log.debug "customField_invoiced"
log.debug (customField_invoiced)
if (issue.getCustomFieldValue(customField_chargableWork) == (issue.getCustomFieldValue(customField_invoiced))) {
	log.debug "custom fields match"
	return "Customer Invoiced"
} else {
	log.debug "custom fields do not match"
	return null;
}

 

Unfortunately I really can only do so much testing here for you. Please note a few changes I've made –

  • I really hate poorly named variables, so I changed the customfield variables
  • I am setting the logging level to 'debug,' and then using the logging utility to put information in the log for myself, so I can see what's happening
    • Since I am using "debug," I can just set the level to "info" or something later which keeps my logging intact but disabled for production use
  • I use a == instead of a = operator
  • I return actual information instead of "yes" or "no"
  • You should return NOTHING for the cases when they don't match, otherwise every other issue in the system which doesn't have this field hidden in the field configuration will have "no" set or whatever, probably not desirable at all

I can keep helping, but you need to tell me what's going wrong – Just telling me it's not working won't help me help you.

Thanks for your advice, I tried your script, but it fails with the error:

 

2016-04-20 15:51:43,286 ERROR [customfield.GroovyCustomField]: *************************************************************************************
2016-04-20 15:51:43,286 ERROR [customfield.GroovyCustomField]: Script field failed on issue: CS-21, field: Invoicing Complete
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script8382.groovy: 18: expecting ')', found 'else' @ line 18, column 3.
   } else {
     ^
1 error

 

Thanks,

Richard

Thank you for posting the error. I was missing several parenthesis somehow, sorry about that! Groovy is a very forgiving language but not that forgiving.

I updated the source in the previous comment to prevent a bunch of code from being present through this thread.

Thanks for your continuing help, it failed with an erro in the line:

import com.atlassian.jira.ComponentAccessor;

so I changed it to:

import com.atlassian.jira.component.ComponentAccessor;

It now fails with the error:

2016-04-21 14:35:46,631 DEBUG [customfield.GroovyCustomField]: setting custom fields
2016-04-21 14:35:46,631 DEBUG [customfield.GroovyCustomField]: running logic
2016-04-21 14:35:46,631 ERROR [customfield.GroovyCustomField]: *************************************************************************************
2016-04-21 14:35:46,631 ERROR [customfield.GroovyCustomField]: Script field failed on issue: CS-1257, field: Invoicing Complete
groovy.lang.MissingMethodException: No signature of method: org.apache.log4j.Logger.debug() is applicable for argument types: (java.lang.String, com.atlassian.jira.issue.fields.CustomFieldImpl) values: [customField_chargableWork, Chargeable Work]
Possible solutions: debug(java.lang.Object), debug(java.lang.Object, java.lang.Throwable), dump(), getAt(java.lang.String)
	at Script10240.run(Script10240.groovy:15)

Thanks,

Richard

0 votes
Steven Behnke Community Champion Apr 21, 2016

Hi Richard. No worries, let's see this through eh?

That is an error on my side, I can't log things like that! cheeky

 

log.debug "customField_chargableWork"
log.debug (customField_chargableWork)
log.debug "customField_invoiced"
log.debug (customField_invoiced)

The sciprt is now working! Thank you for your help.

The problem I'm having now is that all issues where "Chargeable Work" and "invoicing" are empty are appearing with "Customer Invoiced". Is it possible to add in "and is not empty" to those two fields in the script?

Thanks,

Richard

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

346 views 0 1
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you