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.
This has now been answered / resolved on:
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:
I hope this helps!
Steve
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you see any errors in the log tab when you run the script?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
yes i get "groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: Script115" as the error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ahhh, you need to add an import statement at the top of the script:
import com.atlassian.jira.component.ComponentAccessor
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.