Forums

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

getCustomFieldValue() producing an error

Tyler Brown-Jones June 7, 2018

Hi

So i am trying to get the value of a custom field that stores the remaining time, here is my code

def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Total Remaining Estimate") 
def cFieldValue = (double) issues.getCustomFieldValue(estField)

In my logs this is returning the following error

2018-06-07 17:45:02,439 Caesium-1-1 ERROR anonymous no estimate - send email [c.o.jira.groovy.GroovyService] Script service failed: D:\Atlassian\scripts\IHNE.groovy
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.ImmutableCustomField) values: [Total Remaining Estimate]

 Hope someone is able to answer/resolve this issue for me as i am stumped.

Kindest regards and many thanks

 

Tyler

2 answers

1 accepted

1 vote
Answer accepted
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.
June 7, 2018

Hello,

How do you initialize the issues variable? It is an ArrayList. I guess it is an ArrayList<Issue>. But to make sure I need to see your code.

If it is ArrayList<Issue> it would work like this:

def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Total Remaining Estimate") 
def cFieldValue
issues.each{
cFieldValue = (double) it.getCustomFieldValue(estField)
}
Tyler Brown-Jones June 7, 2018

Thanks for the reply, this is my full code:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import java.util.logging.Logger
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter


def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug("start of code")

jqlSearch = 'updated >= -4w'; // Create the JQL query to perform

SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
UserUtil userUtil = ComponentAccessor.getUserUtil();
def user = ComponentAccessor.getUserManager().getUserByName("jira_bot")


// Set the default list of issues to null just to be safe
List<Issue> issues = null;

// Perform the search as a user and return the result so it can be validated.
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);



if(parseResult.isValid()){
log.debug("inside parse if")
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
issues = searchResult.issues
log.debug(issues)
issues.each(){
log.debug("inside loop")
def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Total Remaining Estimate")
def cFieldValue = (double) issues.getCustomFieldValue(estField)
log.debug(cFieldValue)
if ( cFieldValue == 0.0) {
def subject = "${issues.key} has no Estimate"
def body = "https://test.atlassian.net/issue/${issues.key} \n This issue does not have an estimate"
def emailAddr = "bot@test.com"
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

if (mailServer) {
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setBody(body.toString());
mailServer.send(email);
log.debug("Mail sent")
}
else {
log.debug("Please make sure that a valid mailServer is configured")
}
}
}
}
else{
log.error("Invalid JQL: " + jqlSearch);
}


 

 Hope you are able to identify the issue a bit better now, Thanks @Alexey Matveev

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.
June 7, 2018

Thank you for the code. It should be like this:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import java.util.logging.Logger
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter


def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug("start of code")

jqlSearch = 'updated >= -4w'; // Create the JQL query to perform

SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
UserUtil userUtil = ComponentAccessor.getUserUtil();
def user = ComponentAccessor.getUserManager().getUserByName("jira_bot")


// Set the default list of issues to null just to be safe
List<Issue> issues = null;

// Perform the search as a user and return the result so it can be validated.
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch);



if(parseResult.isValid()){
log.debug("inside parse if")
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
issues = searchResult.issues
log.debug(issues)
issues.each(){ issue ->
log.debug("inside loop")
def estField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Total Remaining Estimate")
def cFieldValue = (double) issue.getCustomFieldValue(estField)
log.debug(cFieldValue)
if ( cFieldValue == 0.0) {
def subject = "${issue.key} has no Estimate"
def body = "https://test.atlassian.net/issue/${issue.key} \n This issue does not have an estimate"
def emailAddr = "bot@test.com"
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()

if (mailServer) {
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setBody(body.toString());
mailServer.send(email);
log.debug("Mail sent")
}
else {
log.debug("Please make sure that a valid mailServer is configured")
}
}
}
}
else{
log.error("Invalid JQL: " + jqlSearch);
}
Tyler Brown-Jones June 7, 2018

You're great, thanks for this it has got it working.

 

Could you explain what issue -> does within the loop please? just so i know for future reference.

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.
June 7, 2018

When you do a loop with the each statement you can reference each element of an array with the it variable. But I tend to rename this variable so, that it would be more understandable. That is why issue -> means that you will be using the issue variable instead of the it variable. If you remove the issue -> part, then you can change all issue. parts with the it. part.

Like Quinton Lephoto likes this
Tyler Brown-Jones June 7, 2018

Ahh, thanks for the great explanation. I will use this more in the future, thank you again @Alexey Matveev you have been a valid source of help and information these past days.

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.
June 7, 2018

@Tyler Brown-Jones you are welcome!

0 votes
Yogesh Mude
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.
June 7, 2018

HI @Tyler Brown-Jones

Try to use like this and check

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

 

CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(new Long(10266));

Issues.getCustomFieldValue(cf)

Tyler Brown-Jones June 7, 2018

that's exactly what i did apart i referenced by name as it works fine for immutableCustomFields , but why the (new Long(10266)) part in your code?

Suggest an answer

Log in or Sign up to answer