Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

Changing a field value with scriptrunner depending on an other field value

Hi,

I would like to write a script with scriptrunner that updates a field depending on an other field.

Field A (Textfield)

Field B (Textfield)

The user changes Field A. Then Field B gets an other value.

  • Does anyone have a clue, where shall I write the script in Scriptrunner (Built-in Scripts, Listeners, ...)
  • Can anyone provides a snippet?

Thanks in advance.

Best

Friedrich

 

2 answers

1 accepted

Hi @fr Behnk it depends when you want to do this update. If it is in a transition you only have to write a new postfunction script. 

If you want to do it whenever the field is updated you should write a new listener. You have to have in mind that the update only work when and issue is updated. If the field is changed in a field screen, the listener wouldnt work unless you add the event that is fired in that specific transition.

For the postfunction script (this way you can use it even in the create transition. Add it between the update postfunction and the reindex postfunction places):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField cfTextFieldA = customFieldManager.getCustomFieldObject(11111L), //Change the ID with the TextFieldA ID
cfTextFieldB = customFieldManager.getCustomFieldObject(22222L) //Change the ID with the TextFieldB ID

cfTextFieldB.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfTextFieldB), issue.getCustomFieldValue(cfTextFieldA)), new DefaultIssueChangeHolder())

 For the listener:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryItem
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.ofbiz.core.entity.GenericValue

IssueEvent event = event as IssueEvent
MutableIssue issue = event.issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

CustomField cfTextFieldA = customFieldManager.getCustomFieldObject(11111L), //Change the ID with the TextFieldA ID
cfTextFieldB = customFieldManager.getCustomFieldObject(22222L) //Change the ID with the TextFieldB ID

GenericValue changeLogs = event.getChangeLog()
ArrayList<ChangeHistoryItem> changedItem = changeHistoryManager.getAllChangeItems(issue).findAll {
it.changeGroupId == changeLogs.id
}

if (changedItem?.find { it.field.equalsIgnoreCase(cfTextFieldA.name) }) {
cfTextFieldB.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfTextFieldB), issue.getCustomFieldValue(cfTextFieldA)), new DefaultIssueChangeHolder())
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
}

 Hope it helps.

Regards

0 votes
Nir Haimov Community Leader Feb 11, 2019

Hi @fr Behnk,

You should write it as listener.

Because you want to listen to "issue updated".

Any time an issue has been updated, you want to check field A and update B accordingly.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

32 views 0 2
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