Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

need script for scripted field

neeta jain August 20, 2020

Hi Team,

working on the script for below requirement: 

IF (Risk Impact*Risk Probability=0)
Then Risk Level(Calculated) = 0

IF (Risk Impact*Risk Probability<4)
Then Risk Level(Calculated) = 1

IF (Risk Impact*Risk Probability<8)
Then Risk Level(Calculated) = 2

IF (IF(Risk Impact*Risk Probability<9)
Then Risk Level(Calculated ) = 3

 

Risk level (Calculated) is a scripted field created.

 

Kindly help me with the script to achieve this as I am new learner to the scripting.

 

Regards,

Neeta Jain

1 answer

0 votes
Filip Lukáč
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 20, 2020

You have to check both sides of the interval. because for now, everything will be Risk = 3 (you haven't checked lower.

IF (Risk Impact * RiskProbability < 4 AND Risk Impact RiskProbability > 0)

Then Risk Level (Calculated) = 1 

 

For instance, and don't know if I've used AND operator properly, 

neeta jain August 20, 2020

I need script syntax for above requirement

neeta jain August 21, 2020

I have tried below code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue

Issue mainIssue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def riskimpact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18484".getValue())) as String

def riskoccprobablity = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18483".getValue())) as String

def risklevel = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_23091".getValue())) as String

if (riskimpact * riskoccprobablity = 0)
{
return 0
}
else if (riskimpact * riskoccprobablity < 4 AND riskimpact * riskoccprobablity > 0 )
{
return 1
}
else if (riskimpact * riskoccprobablity < 8 AND riskimpact * riskoccprobablity >4 )
{
return 2
}
Else if (riskimpact * riskoccprobablity < 9 AND riskimpact * riskoccprobablity > 8 )
{
return 3
}

 

Kindly check and let me know any correction needed in it.

getting an error :

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script144.groovy: 16: expecting ')', found '=' @ line 16, column 36.
   if (riskimpact * riskoccprobablity = 0)
                                      ^

1 error


Nic Brough -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.
August 21, 2020

The error happens because your question in grammatically unclear - you've got two questions with potentially very different answers in it.

In English, this is one of the examples that absolutely proves the need for punctuations like the Oxford comma and braces.  Computers require that preciseness, they cannot infer the existence of them, the way humans can.

In English, your "if" question is stated as "if X times Y equals Z".  Grammatically though, there are two answers.  You and I can immediately see which one is intended, but a computer cannot because it doesn't speak English or understand the intent.

Your question could be clarified to be

X times (Y equals Z) 

Answer:  X when Y = Z and 0 when Y != Z

(X times Y) equals Z 

Answer:  True when ( X * Y ) = Z, otherwise false

So, try the precise grammar of the one you want:

if ( (riskimpact * riskoccprobablity) = 0)
neeta jain August 25, 2020

Hi tried abpove thing, see below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue

Issue mainIssue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def riskimpact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18484".getValue())) as String

def riskoccprobablity = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18483".getValue())) as String

def risklevel = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_23091".getValue())) as String

if ( (riskimpact * riskoccprobablity) = 0)
{
return 0
}
else if ( (riskimpact * riskoccprobablity) < 4)
{
return 1
}
else if ( (riskimpact * riskoccprobablity) < 8)
{
return 2
}
Else if ( (riskimpact * riskoccprobablity) < 9)
{
return 3
}

 

but still the same error:

 

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script59.groovy: 16: expecting ')', found '=' @ line 16, column 39.
   if ( (riskimpact * riskoccprobablity) = 0)
                                         ^

1 error

 

 

Kindly please check syntax of my script, as I am very new learner to it, finding difficulty to resolve it.

Nic Brough -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.
August 25, 2020

Sorry, that needs an equivalence, not a set operator.  try ...y) == 0 )

neeta jain August 26, 2020

== resolve the issue but now other error cames..

Please check my requirement:

I have dropdownlist custom fields for which I need calculation such as:

riskimpact*riskprobablity (both are Dropdown list)

and scripted field is number value.

so can you please let me know how do I declare them, as i am getting an error at the declaration itself.

thanks in advance

Regards,

Neeta Jain

Nic Brough -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.
August 26, 2020

You'll need to map the stored options in the drop-downs on to numerics.

neeta jain August 26, 2020

i have made the changes to the field at the declation:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.Issue

 

Issue mainIssue = issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def riskimpact = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18484".getValue())) as Integer

 

def riskoccprobablity = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_18483".getValue())) as Integer

 

def risklevel = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_23091".getValue())) as Integer

 

if ( ( riskimpact * riskoccprobablity) == 0)

{

    return risklevel == 0

}

else if  ( ( riskimpact * riskoccprobablity) < 4)

{

    return risklevel == 1

}

else if  ( ( riskimpact * riskoccprobablity)  < 8 )

{

    return risklevel == 2

}  

else

{

    return risklevel == 3

}

 

 

 

But getting error at the declarion:

Can not find matching methiod: java.lang.String#GetValue()please check if the declared type is correct.

 

Kindly suggest

Nic Brough -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.
August 26, 2020

You say the fields you are reading are select-lists.  The fields will contain "options" which you can't just cast to an integer because they are a complex object.  Imagine a list of animal species - you can't just say "what number is an elephant?"

You could try to get the name of the option as a string, and use parseInt oon it, but the safer way to do this (in case one of your admins renames or adds an option that it won't work for) is with an if or case statement.

Suggest an answer

Log in or Sign up to answer