Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Scrips not working in cloud Instance - Script runner

Ganesh Choubey D
Contributor
January 10, 2024

Hello Community,

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import static java.lang.Math.*
import java.sql.Timestamp
def today = new java.sql.Timestamp(new Date().getTime())
def issue = issue as MutableIssue


def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(""In Analysis Time"")
def cfFuncDate = issue.getCustomFieldValue(cf)
issue.setCustomFieldValue(cf,today)
log.error""==========""+cfFuncDate"

----------------------------------------------------------------------------------------------------
"import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

DefaultIssueChangeHolder changeholder = new DefaultIssueChangeHolder();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(""Back to In DEV Count"")
def value = issue.getCustomFieldValue(cf)

def currValue=(Double)cf.getValue(issue)
log.error""-------currval=""+currValue
def newValue;
if (currValue== null)
{
newValue= 1.0D;
}
else{
newValue=currValue+1.0D
}
cf.updateValue(null, issue, new ModifiedValue(currValue,newValue),changeholder);
log.error""---------count=""+newValue"

==================================================================================================================

We have been using the above scripts in our Server Instance in Jira but due to the groovy script not working in the cloud Instance we need to write a fresh scripts with different methods and syntaxs(beacuse the cloud uses API calls) can anyone help me with the scripts for cloud 

 

Thanks & Regards,

Ganesh

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 12, 2024

Hi @Ganesh Choubey D

The previous code you used on the Jira Server Instance does not apply to the Cloud because you must make REST requests to update the field value.

I am going to assume you are doing the update via Post-Funtion.

You will need to use ScriptRunner's Run Script Post-Function for this. Below are the steps to configure the Post-Function.

1. Go to the transition you want to include the Post-Function and click on the Add post function link as shown in the screenshot below:-

image1.png

 

2. Select the ScriptRunner Post-Function option as shown in the screenshot below:-image2.png

 

3. Next, Select the Run Script Post-Function link as shown in the screenshot below:- image3.png

Please refer to this ScriptRunner for Jira Cloud code snippet.

Below is the working code snippet with sample values for your reference:-

def issueKey = issue.key
def numberFieldId = 'customfield_10049' //Number Field Id
def autoIncrement = 10 //Specify the amount to increment

if (issue.fields[numberFieldId]) {
def currentValue = issue.fields[numberFieldId] as Long
def updatedValue = (currentValue + autoIncrement) as Long

put("/rest/api/2/issue/${issueKey}").header('Content-Type', 'application/json').body([
fields: [
//Number Field Id
customfield_10049: updatedValue
]
]).asString()

} else {

put("/rest/api/2/issue/${issueKey}").header('Content-Type', 'application/json').body([
fields: [
//Number Field Id
customfield_10049: autoIncrement
]
]).asString()
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

You will need to include the code in the Additional Condition section of the Post-Function, as shown in the screenshot below:-

post_function.png

Below are a couple of test screenshots for your reference:-

1. For the first test, I am using an issue with a value to the Sampleinumber Field. In this example, the value of the Sample Number Field has been set to 120 and is currently in the To Do status, as shown in the screenshot below. 

test1.png

 

2. Next, I transition the issue to In Progress status. As expected, the value of the Sample Number field has been incremented by 10 and updated to 130, as shown in the screenshot below:- 

test2.png

3. Next, I will run a test for an issue where no value has been set to the Sample Number field. At the moment, the issue is in To Do status as shown in the screenshot below:-

test3.png

 

4. Once I transition the issue to In Progress status, as expected, the value for the Sample Number has been incremented by 10 and updated as shown in the screenshot below:-test4.png

Please note that you may need to refresh your page a couple of times to see the effect, as there might be a delay in the Cloud instance displaying the update.

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events