Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Try to get a value from another issue

Marc-Antoine Gosselin March 29, 2019

Description:

My client is using a issue picker to select an issue in his ticket.

When the issue is picked I try to populate other field base on the information available is the issue he picked.

I want to send the information from the issue he picked to the issue where he picked it.

 For exemple he picked an issue who have multiple field. I want to copy the value from one of those field to a field in the ticket he's currently using.

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor;

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def issuePicked = customFieldManager.getCustomFieldObjectByName("Courtier")
def cf = cfValues[issuePicked]
def issueManager = ComponentAccessor.getIssueManager()


def targetIssue = issueManager.getIssueObject(cf)

CustomField acConseiller = customFieldManager.getCustomFieldObjectByName('Conseiller')
CustomField acBureauVentes = customFieldManager.getCustomFieldObjectByName('Bureau de ventes')
CustomField acPodium = customFieldManager.getCustomFieldObjectByName('Podium')


def acConseiller_value = targetIssue.getCustomFieldValue(acConseiller)
def acBureauVentes_value = targetIssue.getCustomFieldValue(acBureauVentes)
def acPodium_value = targetIssue.getCustomFieldValue(acPodium)


issue.setCustomFieldValue(acConseiller,acConseiller_value)
issue.setCustomFieldValue(acBureauVentes,acBureauVentes_value)
issue.setCustomFieldValue(acPodium,acPodium_value)

 

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Champion
March 29, 2019

Hi @Marc-Antoine Gosselin , 

I would advise to use another method to update a custom field value, i.e. : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

CustomField acConseiller = customFieldManager.getCustomFieldObjectByName('Conseiller')
def acConseiller_value = targetIssue.getCustomFieldValue(acConseiller)
def acConseiller_current_value = issue.getCustomFieldValue(acConseiller)

acConseiller.updateValue(null, issue, new ModifiedValue(acConseiller_current_value, acConseiller_value), new DefaultIssueChangeHolder())

 Also  to get the custom field using its ID (so the script will still be working if an admin changes the field name) : 

int acConseillerId = 12345
CustomField acConseiller = customFieldManager.getCustomFieldObject(acConseillerId)

Antoine

Marc-Antoine Gosselin March 29, 2019

Hi Antoine,

I will change the method right away

Thank you so much for the quick answer.

 

The problem is the value is in another issue. An issue select by the client in an custom field issue picker.

 

I'm not sur how to resolve this

for exemple conseiller is a field in the ticket cac-254

the client select cac-254 in another issue for exemple act-293

I need the field to be updated in the issue act-293 using the value from the issue cac-254

Antoine Berry
Community Champion
March 29, 2019

I see. Is the field a "Single Issue Picker" custom field ?

Marc-Antoine Gosselin March 29, 2019

yes

Antoine Berry
Community Champion
March 29, 2019

Then it should be pretty straight forward, since the value you get from retrieving that custom field value is an "Issue" : 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

int issuePickerId = 12345
CustomField issuePicker = customFieldManager.getCustomFieldObject(issuePickerId)
def targetIssue = issue.getCustomFieldValue(issuePicker)

int acConseillerId = 11111
CustomField acConseiller = customFieldManager.getCustomFieldObject(acConseillerId)
def acConseiller_current_value = issue.getCustomFieldValue(acConseiller)

def acConseiller_value = targetIssue.getCustomFieldValue(acConseiller)

acConseiller.updateValue(null, issue, new ModifiedValue(acConseiller_current_value, acConseiller_value), new DefaultIssueChangeHolder())

 Just update the IDs with your custom field IDs. 

What it does :

  • get the issue object (targetIssue) from the current issue custom field issue picker.
  • retrieve the current issue (acConseiller_current_value) value for custom field acConseiller
  • retrieve the target issue (acConseiller_value) value for custom field acConseiller
  • updates the current issue value for custom field acConseiller with the value on the target issue.

Hope this is what you are aiming for.

Marc-Antoine Gosselin March 29, 2019

Yes right to the point thanks a lot!!!

Like Antoine Berry likes this
Marc-Antoine Gosselin April 1, 2019

I'm sorry to ask for your help once more but since once of the field is an user picker I'm not able to get the value ''name'' in the costumfield

Antoine Berry
Community Champion
April 1, 2019

Hi,

You can use these methods : 

int userPickerId = 10800
def cfUserPicker = customFieldManager.getCustomFieldObject(userPickerId)
def cfUserPickerValue = issue.getCustomFieldValue(cfUserPicker)
def userName = cfUserPickerValue.getDisplayName()
def userKey = cfUserPickerValue.getName()

Check the javadoc to see what methods are available for a user object : 

https://docs.atlassian.com/software/jira/docs/api/7.1.0/com/atlassian/jira/user/ApplicationUser.html

Antoine

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events