How to link a CR to list of customfields ID in scriprunner

Joshua Molina September 3, 2019

Hello,

I have a question. I have a CR and a Customfields ID list. That CR belongs to one of the customfields, and I wish I could link it.

Example: CR-2546 --> "customfield_xxx"

I have the following code, but it runs through the two customfields and only takes the value of one of them but does not link:

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult
import com.atlassian.query.Query
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.Issue


CommentManager commentManager = ComponentAccessor.getCommentManager();
def changeHolder = new DefaultIssueChangeHolder();

// Get List of issues
List<Issue> issuesFound = searchResults.getIssues();
int total = issuesFound.size();

log.info("Closing of " + total + " resolved issues");
if (total > 0) {

int totalIssuesFound = issuesFound.size();
int counter = 1;
for (Issue issueInmutable : issuesFound){

      MutableIssue issue = issueManager.getIssueObject(issueInmutable.key);


String[] Customfields = ["customfield_xxx", "customfield_yyy"]
      def customFieldManager = ComponentAccessor.getCustomFieldManager()
for(int a = 0; a < Customfields.length; a++){
def currentCf = Customfields[a]
def customfield = customFieldManager.getCustomFieldObject(currentCf)
//String ticket = issue.getCustomFieldValue(customfield)
def linkedIssueCustomFieldValue = issueManager.getIssueObject(issueInmutable.key).getCustomFieldValue(customfield)
log.error(linkedIssueCustomFieldValue)
}
}
}

 

Can somebody help me?

 

Thank you

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 3, 2019

I don't understand your question.

  1. What is CR-2546? Is that a jira issue key?
  2. What types are your customFields? Free text? Select? Issue Picker?
  3. What do you mean by "link"? Do you wish to create an issue link based on the key found in customField?
Joshua Molina September 3, 2019

Hi,

  1. What is CR-2546? Is that a jira issue key?
    1. Yes, CR-2546 is a jira issue key
  2. What types are your customFields? Free text? Select? Issue Picker?
    1. The customFields are free text
  3. What do you mean by "link"? Do you wish to create an issue link based on the key found in customField?
    1. I want to relate each key issue to the customfield to which they belong

 

Thank you

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 4, 2019

I still don't understand what you are trying to do.

Joshua Molina September 4, 2019

Hello,

What I want is:

The first 'for' will go through key issues, and the second 'for' will go through the 'Customfields' variable. Each issue key belongs to a value of the 'Customfields' variable, so you should go through the first 'for' and in the second, when you find your associated customfield, stop and go to the end of 'Customfields.length'.

I don't know if I have explained myself better now.

Thank you

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 5, 2019

I am wondering what your goal is. What's the business requirement?

I can see from your code that you are trying to output the value from custom fields xxx and yyy for all the issues in your searchResults (searchResult definition was not included in your initial post). 

But I don't understand what you want to do with those values. You've talked about "linking" or "relating" them. Are you trying to actually create issue links? Or store different values in other fields? 

I'm also not clear what your error or unexpected behavior is.

Perhaps if you simplify your code and output more logs it will help you. You seem to have some redundant part getting mutable issue which, in my experience is not necessary.

This script works as expected for me in the console:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter

def issueManager = ComponentAccessor.issueManager
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager= ComponentAccessor.customFieldManager

def query = jqlQueryParser.parseQuery('issuekey=JSP-1922')
def searchResults = searchProvider.search(query, currentUser, PagerFilter.getUnlimitedFilter())

def customFieldIds = ['customfield_10025', 'customfield_10101']
searchResults.total
log.info "Closing of ${searchResults.total} resolved issues"
searchResults.issues.each{issue->
customFields.each{cfId->
def customField = customFieldManager.getCustomFieldObject(cfId)
def cfValue = issue.getCustomFieldValue(customField)
log.debug "$cf value for $issue.key is $cfValue"
}
}

Suggest an answer

Log in or Sign up to answer