How to round up the return value in the script

neeta jain September 7, 2020

Hi Team,

 

Wanted to return no of weeks in the field through script.

Script is working fine and giving an output but not in the rounded form.

e.g. no of weeks should count as = 4 weeks but coming up as 4.129.

How to get the round up value from the script,

 

Kindly suggest.

Regards,

Neeta Jain

1 answer

1 accepted

0 votes
Answer accepted
Gustavo Félix
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.
September 7, 2020

Hi @neeta jain 
are you talking about a groovy script?

neeta jain September 7, 2020

I am using scriptrunner to implement it in JIRA , below is my script:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_18880');

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_12110');

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2= issue.getCustomFieldValue(dateFieldObject2) as Date

def diffdate = dateValue - dateValue2

def noofweeks = diffdate/7

return noofweeks

}

 

so output of noofweeks comes as 4.429 where I need to round up to 4.

How do I do that?

Gustavo Félix
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.
September 8, 2020

Hi @neeta jain 
double noofweeks = diffdate/7
double roundNoOfWeeks = Math.round(noofweeks)

That should round to 4.

neeta jain September 8, 2020

Thanks Gustavo for your help.

Thank you so much again, it worked.

Gustavo Félix
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.
September 9, 2020

Your welcome.
Can you put the answer as accepted? That way people could see the question was answered.

neeta jain September 10, 2020

Accepted the answer.

One small query I have.

There are certain issues already available and script is written later on.

So Do we get the the script field for exiting issues as well?

Gustavo Félix
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.
September 10, 2020

I did a test adding an scripted field to my project and all the issues created before the scripted field had a value. 

So , I'm guessing the moment you add your scripfield to your screen the issues created before get the value.

neeta jain September 11, 2020

Hi,

 

You are right, I can also see the scripted field for all existing issues.

Thanks again for your help.

 

Regards,

Neeta Jain

Like Gustavo Félix likes this
neeta jain September 25, 2020

Hi Gustavo,

The script is working fine and output is coming as expected.

But when I configure this field to easyBI plugin, at the time of importing issues, it gives an error where Number Of weeks(Calculated)=0.

Do we need to declare the field as different type, below is the script for your reference.

Kindly suggest the answer:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_18880');

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_12110');

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2= issue.getCustomFieldValue(dateFieldObject2) as Date

def diffdate = dateValue - dateValue2

double noofweeks = diffdate/7

double roundNoOfWeeks = Math.round(noofweeks)

return roundNoOfWeeks


}

Gustavo Félix
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.
September 25, 2020
Gerda Grantiņa _ eazyBI
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.
October 12, 2020

Hi @neeta jain ,

to import your field in eazyBI, you need to add this code to eazyBI advanced settings or ask Jira admin to do it (as only they have access to eazyBI advanced settings). 

This code will allow eazyBI to recognize this customfield as a measure, further you need to select the custom field for import in eazyBI source data import.

[jira.customfield_NNNNN]
data_type = "integer"
dimension = true

More examples on how to import Scripted fields you will find in the link @Gustavo Félix shared. 

best,

Gerda // support@eazyBI.com

Suggest an answer

Log in or Sign up to answer