How to change customField value with Label type

I'm trying to change customFiled with type Label in groovy scrip to put it after in post-function in Jira.

How to implement this solution ?

>>>>>>>>>>>>>

ComponentManager componentManager = ComponentManager.getInstance()

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

CustomField customFieldTarget = customFieldManager.getCustomFieldObject("customfield_10005");

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()

customFieldTarget.updateValue(null,issue, new ModifiedValue(issue.getCustomFieldValue(customFieldTarget),"TestLabel"),changeHolder)

>>>>>>>>>>>>>>>>>>

And i recived error like this:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Set

at com.atlassian.jira.issue.customfields.impl.LabelsCFType.createValue(LabelsCFType.java:70)

at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:733)

at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:421)

at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:403)

at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)

at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)

at Script54.run(Script54.groovy:22)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEn

gineImpl.java:315)

... 141 more

3 answers

0 votes

Try passing a set if that's what it wants, eg:

["TestLabel"] as Set

instead of

"TestLabel"

Not helped :-(

Looks like this new label should be created before with appropriate type "LabelsCFType". Any other sugestions ?

Exception is following:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.

atlassian.jira.issue.label.Label

at com.atlassian.jira.issue.customfields.impl.LabelsCFType.setLabels(Lab

elsCFType.java:251)

at com.atlassian.jira.issue.customfields.impl.LabelsCFType.createValue(L

abelsCFType.java:237)

at com.atlassian.jira.issue.customfields.impl.LabelsCFType.createValue(L

abelsCFType.java:70)

at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFie

ldImpl.java:733)

at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFie

ldImpl.java:421)

at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFie

ldImpl.java:403)

at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unkno

wn Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSi

teArray.java:42)

at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unkno

wn Source)

at Script49.run(Script49.groovy:21)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEn

gineImpl.java:315)

... 141 more

try with this code

if (cfType instanceof LabelsCFType) {
     Set<String> set = convertToSetForLabels((String) newValue);
      this.labelManager.setLabels(currentUser,issue.getId(),customField.getIdAsLong(),set,false,true);
  }
				
	 private Set<String> convertToSetForLabels(String newValue) {
        Set<String> set = new HashSet<String>();
        StringTokenizer st = new StringTokenizer(newValue," ");
        while(st.hasMoreTokens()) {
            set.add(st.nextToken());
        }
        return set;
    }

what is the labelManager ?

>>>>>>>>>>

Caused by: groovy.lang.MissingPropertyException: No such property: labelManager

for class: Script56

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptByteco

deAdapter.java:50)

at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(

PogoGetPropertySite.java:49)

import com.atlassian.jira.issue.label.LabelManager;

//declare this variable
 private final LabelManager labelManager;
//initialize using constructor
classname( LabelManager labelManager){
 this.labelManager = labelManager;
}

Not helped ...

This is my full code

import com.atlassian.crowd.embedded.api.User

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.comments.CommentManager

import com.atlassian.jira.issue.label.Label

import com.opensymphony.workflow.WorkflowContext

import com.atlassian.jira.issue.CustomFieldManager

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

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.util.IssueChangeHolder

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.label.LabelManager;

ComponentManager componentManager = ComponentManager.getInstance()

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

CustomField customFieldClient = customFieldManager.getCustomFieldObject("customfield_10100")

CustomField customFieldExecution = customFieldManager.getCustomFieldObject("customfield_10103")

LabelManager labelManager = new LabelManager() {

@Override

Set<Label> getLabels(Long aLong) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Set<Label> setLabels(User user, Long aLong, Set<String> strings, boolean b, boolean b1) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Set<Label> getLabels(Long aLong, Long aLong1) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Set<Label> setLabels(User user, Long aLong, Long aLong1, Set<String> strings, boolean b, boolean b1) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Label addLabel(User user, Long aLong, String s, boolean b) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Label addLabel(User user, Long aLong, Long aLong1, String s, boolean b) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Set<Long> removeLabelsForCustomField(Long aLong) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Set<String> getSuggestedLabels(User user, Long aLong, String s) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

@Override

Set<String> getSuggestedLabels(User user, Long aLong, Long aLong1, String s) {

return null //To change body of implemented methods use File | Settings | File Templates.

}

}

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();

commmgr = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class)

String color = new String()

if (issue.getStatusObject().getName().toString().equals("Closed"))

{

color = "{color:blue}"

} else

{

color = "{color:red}"

}

//if (cfType instanceof com.atlassian.jira.issue.customfields.impl.LabelsCFType) {

Set<String> set = convertToSetForLabels((String) "testlabel");

labelManager.setLabels(currentUser,issue.getId(),customFieldExecution.getIdAsLong(),set,false,true);

//}

private Set<String> convertToSetForLabels(String newValue) {

Set<String> set = new HashSet<String>();

StringTokenizer st = new StringTokenizer(newValue," ");

while(st.hasMoreTokens()) {

set.add(st.nextToken());

}

return set;

}

//customFieldExecution.updateValue(null,issue, new ModifiedValue(issue.getCustomFieldValue(customFieldExecution),color+issue.getAffectedVersions().toString()+"["+customFieldClient.getValue(issue)+"]["+issue.getStatusObject().getName().toString()+"]{color}"),changeHolder)

commmgr.create(issue, currentUser, "||Version||Clients||Execution Status||"+"\n"+"|"+issue.getAffectedVersions().toString()+"|"+customFieldClient.getValue(issue)+"|"+issue.getStatusObject().getName().toString()+"|", true)

issue.store()

And this is the error:

>>>>>>>>>>>>>>>>>

Caused by: groovy.lang.MissingMethodException: No signature of method: org.codeh

aus.groovy.jsr223.GroovyScriptEngineImpl.setLabels() is applicable for argument

types: (java.lang.String, java.lang.Long, java.lang.Long, java.util.HashSet, jav

a.lang.Boolean, java.lang.Boolean) values: [admin, 10100, 10103, [testlabel], fa

lse, ...]

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.callGlobal(GroovySc

riptEngineImpl.java:389)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.access$000(GroovySc

riptEngineImpl.java:72)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$2.invokeMethod(Groo

vyScriptEngineImpl.java:301)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl$2.invokeMethod(Groo

vyScriptEngineImpl.java:290)

at groovy.lang.GroovyObjectSupport.invokeMethod(GroovyObjectSupport.java

:44)

at groovy.lang.Script.invokeMethod(Script.java:78)

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurre

ntN(ScriptBytecodeAdapter.java:85)

at Script62.this$dist$invoke$3(Script62.groovy)

at Script62$1.methodMissing(Script62.groovy)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.

java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces

sorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:

90)

at groovy.lang.MetaClassImpl.invokeMissingMethod(MetaClassImpl.java:811)

at groovy.lang.MetaClassImpl.invokePropertyOrMissing(MetaClassImpl.java:

1103)

at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1056)

at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:884)

at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaC

lassSite.java:39)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSi

teArray.java:42)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCa

llSite.java:108)

at Script62.run(Script62.groovy:80)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEn

gineImpl.java:315)

... 141 more

Rambanam's answer is for a plugin, for a script get a labelManager like:

def labelManager = ComponentAccessor.getComponent(LabelManager.class)

You will get better help if you post less text, and use the formatting tools for the code and stack trace.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,760 views 11 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot