When an issue is edited via the issue UI's inline edit, the custom field does not sync

Ashok Barik
Contributor
March 27, 2022

When an issue is edited via the issue UI's inline edit, the custom field does not sync with the updated value, I have to hit the refresh button everytime to reflect the value. 

I have thee fields.
Field-A - Expenses
Field-B - Total Expenses
Field-C - Remaining amount.   (This is a scripted field with a script listener function that subtracts the value from the Total expenses)

In my case, when I enter a number in Field-A, that value sums with Field B and changes instantly, however, the remaining amount does not. I have to refresh the screen every time I want to check the value.

Could someone possibly assist me in obtaining a listener or behavior script to accomplish this?

Thanks in advance. 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2022

Hi @Ashok Barik

This question is similar to the question you asked earlier, i.e. to get the value for the Total Amount spent.

Could you please share your code for the Roaming Amount scripted field so that I can provide some feedback and suggestion on how to modify the code?

Thank you and Kind regards,

Ram

Ashok Barik
Contributor
March 27, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ Thank you for looking into my request. I was able to fix my last requirement and also I have updated that at https://community.atlassian.com/t5/Jira-questions/Need-a-script-to-calculate-the-sum-of-a-custom-field-with-the/qaq-p/1983996#M526420

 

My case is different here,  the field value never syncs the value until I hit the refresh button. 

Field Field-A - Expenses and Field-B - Total Expenses are working very well but the Remaining amount doesn't show the updated value. I always press the refresh button two to three times to reflect it. 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 28, 2022

Hi @Ashok Barik

If you require the field to be updated immediately, using Listener is not the best approach.

Instead, I showed in the 

https://community.atlassian.com/t5/Jira-questions/Need-a-script-to-calculate-the-sum-of-a-custom-field-with-the/qaq-p/1983996#M526420 comment, you should use the Scripted Field.


Thank you and Kind regards,
Ram

Ashok Barik
Contributor
March 28, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

I had previously used a scripted field, but it did not function for me, therefore I had to relocate Listener as suggested by some Jira experts.

In your suggestion, you display the sum value on the third field, however, in my situation, I have to show it on the second field itself after adding it to field1.

 

Could you please help me to update a field when another field is changed? 

Note: update field is a calculation scripted field. 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 29, 2022

Hi @Ashok Barik

This is still doable in the Scripted Field.

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def changeHistoryManager = ComponentAccessor.changeHistoryManager

def sampleNumber = customFieldManager.getCustomFieldObjectsByName('Sample Number 1').first()
def sampleNumberValue = sampleNumber.getValue(issue)

def ch = changeHistoryManager.getChangeHistories(issue)

def output = 0 as Long

def accumulatedValue = [] as ArrayList<Long>

if ( sampleNumberValue == null ) {
accumulatedValue.add(output)
} else if (ch.size() > 0) {
(0..ch.size()-1).each { i ->
ch.get(i).getChangeItemBeans().each {
if (it.field == 'Sample Number 1') {

if(it['toString'].toString() == '') {
output
} else {
output = Long.parseLong(it['toString'].toString())
}
accumulatedValue.add(output)
}
}
}
}
accumulatedValue.sum()

Please note, this sample code is not 100% exact to your environment. Hence, you will need to modify it accordingly. 

The primary item from which the previous value of the number field is extracted is the ChangeHistoryManager, i.e.

def changeHistoryManager = ComponentAccessor.changeHistoryManager

This is required to get all the number field values, previous and current, and sum it up.

When the issue is first created, and if no value is set to the number field, i.e. Sample Number 1, it will implicitly set the Scripted Field value to 0.

Whenever the Sample Number 1 field is updated, i.e. a numeric value is added to it, it will check the history of the values added to the number field and sum it up add, and that total will be added to the Scripted Field.

For example, if the value 1 is added for the first time to the Sample Number 1 field, the Scripted Fiel will be set to 1 also. If the Sample Number 1 field is updated once again, for example, to 2, the Scripted Field will take the previous value of the Sample Number 1 field and add it to the current value, i.e. 1 + 2, and it will display 3.

Below are a few print screens for your reference:-

1) When the issue is created, the first test is to create it without adding any value to the Sample Number 1 field.

image1.png

2) As expected, although the Sample Number 1 field is empty, the Scripted Field, i.e. Total display the value zero, i.e. 0.

image2.png

3) Next Sample Number 1 field is updated, and a value is added.

image3.png

4) As expected, the Total field is also updated displaying the current value of the Sample Number Field.

image4.png

5) The Sample Number 1 field is updated once again and its value is updated to 4.image5.png

6) As expected the Total field is updated once again, i.e. to 5, i.e. the previous value(s) of the Sample Number 1 field added to the current value of the Sample Number 1 field.image6.png

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

Ashok Barik
Contributor
March 29, 2022

Thank you very much! @Ram Kumar Aravindakshan _Adaptavist_this is exactly what I wanted. 

Ashok Barik
Contributor
March 30, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

In my requirement, I have two functions.

First :-> The money spent value (field1) is added to the total spent value (field2), which is now functioning thanks to your assistance.

Second :-> the "Remaining amount" field displays the subtraction value from the "Category – Predefined (as mentioned in the screenshot)" value and the "Field2 – total spent" value.

 

For example, if our Category value - [Concept-M] is 48 and our Total spent is 24, the remaining amount is –> 24 (total spent) – 48 (category) = 24

 

Jira-funcation.PNG

 

[Note: Category field is a cascading field]

 

My issue is that the "Remaining amount" does not appear on the edit screen. If the value of Field2 (total spent) changes after adding any value to Field1. To view the changes in the Remaining amount, I have to refresh the edit screen every time.

 

I tried merging both scripts into one, but it still does not work for "Remaining amount" updating.

I tried merging both scripts into one, but it still does not work for "Remaining amount" updating.

Could you kindly assist me in merging both scripts into one to ensure that all fields work properly?

 

------------

Script -1

-----------

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def changeHistoryManager = ComponentAccessor.changeHistoryManager

def sampleNumber = customFieldManager.getCustomFieldObjectsByName('Field1').first()
def sampleNumberValue = sampleNumber.getValue(issue)

def ch = changeHistoryManager.getChangeHistories(issue)

def output = 0 as Long

def accumulatedValue = [] as ArrayList<Long>

if ( sampleNumberValue == null ) {
    accumulatedValue.add(output)
} else if (ch.size() > 0) {
    (0..ch.size()-1).each { i ->
        ch.get(i).getChangeItemBeans().each {
            if (it.field == 'Field1') {

                if(it['toString'].toString() == '') {
                    output
                } else {
                    output = Long.parseLong(it['toString'].toString())
                }
                accumulatedValue.add(output)
            }
        }
    }
}
accumulatedValue.sum()

 

 

------------------

Script -2 

------------------

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.MutableIssue

import com.opensymphony.workflow.InvalidInputException

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.IssueManager;

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.link.IssueLinkManager

import com.atlassian.jira.user.ApplicationUser

 

def issueManager = ComponentAccessor.getIssueManager()

def linkManager = ComponentAccessor.getIssueLinkManager()

def cfm = ComponentAccessor.getCustomFieldManager()

 

Issue issueKey = issue

def id=issueKey.getId()

 

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def totalValue = customFieldManager.getCustomFieldObjectByName("Field2") // This is Total spent field.  

def issue = issue as MutableIssue

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName ("Category") // The Category cascading field. 

def cfValue = issue.getCustomFieldValue(cf) as HashMap

 def n = cfValue?.get(null).toString()

def b = cfValue?.getAt("1").toString()

 

if (!b) {

throw new InvalidInputException("Error in field '" + myField + "': the second part (region) cannot be empty")  // I tried this funcaiton but it is not working - if you could fix then it would be a great help. 

}

 

def c = Integer.parseInt((issue.getCustomFieldValue(totalValue) ?: 0).toString().replaceAll(~/[.].*/, ""))

def d = 0;

if(n=="CONCEPT" && b =="M"){ d = 48 }

if(n=="CONCEPT" && b =="M"){ d = 48 }

if(n=="CONCEPT" && b =="L"){ d = 96 }

if(n=="CONCEPT" && b =="XL"){ d = 192 }

if(n=="CONCEPT" && b =="XXL"){ d = 384 }

 

Integer remaining = d - c

return remaining

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 30, 2022

Hi @Ashok Barik

You cannot merge multiple scripted fields code into one script.

Each scripted field must have its code.

You can, however, invoke one Scripted Field in another Scripted Field's code.

You mentioned:-

My issue is that the "Remaining amount" does not appear on the edit screen. If the value of Field2 (total spent) changes after adding any value to Field1. To view the changes in the Remaining amount, I have to refresh the edit screen every time.

What field are you using to determine the original amount before doing the subtraction to get the remaining amount?

Thank you and Kind regards,

Ram

Ashok Barik
Contributor
March 30, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ , 

I apologize for the typo in my sentence. The Field "Remaining amount" doesn't reflect the updated value until I hit the refresh button. 

The "Remaining amount" is the calculation subtracts value field. 

Remaining amount = "Category" - "Field2 (Total spent)" {Kindly check my last attached screenshot}

 

If I change the value in Category then the "Remaining amount" subtracts  the"Total spent" value from Category then reflects in the Remaining field [ IT IS WORKING]

 

If I add any number in the "Field1 (Money spent)" then "Field2 value adds with field1" then reflect the updated value on "field2 (Total spent)" (Your script and IT IS WORKING)

Now that the "Total spent" value has changed, the "Remaining amount" should change as well, but  IT DOESN'T. Every time I refresh the screen to see the change value. 

Ashok Barik
Contributor
March 30, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ ,

I have merged both scripts into one and configured it in the Listener and IT IS WORKING now. :)

Thanks for your support. 

Suggest an answer

Log in or Sign up to answer