Basic method to update a custom field with Scriptrunner

Hi,

Almost embarrassed to ask this! I am trying to update the displayed value of a simple one line text custom field.

I have read many example online but can't figure it out. I'm using script console, so my issue context is hard-coded:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("GEO-6")
def String myval = "my new text"
def textCf2 =customFieldManager.getCustomFieldObjectByName("hidden_start")
issue.setCustomFieldValue(textCf2, myval)

I am certain the field name is correct. The script seems to execute fine, and shows no errors in log after it runs, however the value of my custom field never actually updates when I go back and view my issue. What am I doing wrong?

(JIRA 7.1.4 & Scriptrunner 4.2.0.7)

Any help appreciated

1 answer

1 accepted

Found another way that works:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("GEO-6")
def String myval = "my new text"
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def textCf2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "hidden_start"}
if (textCf2) {
    def changeHolder = new DefaultIssueChangeHolder()
    textCf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCf2), myval),changeHolder)
}

 

My initial effort was following a guide I found here:

Scriptrunner site

Maybe it should be updated as it doesn't seem to be accurate?

Hi Paddy

Glad that you find the solution. Just a correction, the script you were looking was for a post function. And actually there is a message:

You can only use this method to update issue attributes if your script post-function comes before the standard functionUpdate change history for an issue and store the issue in the database. If you need your function to run after that, you have to use a more complex method.

Which means if you try to run it via script console you will need somehow to store the issue in the database, and this is what the 

textCf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCf2), myval),changeHolder)

does.

Kind regards

Thank you for clearing that up for me, I did not read the instructions properly!

I understand now I was effectively updating the field but not saving it. I appreciate the explanation

Hi, how can I use to update with a number?

 

regards

Hi Artur - I'm guessing the field you are trying to update can is some kind of text field?

If so, the value you are trying to enter into the target field can be cast to a string with something like

def String myval = MyResultAsNumber.toString()

and then it should work. Or change the target field to number compatible type..

I want to get the value that I am bringing from the variable "RESULT.AVERAGE_SEC" to display it in customfield.

Id of my customField: 11300

Part of my code:

def result = sql.firstRow(s)
log.warn "${result.AVERAGE_SEC}"

def customField = result.AVERAGE_SEC
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(11300);
if(customField != null) {
issue.getCustomFieldValue(customField);
}

 but is something wrong because I need to show the value of RESULT.AVERAGE_SEC into my custom field

Hi - you have defined customField twice and seem to only get its value, not write to it.

Did my best to merge the snippet you gave with code I know works:

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def TargetCF1 = customFieldManager.getCustomFieldObject("customfield_11300")
def result = sql.firstRow(s)
log.warn "${result.AVERAGE_SEC}"
def String myval = result.AVERAGE_SEC.toString()
if (TargetCF1) {
    def changeHolder = new DefaultIssueChangeHolder()
    TargetCF1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(TargetCF1), myval),changeHolder)
}

I don't really understand where "result.AVERAGE_SEC" comes from, but if it is writing a value to the log in your log.warn statement, then it should get written to your CF

Paddy Byrne, it worked for me, but I made some changes

 

def avgValue = ""
def result = sql.firstRow(s) avgValue = "${result.AVERAGE_SEC}" log.warn avgValue

issueInputParameters.with {projectId = projectManager.getProjectObjByKey(key[0]).idsummary = summaryCurrent + " ~ ISR"reporterId = user.name issueTypeId = tipo setDueDate(dateValue)assigneeId = userOm setPriorityId(issueCurrent.getPriorityObject().getId())addCustomFieldValue(11300, avgValue) }
 
11300 is my custom field.
Thank you

 

 

Hi @Paddy Byrne and @Thanos Batagiannis [Adaptavist],

I have been running into issues regarding getting custom fields to update with ScriptRunner.

I came accross your post and tried out your code with no success.

I am trying to update the value of a custom field of type checkbox. My code runs within a Behaviour.

Code:

//utilized same code as you have to grab the customField manager as well as the issue which I name documentIssue

def String myval = "Yes"
def textCf2 = customFieldManager.getCustomFieldObjects(documentIssue).find {it.name == "DEV Reviewd"}

if (textCf2) {
def changeHolder = new DefaultIssueChangeHolder()
textCf2.updateValue(null, documentIssue, new ModifiedValue(documentIssue.getCustomFieldValue(textCf2), myval),changeHolder)
} else log.debug( "Cannot update the value :( " )

 

I fail to understand why it is not working, perhaps its because it is of type checkbox? I have tried many other articles but have not found anyway that works for me.

I would appreciate if you could provide some guidance on this :).

-Roberto

Hi Roberto,

You mentioned that you are trying to set a value to a checkbox in a behaviour right ?

Then please take a look at this example in the documentation - Setting Defaults for Selects etc 

The example code in this thread is for updating the value of a custom field in a post function or via the script console. 

Hi @Thanos Batagiannis [Adaptavist],

Thank you for that link, it has helped me in figuering out the answer to my issue!

Thank you!

-Roberto

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published 4 hours ago in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

26 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you