Update customfields throw subtask

Hi guys,

I am trying to develop a groovy script for a post function but I am completely blocked and need your help.

The goal of my script is throw a subtask, update two customfields (checkbox) of the parent issue.

The update of my "customfield2" works well but I have this error message for for the second customfield ("customfield3" in the code) :

java.lang.ClassCastException: com.atlassian.jira.bc.project.component.ProjectComponentImpl cannot be cast to com.atlassian.jira.issue.customfields.option.Option
at com.atlassian.jira.issue.customfields.impl.MultiSelectCFType.convertTypeToDbValue(MultiSelectCFType.java:82)
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.convertTypesToDbObjects(AbstractMultiCFType.java:190)
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.updateValue(AbstractMultiCFType.java:142)
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.updateValue(AbstractMultiCFType.java:39)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:464)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:434)
at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
at Script49.run(Script49.groovy:59)

My "newList" object is a list as cf2, so I don't understand my error.

Thanks for your help.

Here is my code :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.bc.project.component.ProjectComponent
import org.apache.commons.collections.ListUtils

OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

//MutableIssue issue = issue
def issue = ComponentAccessor.getIssueManager().getIssueObject("USER-2387")
def parentIssue  = issue.getParentObject()



CustomField customField2 = customFieldManager.getCustomFieldObject((long)21740); // Using These Applications Prod
CustomField customField3 = customFieldManager.getCustomFieldObject((long)22900); // Used These Applications Prod
def cf2 = parentIssue?.getCustomFieldValue(customField2)
def cf3 = parentIssue?.getCustomFieldValue(customField3)

def componentValue = issue.getComponents()*.name


def changeHolder = new DefaultIssueChangeHolder();

//fieldConfig1 = customField2.getRelevantConfig(parentIssue)
//fieldConfig2 = customField3.getRelevantConfig(parentIssue)

if(customField2 != null){

    for (int i = 0; i < cf2.size(); i++) {
        for (int j = 0; j < componentValue.size(); j++) {
            if((String)cf2.get(i)==componentValue.get(j)){
                cf2.remove(i)
            }
        }
    }

    if(cf2.size()!=0) {
        customField2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(customField2), cf2), changeHolder)
    }
    else{
        customField2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(customField2), null), changeHolder)
    }


    if(cf3!=null){

        List<Option> newList = ListUtils.union((List<Option>)componentValue,(List<Option>)cf3)
        customField3.updateValue(null, parentIssue, new ModifiedValue(cf3, newList), changeHolder)
    }
    else {

        customField3.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(customField3), componentValue), changeHolder)

    }
}

//parentIssue.store()

1 answer

1 accepted

This widget could not be displayed.

Have a look at the objects you are working with here.  A "component" is not an "option" in terms of objects.  They have a lot in common, but not enough that you can just start adding components to a select list as though it's an option.

I suspect you probably need to parse your component list, looking at the name of the component, then, for each one found, look for it in the list of possible options and then add a new option of the matching value to the custom field.

Thank you Nic for your answer, I have update my code. No more error but still a problem :

when my customfield is empty (case of the else), I success to update the value (alright).

But in the first case (case of the if, when my customfield already contains value(s)) my customfield is not update even if my object "newList" contains well the new values that I want to assign to my customfield.

Do you have an idea ? 

Here is my code :

if(cf3!=null){

List<String> newList = ListUtils.union((List<String>)componentValue,(List<String>)cf3)
for(int i = 0;i<newList.size();i++) {
yes = [optionsManager.getOptions(fieldConfig2).getOptionForValue((String)newList.get(i), null)]
customField3.updateValue(null, parentIssue, new ModifiedValue(cf3, yes), changeHolder)
}


}
else {

for(int i = 0;i<componentValue.size();i++) {
yes = [optionsManager.getOptions(fieldConfig2).getOptionForValue((String)componentValue.get(i), null)]
customField3.updateValue(null, parentIssue, new ModifiedValue(cf3, yes), changeHolder)
}
}

Hi guys, 

I have found my error, I put my code here, it could maybe help someone ;)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.bc.project.component.ProjectComponent
import org.apache.commons.collections.ListUtils

OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

MutableIssue issue = issue
//def issue = ComponentAccessor.getIssueManager().getIssueObject("USER-2387")
def parentIssue  = issue.getParentObject()



CustomField customField2 = customFieldManager.getCustomFieldObject((long)21740); // Using These Applications Prod
CustomField customField3 = customFieldManager.getCustomFieldObject((long)22900); // Used These Applications Prod
def cf2 = parentIssue?.getCustomFieldValue(customField2)
def cf3 = parentIssue?.getCustomFieldValue(customField3)

def componentValue = issue.getComponents()*.name


def changeHolder = new DefaultIssueChangeHolder();

fieldConfig1 = customField2.getRelevantConfig(parentIssue)
fieldConfig2 = customField3.getRelevantConfig(parentIssue)

if(customField2 != null){

    for (int i = 0; i < cf2.size(); i++) {
        for (int j = 0; j < componentValue.size(); j++) {
            if((String)cf2.get(i)==componentValue.get(j)){
                cf2.remove(i)
            }
        }
    }

    if(cf2.size()!=0) {
        customField2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(customField2), cf2), changeHolder)
    }
    else{
        customField2.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(customField2), null), changeHolder)
    }


    if(cf3!=null){

        List<String> newList = ListUtils.union((List<String>)componentValue,(List<String>)cf3)
        ArrayList yes = new ArrayList<>()
        for(int i = 0;i<newList.size();i++) {
            yes.add(optionsManager.getOptions(fieldConfig2).getOptionForValue((String)newList.get(i), null))
        }
        customField3.updateValue(null, parentIssue, new ModifiedValue(cf3, yes), changeHolder)

    }
    else {

        for(int i = 0;i<componentValue.size();i++) {
            yes = [optionsManager.getOptions(fieldConfig2).getOptionForValue((String)componentValue.get(i), null)]
            customField3.updateValue(null, parentIssue, new ModifiedValue(cf3, yes), changeHolder)
        }
    }
}

parentIssue.store()

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

511 views 3 18
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