Listener script needed for linked issues

Scott Federman December 5, 2017

I have two issue types (project budget and order request) and they are linked using the "relates to" link type. Each project budget can have multiple order requests linked to it. The project Budget will contain a "Budget" Number field. The Order requests will have a "total order expense ($)" calculated number field. I was hoping someone could help me with a listener script that would sum up all of the linked order "total order expense ($ )" fields  in the order requests and subtract them from them from the "budget" field in the project budget and return a "Remaining budget" in the project budget issue type. 

Project budget - (total order expense+total order expense+total order expense....) = Remaining budget.

2 answers

1 accepted

0 votes
Answer accepted
Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2018
0 votes
Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2017

Hi Scott,

This should definitely be possible. A couple of things to get you started. Which event do you want to listen to? You mentioned a "listener script" but I feel like this might be better as a Script Field. We have an example in our documentation that would need a bit of tweaking but gives you the basics of dealing with linked issues:

https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html

I hope this helps!

Steve

Scott Federman December 5, 2017

Hi Steve!

 

Yes i think you're probably right. It should be a scripted field. I unfortunately have no idea how i should alter that script as i am new to this. Is it something you could help me with?

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2017

Hi Scott,

No problems! I've "sketched" out a script field script that should do what you need it to do:

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldMgr = ComponentAccessor.getCustomFieldManager()

def budgetField = customFieldMgr.getCustomFieldObjectByName("Budget")

// Get all of the linked issues for the project budget "issue"
def total = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "relates to") {
// Linked Issue is going to be your order request
def linkedIssue = issueLink.destinationObject
// Get the Budget number field
total += linkedIssue.getCustomFieldValue(budgetField) as // Specify type depending on the type of Budget
}
}

return total

You'll need to fill in the blanks and test it, like the type cast as mentioned in the comments. This will depend on the type of the budget custom field value.

It should definitely get you most of the way there though.

Scott Federman December 5, 2017

Ok so the budget field is a number field. I don't see anywhere where it is pulling the Total Order Expense ($) field, which would be a scripted field. and it looks like the link type is actually "Related"

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2017

My bad, I misread the description. I thought you were summing "Budget". I've updated the script to do the cast, it should also now be doing the correct arithmetic.

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldMgr = ComponentAccessor.getCustomFieldManager()

def budgetField = customFieldMgr.getCustomFieldObjectByName("Budget")
def oeField = customFieldMgr.getCustomFieldObjectByName("total order expense (\$ )")

// Get all of the linked issues for the project budget "issue"
def total = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Related") {
// Linked Issue is going to be your order request
def linkedIssue = issueLink.destinationObject
// Get the Budget number field
total += linkedIssue.getCustomFieldValue(oeField) as Double
}
}

// Now get the budget field value
def budgetVal = issue.getCustomFieldValue(budgetField) as Double

return budgetVal - total

I've also updated the link type.

Scott Federman December 5, 2017

Still no love. I updated the script below to match the field names as they appear in the system. what does "as Double" mean?

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldMgr = ComponentAccessor.getCustomFieldManager()

def budgetField = customFieldMgr.getCustomFieldObjectByName("Budget")
def oeField = customFieldMgr.getCustomFieldObjectByName("Total Order Expense ($)")

// Get all of the linked issues for the project budget "issue"
def total = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Related") {
// Linked Issue is going to be your order request
def linkedIssue = issueLink.destinationObject
// Get the Budget number field
total += linkedIssue.getCustomFieldValue(oeField) as Double
}
}

// Now get the budget field value
def budgetVal = issue.getCustomFieldValue(budgetField) as Double

return budgetVal - total

Scott Federman December 5, 2017

nope. Im summing the "Total Order Expense ($)" in the Order request and then subtracting that from the "Budget" in the Project Budget to return the "Remaining Budget" in the Project Budget.

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 5, 2017

nope. Im summing the "Total Order Expense ($)" in the Order request and then subtracting that from the "Budget" in the Project Budget to return the "Remaining Budget" in the Project Budget.

The last piece of code I gave you is adjusted for this already.

Still no love. I updated the script below to match the field names as they appear in the system. what does "as Double" mean?

Again, I already did this for you. When you say "still no love" what do you mean? Can you give me a better idea of what's broken.

P.S. The "Remaining Budget" field is this custom field that we are trying to create. You can use the context configuration to control which issue types we see this field on.

Scott Federman December 5, 2017

Sure and i really appreciate your time and help. So ill create a project budget issue and assign a number in the project budget field. 

Then ill create and link s a few order requests with the total order expense field populated. 

At this point i am looking to go back to the project budget and see the remaining budget field populated with the budget number less the total of the expenses from the attached orders. However nothing populated.

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2017

Hey Scott,

When you say that "nothing populated", does the field appear on the issue, but there is no value, or is the field not there at all.

You can test the field setup by writing a script that just returns something basic like "return 2-1" to test that the field setup is working as expected. If that works then we can work back from there.

Scott Federman December 6, 2017

Hey Stephen, 

 

Sorry for the delay. What you said is right. The Remaining Budget field doesn't show up on the screen when i use the script. However when i use "return 2 - 1" the remaining budget field appears it and returns "1".

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 7, 2017

Do you see any errors in the log tab when you run the script?

Scott Federman December 7, 2017

yes i get "groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: Script115" as the error. 

Stephen Cheesley _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2017

Ahhh, you need to add an import statement at the top of the script:

import com.atlassian.jira.component.ComponentAccessor

Suggest an answer

Log in or Sign up to answer