Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,362,152
Community Members
 
Community Events
168
Community Groups

script runner calculated field

Edited

Hi all,

Does anyone can help me. I'm quite new in scripting...

I need to get a value from a calculated field (number field) and multiply it by 250

 

I'm using this for a field with the value "5":

import com.atlassian.jira.component.ComponentAccessor;
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10102");
def value = (String)issue.getCustomFieldValue(customField);

return value * 250

 

But the result is:

5.05

 

tks for any help

 

2 answers

1 accepted

0 votes
Answer accepted

Try to convert your value to a double before making math operation:

return value.toDouble() * 250

Or get it as a double in the first place

def value = (Double)issue.getCustomFieldValue(customField);

Tks Peter.

this works fine

return value.toDouble() * 250

Buenas Tardes, 

alguien me puede ayudar?.

Estaba creando un script para ponerlo como condicion en una transaccion, consiste en que si el customfield (Fecha), es menor que el dia 25 del mes actual me deje validar la issue sino no, el problema es que lo meto en conditions se me ejecuta pero no me hace nada.

 

Script: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.User
import java.sql.Timestamp
import com.atlassian.jira.issue.MutableIssue
import java.util.Date
import java.text.SimpleDateFormat
import java.text.DateFormat


//MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('MANI-8')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManafer = ComponentAccessor.getIssueManager()
def userManager = (UserManager) ComponentAccessor.getUserManager()

CustomField cfFecha = customFieldManager.getCustomFieldObjectByName("Fecha")

def valorFecha = (String)issue.getCustomFieldValue(cfFecha)
def diaCustomField=valorFecha.substring(8,10)
def mesCustomField=valorFecha.substring(5,7)

def currentDate = new Date ()
def fechaActual=currentDate.dateString
def mesActual= fechaActual.substring(3,5)
def diaActual=currentDate.date.toString()

if (diaActual <= '25' && diaCustomField <= '25' && mesCustomField==mesActual || diaCustomField >= '25' && mesCustomField != mesActual ) {
return true
}
else {
return false
}

While it may work, I don't think it's a good idea to use<= on a string value, I'd recommend you work with integers.

You don't need to convert your date to sting then parse it with substring, just use the built-in date object methods like getDate() and getMonth()

For example

def currentDate = new Date().minus(10)
def valorFecha = issue.getCustomFieldValue(cfFecha)

return (currentDate.date <= 25 && valorFecha.date <= 25 && currentDate.month == valorFecha.month || valorFecha.date >=25 && currentDate.month != valorFecha.month)

Here are all the different permutation with example dates for the logic statement:

today=30Apr, Cf = 30Apr > false (same month /no date<25)
today=30Apr, Cf = 15Apr > false (same month /cf date<25)
today=15Apr, Cf = 30Apr > false (same month /today date<25)
today=15Apr, Cf = 15Apr > true  (same month /both dates<25)

today=30Apr, Cf = 15May > false  (different month /cf date<25)
today=15Apr, Cf = 30Apr > true (different month /cf date !<25)
today=15Apr, Cf = 15May > false (different month /both dates <25) 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events