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

ScriptRunner - failed script setting a Group picker field from another custom field

Cary Watkins June 27, 2018

I am trying to use the below script to pull the value from one field and apply it to a single Group picker. I have seen this script in another discussion and it worked for them, but I am not sure why it is not working for me. I would like to add this script as a listener for whenever the ticket is updated. Is there any way to make the listener only trigger when a specific custom field is updated? Below is the script that is failing for me:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def assignGroupField = customFieldManager.getCustomFieldObjectByName("Test Group")

def assignedGroupValue = customFieldManager.getCustomFieldObject("customfield_10601")
def assignableGroup = groupManager.getGroup(assignedGroupValue) //jira group
issue.setCustomFieldValue(assignGroupField, assignableGroup)

In this instance, Custom Field 10601 is where the value is currently. This is an Insight Custom field. I need to take the text from that field, and apply it as a group in the field called "Test Group".

As part of this script, is there any way to add a component that manipulates the text before it adds it to Test Group? I was wondering if I could use the 'substringBefore' function to only utilize part of the text?

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Jenna Davis
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.
July 5, 2018

Hello Cary!

I do see a few problems in your script. First, since you're trying to use a listener you have to access the value a bit different that in other types of scripts by using 'event.issue'. Also, you'll need to update the custom field value in a different manner than what you're doing. I've written out an example script that should work, though I haven't tested this using an Insight custom field. If you have problems due to the Insight field I'd recommend adding some logging statements to the script to see exactly what 'asssinedGroupValue' is getting set to to see if you need to gather the value differently. 

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 issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def assignGroupField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Test Group"}
def assignedGroupValue = customFieldManager.getCustomFieldObject("customfield_10601")
def changeHolder = new DefaultIssueChangeHolder()
assignGroupField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(assignGroupField), assignedGroupValue),changeHolder)

Let me know your results or if you have any other questions. :)

Regards, 

Jenna 

Cary Watkins July 5, 2018

Hi Jenna, 

 

Thank you for the response. I am getting the below errors when I run the listener. Thoughts?

 

2018-07-05 18:39:05,832 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-07-05 18:39:05,832 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.ClassCastException: com.atlassian.jira.issue.fields.ImmutableCustomField cannot be cast to java.util.Collection
 at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script28.run(Script28.groovy:12)
Jenna Davis
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.
July 5, 2018

Like I said, I haven't tried this out with an Insight custom field - it's possible that is causing a problem here. Could you try adding in some logging around that field just to see what you're returning from it?

Something like

log.debug("assignedGroupValue: ${assignedGroupValue}")

should work. You can check the listener's execution information (under the 'logs' tab) to see the output after rerunning the script. 

Jenna

Cary Watkins July 5, 2018

Hi Jenna,

 

I added that line item to my script but did not see anything new in the log. Maybe I am doing something wrong? 

If it makes any difference, I replaced the Insight Custom Field (10601) with a text field (10602) but received the same error message. Maybe its not related to retrieving the value but when applying it? Below is the latest, please let me know if I missed anything to make the log more useful - apologies I am very new to this and still learning basics.

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
import org.apache.log4j.Level
import org.apache.log4j.Logger


def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def assignGroupField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Test Group"}
def assignedGroupValue = customFieldManager.getCustomFieldObject("customfield_10602")
def changeHolder = new DefaultIssueChangeHolder()

log.debug("assignedGroupValue: ${assignedGroupValue}")
assignGroupField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(assignGroupField), assignedGroupValue),changeHolder)
Jenna Davis
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.
July 5, 2018

I read back over my code and realized that I missed a method that might've caused the original error, though I'm still not certain about grabbing values from that field. Try out this modified version and let me know you results:

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
import org.apache.log4j.Level
import org.apache.log4j.Logger


def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def assignGroupField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Test Group"}
def assignedGroupValue = customFieldManager.getCustomFieldObject("customfield_10602").getValue(issue)
log.debug("assignedGroupValue: ${assignedGroupValue}")
def changeHolder = new DefaultIssueChangeHolder()

assignGroupField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(assignGroupField), assignedGroupValue),changeHolder)
Cary Watkins July 5, 2018

Looks like the error is the same as before. 10602 is a text field so hopefully there is no issue pulling its value. 

2018-07-05 20:44:27,655 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-07-05 20:44:27,655 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
 at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script41.run(Script41.groovy:16)
Jenna Davis
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.
July 5, 2018

I'm not sure why the value of the field isn't logging out on your end, but I was able to suss out the cause of this error. The group field take a Collection of objects as opposed to a single object, so my script was previously passing the data incorrectly. I've tested this out with default JIRA fields, if you're unable to get it to work on Insight fields I'd recommend reading through their docs for some information on how to pull an Insight field's value. 

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def assignGroupField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "test"}
def assignedGroupValue = customFieldManager.getCustomFieldObject("customfield_10601").getValue(issue) as String
Collection assignableGroup = [groupManager.getGroup(assignedGroupValue)]
def changeHolder = new DefaultIssueChangeHolder()

assignGroupField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(assignGroupField), assignableGroup),changeHolder)

 Jenna

Like Houha Amdouni likes this
Cary Watkins July 6, 2018

Jenna,

I applied the changes you made and it seems to be working as expected. Thank you very much for the long efforts; I will make your latest as the answer to this.

 

I did have just one follow up question as it relates to what I have. I ran into the issue with it not applying the group but that was my own fault because the group didn't exist. Long story short - Once you get the value of the CF as a string, is there any way to edit that string using something like the StringUtils I see here? Ideally I would like to use the 'substringBefore' function to trim it down. my example below:

 

Current String: Group Value (ID:1)

Ideal value: Group Value

I've been trying to research it myself all morning to no result...

Jenna Davis
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.
July 6, 2018

I'm glad it's working now :) Sorry for taking a bit to get a proper solution to you. 

You definitely can do that, in fact you could probably just get away with using groovy string operations instead of StringUtils. 

As long as that is the exact format current string (parenthesis included) you could do something like: 

def currentString = "Group Value (ID:1)"
def subString = currentString.substring(0,currentString.indexOf('(')-1) // -1 to remove the trailing space
//subString should equal 'Group Value'

You might be able to make it more elegant, but it should work for what you described. :)

Jenna

TAGS
AUG Leaders

Atlassian Community Events