Scriptrunner: Getting information from multiple tickets and using mathematical logarithms.

Andrew Lonardelli July 4, 2017

Hi everyone,

My code can be seen below and it is trying to take data from two different projects and two different issuetypes and changing a field accordingly. The jqlInfo and issuesInfo should give me a list of just one ticket. This is a scritped field and I am calling from that current issue ticket and another ticket. I get an error saying <

Cannot cast object 'null' with class 'null' to class 'float'. Try 'java.lang.Float' instead
	at Script621.run(Script621.groovy:49)

>

I believe that my error lies in this line but I can be mistaken:

float accrual = issuesInfo.get(s).getCustomFieldValue(cfaccrual) as float?:0

import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.MutableIssue import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.user.ApplicationUser; import com.atlassian.jira.bc.issue.search.SearchService; import com.atlassian.jira.web.bean.PagerFilter; import java.lang.Object boolean check =true; String jqlVaca = 'project = "CENGN Leave" AND issuetype = "Vacation Request" AND reporter = currentUser() AND ((status != Dismiss) OR (status != Rejected))' String jqlInfo = 'project = "HR" AND issuetype = "Employee Information" AND "Employee Name" = currentUser()' // Get a search-Object for JIRA SearchService searchService = ComponentAccessor.getComponent(SearchService.class) def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def issuesVaca; def issuesInfo; SearchService.ParseResult parseResultVaca = searchService.parseQuery(user, jqlVaca) SearchService.ParseResult parseResultInfo = searchService.parseQuery(user, jqlInfo) if (parseResultVaca.isValid()) { issuesVaca = (searchService.search(user, parseResultVaca.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues()) } else check =1; if(parseResultInfo.isValid()){ issuesInfo = (searchService.search(user, parseResultInfo.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues()) } else check =1; // Get Information about Accrual Rate and Past Vacation Days used def requestedDays = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Requested Days") float daySum =0; if((issuesVaca.size()>=0)&&(check!=1)){ for(int i =0; i <= issuesVaca.size()-1 ; i++){ float dayCounter = issuesVaca.get(i).getCustomFieldValue(requestedDays) as float?:0 daySum = dayCounter + daySum } } def cfaccrual = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Accrual Rate") int s = issuesInfo.size()-1 //the line below is likely the one giving me errors float accrual = issuesInfo.get(s).getCustomFieldValue(cfaccrual) as float?:0 Date today = new Date() float month = today.getMonth() if(accrual == 0) return 0 else if (((today.getMonth() * accrual) - daySum) <= 0) return 0 else{ return ((today.getMonth() * accrual) - daySum) }  

 

 

1 answer

1 accepted

2 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
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 5, 2017

The object Float can be set to null to represent a value that is unknown.

The primitive float is guaranteed to have a value.

I think that's why the error is telling you to use java.lang.Float. Perhaps try writing:

Float accrual = issuesInfo.get(s).getCustomFieldValue(cfaccrual) as Float?:0

 

Andrew Lonardelli July 5, 2017

I tried it and I still get the same error. Does it matter if the customfield is a number field custom field that stores a (floating point input)?

Andrew Lonardelli July 5, 2017

I just changed all the float to Float and the code works fine. Thanks alot Joshua

Joshua Yamdogo @ Adaptavist
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 5, 2017

I can reproduce the error you're having if I use your original code using the primitive float, which makes sense because if the custom field is empty (null), you won't be able to cast it. It works for me using the object Float.

You should definitely use the number custom field. Your script will work with a regular text custom field IF you trust your users to never enter anything other than a number. Changing the field to a number field will always force the user to enter something valid (i.e. numeric only), which is what I would do.

Changing your field to a number field would also remove the need to cast the custom field value. So I think you could just write this:

 

float accrual = issuesInfo.get(s).getCustomFieldValue(cfaccrual)?:0

If the value of the custom field is null, it'll be coereced to 0. Else, get the floating point value. From what I've tested, this seems to work, but you might get an erroneous static type checking error if you're using the inline editor.

 

Suggest an answer

Log in or Sign up to answer