Forums

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

Updating custom fields with scriptrunner - can't see where I'm going wrong...

Graham.Wilson
Contributor
May 23, 2019

Hi.  Firstly, apologies if this is a bit basic - hopefully that'll translate into an easy answer!

 

I have a workflow set up to update a custom field to "No" whenever a certain status is transitioned to - that's performed via Scriptrunner post function (script is at the bottom of this post). Put simply, it doesn't work.

 

I have verrrrrrrrry little scripting experience so I've put this together from various sources.  I've likewise looked at a couple of solutions already posted here but these haven't worked, which makes me think I'm missing something fundamental.  

 

The custom field is called "RTS Buddying Complete?" and is a "Select list - single choice" type. The only choices are "No" which is the default, and "Yes."

 

When I transition to the appropriate status, I get a "Workflow Error" popup saying, "Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB java.lang.String cannot be cast to com.atlassian.jira.issue.customfields.option.Option

It seems that you have tried to perform an illegal workflow operation."

 

NB I can't open that link so I'm a bit stuck.

I've seen elsewhere that it might be because how I'm trying to update the field is incompatible with the select list, but - unless I'm missing something - I can't see what I need to do to get it to work.

 

Anyway, this is the script, which runs as a custom script post-function (inline script): I'm not even sure that I need the "Get custom field value" bit but I've included it here so you can see it in completion. It should be simple but it's really annoying me.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

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

 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

//get custom field object

def cfoBuddyComplete = customFieldManager.getCustomFieldObjectByName("RTS Buddying Complete?")

 

//get custom field value

def cfBuddyComplete = issue.getCustomFieldValue(cfoBuddyComplete)

 

//set value

issue.setCustomFieldValue(cfoBuddyComplete, 'No')

1 answer

1 accepted

2 votes
Answer accepted
Tom Lister
Community Champion
May 23, 2019

Hi @Graham.Wilson 

Is your post function running before the standard Update Change History function?

Tom Lister
Community Champion
May 23, 2019

for select lists try

def cf = customFieldManager.getCustomFieldObjectByName("customFieldId=10807")def cfConfig = cf.getRelevantConfig(issue)value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Training' }issue.setCustomFieldValue(cf, value)
Graham.Wilson
Contributor
May 23, 2019

Hi - thanks for replying.  Yes it is.

Graham.Wilson
Contributor
May 23, 2019

I'll give that a go and see what happens - thanks!

Graham.Wilson
Contributor
May 23, 2019

Hi.  Is that meant to be all one line? How does that fit into the script I've drawn up? Where you've put "training", what do I replace that with?

 

I know it's a big ask but could you put what you've done into the context of the script I've included, as I have no idea looking at that what I need to change either within your part or mine.   Like I said, a newbie!

Tom Lister
Community Champion
May 24, 2019

Hi

Try this. If it doesn't work I'll try and refine it on my instance

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
//get custom field object
//def cfoBuddyComplete = customFieldManager.getCustomFieldObjectByName("RTS Buddying Complete?")

def cf = customFieldManager.getCustomFieldObjectByName("RTS Buddying Complete?")
def cfConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'No' }
issue.setCustomFieldValue(cf, value)
Like Soley Gudmundsdottir likes this
Tom Lister
Community Champion
May 24, 2019

Hi @Graham.Wilson 

This snippet works on my instance - doesn't like multi-selects though in this form.

Like Graham.Wilson likes this
Graham.Wilson
Contributor
May 24, 2019

That works perfectly.  I can't thank you enough!

 

Very very much appreciated.

Suggest an answer

Log in or Sign up to answer