I have a script to update an Elements Connect field without affecting "change history" or "last update time", but it only works for single values:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.index.IssueIndexingParams
// Define the custom field ID. CUSTOMFIELD_ID = 333 for the custom field "Platform".
def CUSTOMFIELD_ID = 333
// Retrieve the CustomField object using its ID
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(CUSTOMFIELD_ID)
// Define the new value to be set for the custom field
def newValue = 'PT3'
// Define the JQL query to find issues
def jqlQuery = 'key in (ISSUEKEY-101,ISSUEKEY-102)'
// Execute the search with the provided JQL query
Issues.search(jqlQuery).each { issue ->
// Update the custom field value for each issue
customField.updateValue(
null, // No custom field configuration
issue, // The issue being updated
new ModifiedValue(issue.getCustomFieldValue(customField), newValue), // New value for the field
new DefaultIssueChangeHolder() // A holder for changes
)
// Re-index the issue to reflect the changes in the search index
ComponentAccessor.getComponent(IssueIndexingService).reIndex(
issue, // The issue to be re-indexed
IssueIndexingParams.INDEX_ISSUE_ONLY // Re-index only the issue
)
}def newValue = 'PT3,PT8'
results in: {"keys":["PT3,PT8"]}
but what I need is: {"keys":["PT3","PT8"]}
def newValue = ['PT3','PT8']
does not work with: new DefaultIssueChangeHolder()
Any ideas on how to make this work?
Community moderators have prevented the ability to post new answers.
it seems the value you need to assign to the newValue variable should be:
def newValue = '{"keys":["PT3","PT8"]}'
From your current script, it seems you're using a static value for newValue instead of a dynamic one. Updating it to the following format should resolve the issue.
Could you clarifiy if there's a specific reason why you're not replacing def newValue = 'PT3' with def newValue = '{"keys":["PT3","PT8"]}' in your script?
Don't hesitate to let me know if the problem persists.
Kind regards.
Maelle
Hi Maelle,
I feel stupid and happy at the same time...
Setting the value I was looking for directly, as you suggested, went totally over my head.
Thanks a lot for noticing and making me see.
Here is the working code in case someone faces the same blindness or just wants to do this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.index.IssueIndexingParams
// Define the custom field ID. CUSTOMFIELD_ID = 333 for the custom field "Platform".
def CUSTOMFIELD_ID = 333
// Retrieve the CustomField object using its ID
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(CUSTOMFIELD_ID)
// Define the new value to be set for the custom field
def newValue = '{"keys":["PT3","PT8"]}'
// Define the JQL query to find issues
def jqlQuery = 'key in (ISSUEKEY-101,ISSUEKEY-102)'
// Execute the search with the provided JQL query
Issues.search(jqlQuery).each { issue ->
// Update the custom field value for each issue
customField.updateValue(
null, // No custom field configuration
issue, // The issue being updated
new ModifiedValue(issue.getCustomFieldValue(customField), newValue), // New value for the field
new DefaultIssueChangeHolder() // A holder for changes
)
// Re-index the issue to reflect the changes in the search index
ComponentAccessor.getComponent(IssueIndexingService).reIndex(
issue, // The issue to be re-indexed
IssueIndexingParams.INDEX_ISSUE_ONLY // Re-index only the issue
)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.