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

trying to grab linked customfield value from a specific linked issue that i defined in issue picker

Break down:

we are linking issues from a script runner issue picker field. at witch time would like to grab a custom field value from linked issue defined by the issue picker. this value will then be put in a table grid.  need help with the cost section. here is what we have.

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;
import java.text.DecimalFormat;
import org.apache.log4j.Logger;

// set up logger
Logger log=Logger.getLogger("com.idalko.scripts");

// get an issue
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("TSTCOR-198"); //testing
//Issue issue = issueManager.getIssueObject(issue.key); //actual use
Long issueId = issue.getId().toLong();

// get TGE custom field
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomField = customFieldManager.getCustomFieldObjectsByName("History")[0];
Long tgeCustomFieldId = tgeCustomField.getIdAsLong();

ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// read grid data
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService");
def gridFieldData = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass);

StringBuilder result = new StringBuilder();
def callResult = null;

try {
callResult = gridFieldData.readFieldData(issueId, tgeCustomFieldId, applicationUser, null);
} catch (Exception e) {
log.error(e.getMessage())
}
//Date
def now = new Date().format("MM/dd/yyyy' 'HH:mm:ss");

//Key
def key = customFieldManager.getCustomFieldObject("customfield_12525");
def InvPickFieldValue = issue.getCustomFieldValue(key);
def InvPickFieldStringValue = InvPickFieldValue.toString();;

//Summary
def Summary = issue.summary;

//RedactedQTY
def cField = customFieldManager.getCustomFieldObject("customfield_12305");
def cFieldValue = issue.getCustomFieldValue(cField) as int;
def cFieldStringValue = cFieldValue.toString();


//cost
def cfm = ComponentAccessor.customFieldManager
def linkManager = ComponentAccessor.issueLinkManager
def costCF = cfm.getCustomFieldObject("customfield_12225")
def costFieldStringValue =""

linkManager.getOutwardLinks(issue.id).each{
def linkedIssue = it.destinationObject
def linkedIssueProject = linkedIssue.getProjectId()
def linkType = it.getIssueLinkType()
def linkedIssueCostValue = linkedIssue.getCustomFieldValue(costCF)
def pattern = "##,###.##"
def moneyform = new DecimalFormat(pattern)
String formattedVallue = moneyform.format(linkedIssueCostValue)
costFieldStringValue = formattedVallue + " "
}

// let's add some rows
Map<String, Object, Integer> row = new HashMap<String, Object, Integer>();
row.put("TransDate", now);
row.put("IssueKey", InvPickFieldStringValue);
row.put("IssueSummary", Summary);
row.put("RedactedQTY", cFieldStringValue);
row.put("Cost", costFieldStringValue);

//Script Runner can show validation errors here as well. Ignore them.
try {
List<Long> rowIds = gridFieldData.addRows(issue.getId(), tgeCustomFieldId, applicationUser, [row]);
result.append("Grid ID=" + tgeCustomFieldId + " data was successfully added. IDs of added rows: " + rowIds + "\n");
} catch (Exception e) {
log.error(e.getMessage())
}

return result;

 cost is only grabbing last linked issue custom field value.  the goal is to have it grab the linked issue custom field value of the issue selected in the issue picker field which is customfield_12525 

2 answers

1 accepted

0 votes
Answer accepted
//cost
def issuepicker = ComponentAccessor.getIssueManager().getIssueObject(InvPickFieldStringValue)
def costCF = customFieldManager.getCustomFieldObject("customfield_12225")
def cost = issuepicker.getCustomFieldValue(costCF)
def costStringValue = cost.toString();
def pattern2 = "##,###.##"
def moneyform = new DecimalFormat(pattern2)
moneyform.format(cost)

Hi @Eric 

I'm not familiar with the custom grid field you are working on and I could point a few other possible error in the script, but let's try to get straight to your issue of only getting the cost from the last issue link.

Looking at this block specifically:

linkManager.getOutwardLinks(issue.id).each{
def linkedIssue = it.destinationObject
def linkedIssueProject = linkedIssue.getProjectId()
def linkType = it.getIssueLinkType()
def linkedIssueCostValue = linkedIssue.getCustomFieldValue(costCF)
def pattern = "##,###.##"
def moneyform = new DecimalFormat(pattern)
String formattedVallue = moneyform.format(linkedIssueCostValue)
costFieldStringValue = formattedVallue + " "
}

With each itteration of each, you are completely overwriting the value of "costFieldStringValue".

I'm not clear what you hope to do with each of those costValues from each outward link or how you expect to use it.

If you just intend to concatenate them, then perhaps this will work:

costFieldStringValue = costFieldStringValue + formattedVallue + ' '

But if you are interested in a more groovy way to do this, you could try

costFieldStringValue = linkManager.getOutwardLinks(issue.id).collect{
def linkedIssue = it.destinationObject
def linkedIssueCostValue = linkedIssue.getCustomFieldValue(costCF)
def pattern = "##,###.##"
def moneyform = new DecimalFormat(pattern)
moneyform.format(linkedIssueCostValue)
}.join(' ')

Hi @Peter-Dave Sheehan

Thanks for your reply.

we are trying to just return one value from a specific linked issue. the field that determines witch issue is a Script runner Issue picker custom field. (customfield_12525)  

If you have an issue picker, why do you need the linked issue?

Why don't you just look up the cost value of the issue associated with the issue picker?

Like Eric likes this

That is what I'm trying to do. I'm just not sure how to. i was trying to do it with linked issue because that was the closest thing i could fined that was doing what i am trying to do. 

what would what your suggesting look like?

i have figured it out. 

//cost
def issuepicker = ComponentAccessor.getIssueManager().getIssueObject(InvPickFieldStringValue)
def costCF = customFieldManager.getCustomFieldObject("customfield_12225")
def cost = issuepicker.getCustomFieldValue(costCF)
def costStringValue = cost.toString();
def pattern2 = "##,###.##"
def moneyform = new DecimalFormat(pattern2)
moneyform.format(cost)

which returns the Issue pickers, Issue's custom field value.

Like Peter-Dave Sheehan likes this

Yeah, that's what I was going to suggest once I understood what you meant. You beat me to it.

@Peter-Dave Sheehan 

sorry i just started teaching myself this stuff. so i am learning as i go.. thank you for your help and pointing me in the right direction.

Suggest an answer

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

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

260 views 2 5
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