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

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

Accepted Answer
2 votes

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

 

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)?

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

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
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

26,945 views 2 7
Join discussion

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