Groovy script sets customfield from console, but not when run from a workflow

Hi - 

I have a customfield called "Responsible Group" - a user picker field

also a customfield call "Additional Approvers" - cf 16011 - defined as "Multi Select Checkbox" 

the use case is:

when a user selects a group in the "Responsible Group" this group should not be repeated / selected in the "Additional Approvers" field -
for example: if the user makes a "mistake" and includes this same group in the "Additional Approvers" 

then this group should be removed from the "Additional Approvers" customfield.

 


I placed the code below - I understand there are a few too many imports done, but I am still debugging and fairly new to all this, sorry.

anyway, the mystery is that the code works when I run it from the Script console fine - when I run it in the console I comment out the line which defines the "issue" and uncomment the line with the "Mutable issue..." 

when I apply it in the Workflow and update a field in a post-function then I get this error in the logfile, yes the workflow has been published.

this is the corresponding error log entry

Error while executing SetFieldValueFunction: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.innovalog.jmwe.IssueProxy@1218a803' with class 'com.innovalog.jmwe.IssueProxy' to class 'com.atlassian.jira.issue.Issue'
org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object 'com.innovalog.jmwe.IssueProxy@1218a803' with class 'com.innovalog.jmwe.IssueProxy' to class 'com.atlassian.jira.issue.Issue'

 

When I research this, it tells me that I dont have the 'correct handle on the issue' -  
so I have an error here: ??

def originalIssue = issueManager.getIssueObject(issue.issueObject.key) 

 

thanks for all your contributions/ help and tips

'

package examples.faq
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.util.ImportUtils
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

// comment it out to test 
Issue issue = issue
//
//comment this out to test on specific issue
//MutableIssue issue = componentManager.getIssueManager().getIssueObject("ICM-285")
// the source value is the array/list which has been set by the user 
// these are the additional approvers 
sourceValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_16011"))
//return sourceValue
//set the cfg variable to the responsible group selected by the user
// this is  a custom field called "Responsible Group"
def cfg = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Responsible Group"};
def cfgtestvalue = issue.getCustomFieldValue(cfg);
//variable for the actual group value set in issue
def aname 
    
for(Group group:cfgtestvalue)
{
  if (group.getName() !="")
  {
   aname =  group.getName().toString()
     //print what you've got 
     // return aname
  }    
}
if (sourceValue==null) return null;
newValue = []
for (com.atlassian.jira.issue.customfields.option.Option option : sourceValue) {
  if (!option.getValue().equals(aname))
    newValue.add(option);
}
//return newValue;

CustomField target = customFieldManager.getCustomFieldObject("customfield_16011");
 def originalIssue = issueManager.getIssueObject(issue.issueObject.key)  
originalIssue.setCustomFieldValue(target, newValue)

3 answers

1 accepted

0 vote

> You should use issueObject instead of issue. See JMWE documentation

Yes... if you want to use JMWE. If you want to use ScriptRunner then make sure it's the script runner post-function... I think it's called "script function", then choose the one called "custom script", then either point to the file containing the above or paste that in.

Of course - there are many options. I've thought it is JMWE only question at the beginning. By the way, I always miss clearer distinction of add-on functionality - I think it would be helpful to display add-on/vendor when selecting/showing condition/validator/post function in administration

Now there's such a proliferation of plugins, oftentimes with overlapping functionality, I think that's a good idea.

You should use issueObject instead of issue. See JMWE documentation.

It seems to me, taht you are mixing functionalities of Script Runner (console) and JMWE (postfunction). They are not compatible.

I decided on the Script Runner option and got it to work - thanks a lot guys!

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 Thursday 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...

182 views 0 5
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