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

werner kolze June 6, 2015

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 votes
Answer accepted
JamieA
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.
June 7, 2015

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

Jozef Kotlár
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.
June 7, 2015

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

JamieA
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.
June 8, 2015

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

1 vote
Jozef Kotlár
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.
June 7, 2015

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.

0 votes
werner kolze June 9, 2015

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
TAGS
AUG Leaders

Atlassian Community Events