• Community
  • Products
  • Jira
  • Questions
  • Copying value to Jira default field "Component/s" from other custom field values, getting Error Cannot invoke method getRelevantConfig() on null object

Copying value to Jira default field "Component/s" from other custom field values, getting Error Cannot invoke method getRelevantConfig() on null object

Hi,

I am trying to Copy value to Jira default field "Component/s" from other custom field values by doing a clone, getting Error Cannot invoke method getRelevantConfig() on null object using my below script - please suggest what's wrong in my script?

/* def Components = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Component/s") */

def cascade6 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Global XLS Config Input List-1 Components") //it's multiple select list custom field 

def cfVal6 = issue.getCustomFieldValue(cascade6)

    cfVal6?.each { LazyLoadedOption it ->
    def issueFactory = ComponentAccessor.getIssueFactory()
    def issueManager = ComponentAccessor.getIssueManager()
    def newIssue = issueFactory.cloneIssue(issue)
 
    def optionToSet = 	ComponentAccessor.getOptionsManager().getOptions(Components.getRelevantConfig(issue)).find {option -> option.value == it.value}
 
    // set the value of the Components from "Global XLS Config Input List-1 Components" custom field values updated by user
    
    if(optionToSet)    
    newIssue.setCustomFieldValue(Components, optionToSet)
  
    def Priority = issue.getPriority()
    def Asignee = issue.getAssignee()
    newIssue.setPriority(Priority)
    
    newIssue.setSummary("CLONE - $issue.summary")
    newIssue.setProjectId(issue.projectId)
    newIssue.setDescription(issue.description)
    //newIssue.setAssigneeId("Unassigned")
    newIssue.setAssigneeId("Asignee") 
    
	Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
    issueManager.createIssueObject(currentUser, newIssueParams)
 
    log.info "Issue " + newIssue?.getKey() + " created"
	
	issueLinkManager = ComponentAccessor.getIssueLinkManager()    
    
    Collection<IssueLinkType> 	issueLinkTypes1=ComponentAccessor.getComponentOfType(IssueLinkTypeManager.class).getIssueLinkTypes();
    String linkID=null;
    for (IssueLinkType linktype : issueLinkTypes1) {
        String name=linktype.getName();
        log.warn("Issue Link Type Name : " + name);
        String id=linktype.getId();
        log.warn("Issue Link Type Id : " + id);
        
        if(name.equals("Cloners")){// change link name here
            linkID=linktype.getId();
            break;
        }
    }
    log.warn("Issue Link Types : " + IssueLinkType)
    log.info("createIssueLink: i")
           
    //linkManager.createIssueLink(newIssue.getId(), issue.getId(), 	Long.parseLong(linkID),Long.valueOf(0), currentUser);   
     issueLinkManager.createIssueLink(newIssue.getId(), issue.getId(), Long.parseLong(linkID),Long.valueOf(0), currentUser);      
           
    }// end of cfVal6?.each

2 answers

1 accepted

This widget could not be displayed.

The first example in the ScriptRunner documentation here shows you how to get the components and set them.

You should be able to adapt that for your requirement.

Hi Adam,

Same script has been used in my workflow post function, still same error has been observed.

Even I tried to copy the exact lines posted in the documentation link shared by you by defining another post function separately to isolate the issue - even then it's throwing same ERROR at same line where we set component value i,e issue.setComponentObjects([component])

import com.atlassian.jira.component.ComponentAccessor

def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def project = issue.getProjectObject()

def component = projectComponentManager.findByComponentName(project.getId(), "Agent")

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

ERROR @Manjunatha K R thrown @ line issue.setComponentObjects([component]) as below::

[Static type checking] Cannot find mathing 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 eixsts @ line 9, column 9.

 

Even this post functions are defined before the below other workflow steps.

  1. Update change history for an issue and store the issue in the database.
  2. Re-index an issue to keep indexes in sync with the database.

I am not getting any right clue, why it's throwing this ERROR, even though script and step defined is looks fine.

Please suggest, what is wrong in this ????

That line should be replaced with this one: 

issue.setComponent([component])

We'll update the documentation to reflect that.

I wouldn't rely too heavily on the static type checker. There are a lot of limitations with it that can be found here. We are replacing it with something more powerful soon.

Thanks Adam for your timely feedback. It works perfectly fine now.

This widget could not be displayed.
Vasiliy Zverev Community Champion Mar 15, 2017

Pay attention that you call "Component/s.getRelevantConfig(issue))" but what is Component/s?

As Vasiliy pointed out Component/s doesn't look like the FieldConfig which you need to pass to getOptions. I'm guessing you meant to pass the field config for the Component/s field?

In that case you should be able to get the options, like in this example: https://answers.atlassian.com/questions/39580712

You likely just need to change the name of the field the options are retrieved for.

Thanks Vasiliy and Adam for your prompt response.

Based on the "Global XLS Config Input List-1 Components" custom field number of values (it's Select List (multiple choices) type), I am creating a clone issue(s) and would like to copy 'Global XLS Config Input List-1 Components' values to one by one into cloned issues of Component/s JIRA default field value.

Say example: "Global XLS Config Input List-1 Components" custom field is updated with the following values::

  • F5
  • POC
  • DB
  • Scripts

I will traverse and read the "Global XLS Config Input List-1 Components" custom field values one at a time and create a clone issue(s) and try to copy first value into first cloned issue's "Component/s" JIRA default field and 2nd value into second cloned issue's "Component/s" JIRA default field and same way it repeats until 4 values get over using the below loop.

    cfVal6?.each { LazyLoadedOption it ->
    def issueFactory = ComponentAccessor.getIssueFactory()
    def issueManager = ComponentAccessor.getIssueManager()
    def newIssue = issueFactory.cloneIssue(issue)

 

But how to set F5 value into first cloned issue's "Component/s" JIRA default field as it's not a custom field? and needed values are added into Components field for a project already.

 

def optionToSet =   ComponentAccessor.getOptionsManager().getOptions(Component/s.getRelevantConfig(issue)).find {option -> option.value == it.value}

  
if(optionToSet)   
    newIssue.setCustomFieldValue(Component/s, optionToSet)
I think I should not use setCustomFieldValue(), as Components is a JIRA default system field - what is the right way to set this value into Components field in cloned issues one by one??? 

if any new  value updated into "Global XLS Config Input List-1 Components" values will be updated/added as a new component into "Component/s" JIRA default field.

Also user might update already existing Components values into "Global XLS Config Input List-1 Components" custom field value.

  • Manju

Hi,

The below updated script is working fine now to create clone issues based on the custom field i,e "Global XLS Config Input List-1 Components" values successfully now. And also able to copy the needed JIRA issue default and custom field values from the parent issue to clone issues.

But only issue is with copying "Global XLS Config Input List-1 Components" custom field values into cloned issues Component/s field.

Please let me know whether the below usage of copying is correct or any other method, should work to copy into Issue Component Field correctly.

/* The below snippet is copied from the below shared completed script code used in my workflow post function for the easy reference */

def projectComponentManager = ComponentAccessor.getProjectComponentManager()

def project = issue.getProjectObject()  

def component = projectComponentManager.findByComponentName(project.getId(), it.value)

issueInputParameters1.setComponentIds(component?.getId())

def updateValidationResult1 = issueService.validateUpdate(currentUser, newIssue.id, issueInputParameters1)
 
    if (updateValidationResult1.isValid()) {
        issueService.update(currentUser, updateValidationResult1)
    } else {
        log.warn updateValidationResult1.errorCollection.errors
    }

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 -&gt;
    def issueFactory = ComponentAccessor.getIssueFactory()
    def issueManager = ComponentAccessor.getIssueManager()
    def  newIssue = issueFactory.cloneIssue(issue)
    
	IssueInputParameters issueInputParameters1 = issueService.newIssueInputParameters(); 
    def 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&lt;String,Object&gt; newIssueParams = ["issue":newIssue] as Map&lt;String,Object&gt;
    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
    }
    
       
    issueLinkManager = ComponentAccessor.getIssueLinkManager()    
    
    Collection&lt;IssueLinkType&gt; issueLinkTypes1=ComponentAccessor.getComponentOfType(IssueLinkTypeManager.class).getIssueLinkTypes();
    String linkID=null;
    for (IssueLinkType linktype : issueLinkTypes1) {
        String name=linktype.getName();
        log.warn("Issue Link Type Name : " + name);
        String id=linktype.getId();
        log.warn("Issue Link Type Id : " + id);
        
        if(name.equals("Cloners")){// change link name here
            linkID=linktype.getId();
            break;
        }
    }
    log.warn("Issue Link Types : " + IssueLinkType)
    log.info("createIssueLink: i")
           
   	issueLinkManager.createIssueLink(newIssue.getId(), issue.getId(), 	Long.parseLong(linkID),Long.valueOf(0), currentUser);      
             
}// cfVal6?.each loop end

Further reference to other Atlassian answers found.

https://answers.atlassian.com/questions/16882455/how-to-set-a-component-based-on-a-custom-select-list

https://bitbucket.org/snippets/Adaptavist/Mbdpo

was trying to modify the above my script to copy the custom-select-list values into Cloned issues as below:

But I am getting ERROR in newIssue.setComponentObjects([component]) line.... Not sure why??

 Please advice if anything wrong in the refered script based on the links posted before...

[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 133, column 9.

 

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 -&gt;
    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])
}
}

@Jamie Echlin [Adaptavist]

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

172 views 1 3
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