Snippets

Adaptavist JIRA - Set Component

Created by Jamie Echlin
package examples

import com.atlassian.jira.bc.project.component.ProjectComponent
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.project.Project

def projectComponentManager = ComponentAccessor.getProjectComponentManager()

MutableIssue issue = issue
def cfSrcId = 12800L
CustomFieldManager cManager = ComponentAccessor.getCustomFieldManager()
CustomField srcField = cManager.getCustomFieldObject(cfSrcId)
def cfrt = issue.getCustomFieldValue(srcField)?.value

Project project = issue.getProjectObject()

ProjectComponent component = null
if(cfrt == "Analytics"){
    component = projectComponentManager.findByComponentName(project.getId(), "Analytics")
}
else if(cfrt == "LMM"){
    component = projectComponentManager.findByComponentName(project.getId(),"LMM")
}
else if(cfrt == "Openstack"){
    component = projectComponentManager.findByComponentName(project.getId(),"Openstack")
}

if (component) {
    issue.setComponentObjects([component])
}

Comments (2)

  1. Manjunatha K R

    Hi Jamie Echlin, I was trying to use your script to set Select List (multiple choices) type custom field values individually(1st value to fist clone issue, 2nd value to second clone issue - number of clone issues based on the total number of values updated into Select List ) to cloned issues using my below script. But I am getting ERROR while setting the component value as:: [Static type checking] Cannot find matching method

    com.atlassian.jira.issue.MutableIssue#setComponentObjects(java.util.List<com.atlassian.jira.bc.project.component.ProjectComponent>). Please check if declared type is right and if method exists @ line 25, column 9. @@@ newIssue.setComponentObjects([component])

    .... Not sure why??

    import com.atlassian.jira.bc.project.component.ProjectComponent
    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.project.Project
    
    def cascade6 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Global XLS Config Input List-1 Components")
    
    def cfVal6 = issue.getCustomFieldValue(cascade6)
    
    def projectComponentManager = ComponentAccessor.getProjectComponentManager()
    def project = issue.getProjectObject()       
    
    cfVal6?.each { LazyLoadedOption it ->
        def issueFactory = ComponentAccessor.getIssueFactory()
        def issueManager = ComponentAccessor.getIssueManager()
        def  newIssue = issueFactory.cloneIssue(issue)
        ProjectComponent component = null
    
        component = projectComponentManager.findByComponentName(project.getId(), it.value)
    
    if (component) {
        newIssue.setComponentObjects([component])
    }
    def epicLinkCf1 = customFieldManager.getCustomFieldObjectByName("Epic Link")
        newIssue.setCustomFieldValue(epicLinkCf1, epicLink)
    
        def sprint1 = customFieldManager.getCustomFieldObjectByName("Sprint")
        def sprint123 = issue.getCustomFieldValue(sprint1)
    
        def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
        newIssue.setCustomFieldValue(sprint, sprint123)
    
    
        def Priority = issue.getPriority()
        def Asignee = issue.getAssignee()
        newIssue.setPriority(Priority)
        newIssue.setIssueTypeId("10202")
        newIssue.setSummary("CLONE - $issue.summary - $cfVal4")
        newIssue.setProjectId(issue.projectId)
        newIssue.setDescription(issue.description)
        newIssue.setAssigneeId("Unassigned")
    
        Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
        issueManager.createIssueObject(currentUser, newIssueParams)
    
        log.info "Issue " + newIssue?.getKey() + " created"
    }
    
  2. Manjunatha K R

    Even I tried to use setComponentIds() approach to update the Component field into cloned issues - But Cloned issues are created but Component values are copied from the parent issue component field instead of from select list....

    issueInputParameters1.setComponentIds(component?.getId())

    def cascade6 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Global XLS Config Input List-1 Components")
    
    def cfVal6 = issue.getCustomFieldValue(cascade6)
    
    def projectComponentManager = ComponentAccessor.getProjectComponentManager()
    def project = issue.getProjectObject()       
    
    cfVal6?.each { LazyLoadedOption it ->
        def issueFactory = ComponentAccessor.getIssueFactory()
        def issueManager = ComponentAccessor.getIssueManager()
        def  newIssue = issueFactory.cloneIssue(issue)
    IssueInputParameters issueInputParameters1 = issueService.newIssueInputParameters(); 
        ProjectComponent component = null
    
        component = projectComponentManager.findByComponentName(project.getId(), it.value)
        issueInputParameters1.setComponentIds(component?.getId())
    
        def epicLinkCf1 = customFieldManager.getCustomFieldObjectByName("Epic Link")
        newIssue.setCustomFieldValue(epicLinkCf1, epicLink)
    
        def sprint1 = customFieldManager.getCustomFieldObjectByName("Sprint")
        def sprint123 = issue.getCustomFieldValue(sprint1)
    
        def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
        newIssue.setCustomFieldValue(sprint, sprint123)
    
    
        def Priority = issue.getPriority()
        def Asignee = issue.getAssignee()
        newIssue.setPriority(Priority)
        newIssue.setIssueTypeId("10202")
        newIssue.setSummary("CLONE - $issue.summary - $cfVal4")
        newIssue.setProjectId(issue.projectId)
        newIssue.setDescription(issue.description)
        newIssue.setAssigneeId("Unassigned")
    
        Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
        issueManager.createIssueObject(currentUser, newIssueParams)
    
        log.info "Issue " + newIssue?.getKey() + " created"
    
        def updateValidationResult1 = issueService.validateUpdate(currentUser, newIssue.id, issueInputParameters1)
    
        if (updateValidationResult1.isValid()) {
            issueService.update(currentUser, updateValidationResult1)
        } else {
            log.warn updateValidationResult1.errorCollection.errors
        }
    
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.