Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Direct field update triggers another field update does not work.

Antoine Berry Community Leader Jun 23, 2017

Hello,

I have two custom fields (both Insight fields). When I update customfield1, a listener also updates the value of customfield2 according to customfield1 value. This works fine when I click the "Edit" button, but does not work when I edit directly on the issue. I have checked, the listener is properly called in both cases, it just seems not to commit in some way in the second case.

Here is what my script looks like : 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customfield_1_Changed = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Customfield_1"}
if (customfield_1_Changed){
   def customfield_2_value = SomeMethodThatGivesTheValue()
   def customfield_2 = 
   customFieldManager.getCustomFieldObject("customfield_2")
   customfield_2.updateValue(null, issue, new ModifiedValue(null, 
   customfield_2_value), new DefaultIssueChangeHolder())
   def issueIndexingService = 
   ComponentAccessor.getComponent(IssueIndexingService)
   issue.store()
   issueIndexingService.reIndex(issue)
}

4 answers

1 accepted

0 votes
Answer accepted

Hi Antoine,

The same issue updated event is fired and handled when doing the inline edit and when you bring the edit screen up.

I suspect it's because your doing the reindexing and storing the issue in the database manually.

The best way of updating a custom field on an issue is to use IssueService as it will do the saving and reindexing for you as well as show you what errors prevented the update from happening if any.

The example below copies the value of custom field 1 to custom field 2 (assuming both are text fields) if custom field 1 has changed.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent

def event = event as IssueEvent

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.getIssueService()

def customFieldChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Customfield_1"}

if (customFieldChanged) {
    def customField1 = customFieldManager.getCustomFieldObject("customfield_1")
    def customField2 = customFieldManager.getCustomFieldObject("customfield_2")
    def customField1Value = customField1.getValue(event.issue)

    def issueInputParameters = issueService.newIssueInputParameters()

    issueInputParameters.addCustomFieldValue(customField2.idAsLong, customField1Value)

    def validationResult = issueService.validateUpdate(event.user, event.issue.id, issueInputParameters)

    if (validationResult.isValid()) {
        issueService.update(event.user, validationResult)
    } else {
        log.warn "Did not update issue: $validationResult.errorCollection.errors"
    }
}

Let us know how you get on after you have used issue service in your listener.

 Thanks,
Adam

Antoine Berry Community Leader Jul 12, 2017

Hi Adam, thanks for your reply. I have a problem while updating the field : 

issueInputParameters.addCustomFieldValue(customField2.idAsLong, customField1Value)

only works for String values. Mine is an ArrayList<MutableObjectBean> (I am working with Insight), so I cannot make this work. 

Thanks for that I understand now. It may be worth contacting the vendor for the Insight plugin and seeing if they can give you any pointers here.

There may be some caveats with this type of field when you do the inline edit which is preventing the field from being updated if it works on the edit screen. For the event listener there should be no difference between the two.

Unfortunately we don't have any specific examples of updating that type of custom field.

For regular JIRA custom fields the way I've pointed out is the best way to update them.

Antoine Berry Community Leader Jul 12, 2017

Thanks for your quick feedback. Does that work even for non string customfields? Like list, checkbox or number (Since the custom field value is not directly a string)

If it's a list or checkbox you should put the option id in as a string.

For multiselects you can pass in multiple option ids:

issueInputParameters.addCustomFieldValue(customField.idAsLong, "1", "2")

For numbers you just need to pass them in as a string.

Every JIRA custom field type you can update in this way as JIRA uses IssueService underneath.

Hope this clears things up for you.

Hehe... I tried with the insight unique object code and it worked! My problem is with your method I need to use event. I am using a lot of classes for different functions and I am going through at least 3 classes to update this field... Is there a way to get the event from the Issue object (or in any kind of way which does not imply to put it as a function paramater) ?
Thanks!

That's good to hear I was wondering if that would work.

You could have a common function which it sounds like you have already. The only event-centric things are where we get the issue and the current user from.

If you don't have an event you can use the following to get the current user (note that the user must have edit permissions):

import com.atlassian.jira.component.ComponentAccessor

def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

The issue is already available in most places in ScriptRunner so you can just use it as is.

Hope this helps.

Antoine Berry Community Leader Jul 12, 2017

You are absolutely right, I did not pay enough attention, my bad...
I guess i have to re-write my functions now, but I guess it is worth it. One last thing I want to be sure of, does the code trigger the Issue update event as is ? (I need the notification to be sent)
Thanks!

Yes that will trigger the issue updated event and also send mail, as if the issue was updated by that user in JIRA.

Antoine Berry Community Leader Jul 12, 2017

Wow, thanks for everything and your quick answers. Cheers

Antoine Berry Community Leader Jul 13, 2017

Quick update. The code works, but it usually throws (I did not identify a specific pattern causing the error) this exception : java.util.concurrentmodificationexception on this call : 

issueService.update(user, validationResult)

 Have you already encountered this issue ? I can provide the full stacktrace if needed.

Antoine Berry Community Leader Jul 17, 2017

@adammarkham I had to first clear the custom field not to get the error : 

issueObject.setCustomFieldValue(myCustomField, null)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED ,true)

 Can you confirm this is the right behaviour ?

Antoine Berry Community Leader Jul 18, 2017

@adammarkham I have noticed that this method works only if the field is displayed on the same screen when the field update occurs. This is really problematic because we use some fields which are not displayed directly on the issue on our boards / filters... Any idea?

@Antoine Berry I have not seen that before. It must be something specific to do with the Insight custom field. You may get a solution if you contact the vendor directly.

Do you see the same error for a regular JIRA custom field when updating it via this method?

Antoine Berry Community Leader Jul 31, 2017

@adammarkham I have used this method only with Insight custom field, so no. I guess it is linked to the insight custom field somehow, I will come back here if I notice that for a regular custom field.

Hi @Antoine Berry 

 

I've currently got this issue regarding an Insight custom field and trying to get an issue event to bulk update old tickets via Issue Updated.

https://community.atlassian.com/t5/Adaptavist-questions/Built-in-scripts-from-Generate-Events-wont-trigger-Insight-Read/qaq-p/1456635#M6782

 

Is there any.....insight you could impart on the subject?

 

Thanks

Antoine Berry Community Leader Aug 17, 2020

Hi @Steve Letch ,

I do not work with insight anymore unfortunately. To be honest I did not fully grasp your problem though. Is there an issue with an event or you just wanted to fix old issues using a oneshot script ?

Antoine

@Antoine Berry 

So I have a read only Insight field. These fields update on Issue created and Issue Updated. 

 

However If you generate an 'Issue Updated' event from Scriptrunner, it doesn't trigger the Read Only field to update

 

This would be useful where you have a load of old tickets that you want to inherit the info from the Read only field and switch your reporting stats to look at this new field instead of the current fields used to report

 

I use mine to grab the object based on {currentreporter}

 

Alas the only kind of issue updated event I've been able to get my Read only field to react to, is changes made to the issue in the front end, changing a field etc, either by direct user input or using native Jira bulk changing, even scriptrunner bulk changing doesn't trigger it.

Antoine Berry Community Leader Aug 18, 2020

Ok did you try Pete's answer on your question ?

It seems to be doing exactly what you want. You run a jql query to get all the issues you want to update, then proceed with the update for each of them inside the loop.

0 votes

Hi Adam, thanks for your reply. I have a problem while updating the field : 

issueInputParameters.addCustomFieldValue(customField2.idAsLong, customField1Value)

only works for String values. Mine is an ArrayList<MutableObjectBean> (I am working with Insight), so I cannot make this work. 

Antoine Berry Community Leader Jul 12, 2017

Sorry for the double answer, my answer could not be validated when i added code block...

Monique vdB Community Manager Jul 12, 2017

@Antoine Berry and @adammarkham sorry for the spam filter's over-excitement. I've released your comments from the quarantine. 

0 votes
Antoine Berry Community Leader Jul 12, 2017

I have been able to resolve this Issue by puting a log around the code block supposed to automatically modify the custom fields values. It is not the first time puting a log solves the issue, it is a really weird and constraining behaviour.

Antoine Berry Community Leader Jul 12, 2017

Nevermind, it worked only once. Still need help on this.

0 votes
Antoine Berry Community Leader Jun 27, 2017

Hey, any insight on this issue ?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

97 views 1 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you