Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

script runner calculated field

Antonio_Caeiro March 11, 2020

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 12, 2020

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

Antonio_Caeiro March 13, 2020

Tks Peter.

this works fine

return value.toDouble() * 250

0 votes
Manuel Santiago Risquez April 30, 2020

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
}

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 30, 2020

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) 

TAGS
AUG Leaders

Atlassian Community Events