Listener script needed for linked issues

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

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

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?

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.

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"

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.

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

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.

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.

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.

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.

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".

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

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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

498 views 6 11
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you