Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Read value from CustomField

T I
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.
March 19, 2013

Hallo,

I would like to get the values of two custom fields that both are User Pickers. What do I have to change in the following code to accomplish this?

import com.atlassian.jira.ComponentManager

def componentManager = ComponentManager.instance
def customFieldManager = componentManager.getCustomFieldManager()

def cfX = customFieldManager.getCustomFieldObjectByName("X")
def cfY = customFieldManager.getCustomFieldObjectByName("Y")

if(cfX == null && cfY == null ){
	true
} else if(cfX != null && cfY == null ){
        if(cfX == currentUser){
		true
        } else {
        	false
        }
} else if(cfX == null && cfY != null ){
        if(cfY == currentUser){
		true
        } else {
        	false
        }
} else if(cfX != null && cfY != null ){
        false
}

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Henning Tietgens
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.
March 19, 2013

If you want to have the value of the customfield you have to use issue.getCustomFieldValue(cfX).

Henning

Henning Tietgens
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.
March 19, 2013

Uups... I edited the answer... as Jamie mentioned you can use cfX.getValue(issue). But i prefer the issue.getCustomFieldValue(cfX) way. Don't rember exactly why...

JamieA
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.
March 19, 2013

Yep, with an issue as the parameter.

JamieA
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.
March 19, 2013

There is a difference actually, can't remember what it is off top of my head... I would use issue.getCustomFieldValue(cf) too.

T I
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.
March 19, 2013

Thanks for the answers. I want to add this script to some transitions as a condition. The following code only works if I add it as a "Script Condition"-> "Simple scripted condition", but if I try to add it as a file "Script file path:" it doesn't work at all. It think the reason is the "issue". My file path: C:\Program Files\Atlassian\JIRA\groovy\reviewer.groovy

import com.atlassian.jira.ComponentManager

def componentManager = ComponentManager.instance
def customFieldManager = componentManager.getCustomFieldManager()

try{
	obTechReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Technical Reviewer'}
	cfTechReviewer = issue.getCustomFieldValue(obTechReviewer)

	obFuncReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Functional Reviewer'}
	cfFuncReviewer = issue.getCustomFieldValue(obFuncReviewer)

	// no reviewer entered for this issue 
	if(cfTechReviewer == null && cfFuncReviewer == null ){
		true
	// technical reviewer and no functional reviewer entered for this issue
	} else if(cfTechReviewer != null && cfFuncReviewer == null ){
		// check if currentuser is the technical reviewer for this issue
        	if(cfTechReviewer == currentUser){
			true
        	} else {
        		false
        	}
	} else if(cfTechReviewer == null && cfFuncReviewer != null ){
		// check if currentuser is the functional reviewer for this issue
        	if(cfFuncReviewer == currentUser){
			true
        	} else {
        		false
        	}
	} else if(cfTechReviewer != null && cfFuncReviewer != null ){
		// the reviewer have to be edited for this issue
        	false
	}
} catch (NullPointerException e){
	true
}

JamieA
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.
March 19, 2013

If it couldn't open the file it will be in the logs...

T I
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.
March 20, 2013
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issue for class: Script3

I get the following exception if I run it in the plug-in Script Runner.

There must be a way to get the issue id. But I need it for all the issues in a project not only for one issue. So something like "getIssueObject("XY-123")" won't work.

Henning Tietgens
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.
March 20, 2013

If you use the script as a condition, issue exists. If you run it from the console, issue doesn't exist. You can use

issue = componentManager.issueManager.getIssueObject('XY-123')

to test your script in the console. If you want to run the script in the console for all issues of a project look here.

Henning

T I
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.
March 20, 2013

I tested it for one issue. Console doen'st throw any exceptions with the follwoing code:

import com.atlassian.jira.ComponentManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil

def componentManager = ComponentManager.instance
def customFieldManager = componentManager.getCustomFieldManager()

UserUtil userUtil = ComponentAccessor.getUserUtil()
User currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (!currentUser) {
    currentUser = userUtil.getUserObject('jira_bot')
}

def issue = componentManager.issueManager.getIssueObject('ORD-5')

try{
	(...code...)
} catch (NullPointerException e){
	true
}

Now my problem is that if I put it as a condition like in the following image, it works:

But if I try to load it from a file like in the following image, it doesn't work:

The file can be found. The console don't throw any exceptions. So I have no ideas why it doesn't work.

PS: What I want with this code is to prevent a issue to be closed if a Technical or Functional Reviewer is entered. And if the currentUser is the reviewer it can be closed. Make it even sense to put this in a file? The only reason I want to put this in a file is to make it easier to do changes if needed.

Henning Tietgens
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.
March 20, 2013

Make sense... can you insert log.error statements into the file to test if the script is called?

JamieA
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.
March 21, 2013

Wonder if jira is running as LocalSystem and can't see files under there. In the admin script console run:

new File("c:/program files/whatever/reviewer.groovy").text

and see whether it returns the text of the file when you run it.

T I
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.
March 21, 2013

@Henning:

I added this:

(...imports...)
import org.apache.log4j.Category
 
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "test reviewer"

(...code...)

But I have no idea where the log is located to check if "test reviewer" was written in it. I don't use an IDE.

If I run the script in the "Script Runner" it works. There are no problems with running the script from path.

@Jamie:

Where do I find the adminscript console?

JamieA
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.
March 21, 2013

It;s in the screenshot above. Did you try my code? Enter only the code in the bottom textarea, not a file path as well. If you enter both the file wins.

Henning Tietgens
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.
March 21, 2013

The log is located within your jirahome directory under log/atlassian-jira.log

T I
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.
March 21, 2013

@Jamie:

Your code works. It shows me my code as a text.

@Henning:

This is what the log shows me after I changed the Issuestatus to "Start Progress" and then back to "Stop Progress".

2013-03-22 14:56:12,809 http-bio-8080-exec-8 DEBUG tiwaniec 896x711x1 laniyw 0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.PostFunction] test reviewer
2013-03-22 14:56:12,980 http-bio-8080-exec-8 DEBUG tiwaniec 896x712x1 laniyw 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] test reviewer
2013-03-22 14:56:13,869 http-bio-8080-exec-8 DEBUG tiwaniec 896x713x1 laniyw 0:0:0:0:0:0:0:1 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.PostFunction] test reviewer
2013-03-22 14:56:14,025 http-bio-8080-exec-8 DEBUG tiwaniec 896x714x1 laniyw 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] test reviewer

T I
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.
March 21, 2013

Yep I did it. here the code with more log.debug entries:

If I move in workflow the log shows me the following. That is exactly the result I wanted to get, but still I can close the Issue even I'm not the reviewer.

2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] started log
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: issue
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: ComponentManager and CustomFieldManager
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: UserUtil
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: check currentUser
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] entered try block
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: getting Technical Reviewer field
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] done: getting Functional Reviewer field
2013-03-22 15:30:57,180 http-bio-8080-exec-11 DEBUG tiwaniec 930x1139x1 7msp3n 0:0:0:0:0:0:0:1 /secure/IssueAction!default.jspa [onresolve.jira.groovy.PostFunction] func reviewer isnt null and currentuser isnt reviewer

Henning Tietgens
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.
March 21, 2013

Ok, both ways show your code is executed :-) So move on and add more debugging information to see what your code calculates...

Henning Tietgens
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.
March 21, 2013

Could you try to save your return value to a variable and return this variable as last step?

T I
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.
March 21, 2013

I'm not sure how to do this.

Henning Tietgens
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.
March 21, 2013

Add a

def result = false

at the beginning of your script. Then replace all "true" or "false" with "result = true" or "result = false"

and at the end write

return result

T I
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.
March 21, 2013

It doesn't help. If I put it as simple script it works...if I put it as script file path it doesn't work. The log for both variations is equal.

Henning Tietgens
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.
March 21, 2013

Mmh, weird.

@Jamie, any ideas?

JamieA
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.
March 21, 2013

Sorry you don't need to return it as such, just set it. It will be read out of the script binding when your script has finished evaluating. Again, the examples should help.

JamieA
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.
March 21, 2013

Yeah, you need to set passesCondition to a boolean and return than. Don't define passesCondition, just set to true or false, and return that as the last line.

In the built-in script called "simple scripted condition" you just return true or false, but if you're specifying a file you need it - see https://jamieechlin.atlassian.net/wiki/display/GRV/Conditions

I can see how this might be confusing... and sorry for not pointing this out earlier. I think "simple scripted condition" was my attempt to abstract away some of the detail.

T I
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.
March 21, 2013

This was it! Thank you both for the help.

Here the final script:

import com.atlassian.jira.ComponentManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil

Issue issue = issue

def componentManager = ComponentManager.instance
def customFieldManager = componentManager.getCustomFieldManager()

UserUtil userUtil = ComponentAccessor.getUserUtil()
User currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (!currentUser) {
    currentUser = userUtil.getUserObject('jira_bot')
}

try{
	obTechReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Technical Reviewer'}
	cfTechReviewer = issue.getCustomFieldValue(obTechReviewer)

	obFuncReviewer = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Functional Reviewer'}
	cfFuncReviewer = issue.getCustomFieldValue(obFuncReviewer)

	// no reviewer entered for this issue 
	if(cfTechReviewer == null && cfFuncReviewer == null ){
		 passesCondition = true
	// technical reviewer and no functional reviewer entered for this issue
	} else if(cfTechReviewer != null && cfFuncReviewer == null ){
		// check if currentuser is the technical reviewer for this issue
        	if(cfTechReviewer == currentUser){
			passesCondition = true
        	} else {
        		passesCondition = false
        	}
	} else if(cfTechReviewer == null && cfFuncReviewer != null ){
		// check if currentuser is the functional reviewer for this issue
        	if(cfFuncReviewer == currentUser){
			passesCondition = true
        	} else {
        		passesCondition = false
        	}
	} else if(cfTechReviewer != null && cfFuncReviewer != null ){
		// the reviewer have to be edited for this issue
        	passesCondition = false
	}
} catch (NullPointerException e){
	passesCondition = true
}

JamieA
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.
March 21, 2013

Good work ;-)

Thanks to Henning for adding in the diagnostics advice.

TAGS
AUG Leaders

Atlassian Community Events