JIRA Scriptrunner : "Copy custom field value" Listener script does not work when searching

Hi,

 

  We are currently working on a ScriptRunner Listener Script that updates subtasks' custom field value based on their parent custom field value.  We put it on the "Issue Updated" event.  After some testing, the subtasks' values are correctly updated when we browse the subtask's detailed information.  If we try to search those issues with a filter, we can add the custom field into the displayed columns (the good value is displayed), but filtering by this custom field value does not work (if we select the custom field and use it in the filter predicates, searching for a value only returns the parent task).  

  If we modify manually the custom field in the subtask, the search returns this particular subtask. 

  Also, there is no "Log" entry for the subtask modification : if we change the value from "A" to "B" in the parent task, the subtask's change log does not show the modification even if the value displayed in the subtask's custom field is "B" and not "A" anymore.

  The custom field is from another AddOn (https://marketplace.atlassian.com/plugins/ru.teamlead.jira.plugins.teamlead-crm-plugin-for-jira/server/overview), so it may be related to that

  Is there something we are missing ?  Here is our code.

  Thank you for your help !

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def subTaskList = event.issue.getSubTaskObjects();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject(10226L)
def parentMyFieldValue = event.issue.getCustomFieldValue(cField)

if (cField != null)
{
for(subtaskIssue in subTaskList)
{

def changeHolder = new DefaultIssueChangeHolder();
cField.updateValue(null, subtaskIssue, new ModifiedValue(subtaskIssue.getCustomFieldValue(cField), parentMyFieldValue),changeHolder);

}
 

 

2 answers

Have you tried manually doing a reindex to see if that changes the outcome of the JQL search?

Your script looks correct. You have the right code to update a custom field. 

It seems like the searcher that is on the custom field from CRM for JIRA may be incorrect. I do not have any experience with that add-on. Have you tried to ask them for an explantion?

Sorry for the delay, we were on another issue.

We did not contact the add-on's provider yet.  Performing a reindex did not solved the issue.

We will try to check with the add-on's creator on this.

Thank you for your quick answer !

Hi, 

  After some research and help from the AddOn creator, this code seems to do the trick :

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService


def issueManager = ComponentAccessor.getIssueManager()
def assignedUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def subTaskList = event.issue.getSubTaskObjects();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject(10226L)
def indexing = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.class);
def issueService = ComponentAccessor.getIssueService();

if (cField != null)
{
for(subtaskIssue in subTaskList)
{
MutableIssue mutableIssue = (MutableIssue)subtaskIssue;
def parentMyFieldValue = event.issue.getCustomFieldValue(cField);
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.addCustomFieldValue(cField.id, parentMyFieldValue.toString());

IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(assignedUser, mutableIssue.getId(), issueInputParameters);
if (updateValidationResult.isValid())
{
IssueService.IssueResult updateResult = issueService.update(assignedUser, updateValidationResult);
if (!updateResult.isValid())
log.error("error in issueService.update");
}
else
{
log.error("error in issueService.validateUpdate ; "+updateValidationResult.getErrorCollection());
}
}

}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

71 views 0 5
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