How to copy only new values from field to field?

Tomáš Vrabec May 12, 2021

Hello, got very specific use case and finding a lot of dead ends. 

I got two insight field, multi object field.

Lets say we have SourceField and TargetField.

The SourceField is present on transition screen.

The TargetField is hidden, just field for the logic behind. 

What I need is:

When I execute the transition, the SourceField already contain object(s). 

After I execute the transition I need to copy the objects into the TargetField, but only the new added values, during the transition. 

Cannot use the TargetField, as it is handled and updated by another scripted logic behind and could be even empty. 

Was thinking about originalIssue, but it doesnt work for me.

Any ideas?

Cheers, Tom

1 answer

0 votes
Tomáš Vrabec May 18, 2021

Here is my code I trying currently to run:

The general target is approval process, where manager can edit pending persons (represented by insight objects) by both add new and remove.

When adding, user or group should appear in PENDING field.

When removing, user or group should disappear from all PENDING, APPROVED and DECLINED. 

Its quite complex, but generally, what we need is:

Source Field have value [PERSON A]

During transition you will add to that custom field also PERSON B, so during transition it will look like [PERSON A, PERSON B]

When we need, that only PERSON B will be copied to the Target Field

What we have, it that both PERSON A and PERSON B are copied to Target Field.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;

def members = []

Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()

//--------------------------- MAIN ELEMENT DEFINITION ------
def fawi = issue.get("customfield_18201") // 1st approval wave individuals BUFFER
def fawiP = issue.get("customfield_18207") // 1st approval wave individuals Pending
def fawiA = issue.get("customfield_18209") // 1st approval wave individuals Approved
def fawiD = issue.get("customfield_18208") // 1st approval wave individuals Declined

def fawg = issue.get("customfield_18200") // 1st approval wave groups BUFFER
def fawgP = issue.get("customfield_18204") // 1st approval wave groups Pending
def fawgA = issue.get("customfield_18205") // 1st approval wave groups Approved
def fawgD = issue.get("customfield_18206") // 1st approval wave groups Declined

def sawi = issue.get("customfield_18203") // 2nd approval wave individuals BUFFER
def sawiP = issue.get("customfield_18213") // 2nd approval wave individuals Pending
def sawiA = issue.get("customfield_18214") // 2nd approval wave individuals Approved
def sawiD = issue.get("customfield_18215") // 2nd approval wave individuals Declined

def sawg = issue.get("customfield_18202") // 2nd approval wave groups BUFFER
def sawgP = issue.get("customfield_18210") // 2nd approval wave groups Pending
def sawgA = issue.get("customfield_18211") // 2nd approval wave groups Approved
def sawgD = issue.get("customfield_18212") // 2nd approval wave groups Declined


def sawiAcf = customFieldManager.getCustomFieldObject("customfield_18214")
def sawiDcf = customFieldManager.getCustomFieldObject("customfield_18215")
def sawiPcf = customFieldManager.getCustomFieldObject("customfield_18213")

def fawiAcf = customFieldManager.getCustomFieldObject("customfield_18209")
def fawiDcf = customFieldManager.getCustomFieldObject("customfield_18208")
def fawiPcf = customFieldManager.getCustomFieldObject("customfield_18207")

def sawgAcf = customFieldManager.getCustomFieldObject("customfield_18211")
def sawgDcf = customFieldManager.getCustomFieldObject("customfield_18212")
def sawgPcf = customFieldManager.getCustomFieldObject("customfield_18210")

def fawgAcf = customFieldManager.getCustomFieldObject("customfield_18205")
def fawgDcf = customFieldManager.getCustomFieldObject("customfield_18206")
def fawgPcf = customFieldManager.getCustomFieldObject("customfield_18204")
//--------------------------------

//--- FIRST WAVE GROUP CLEARANCE
members = []
members = [fawgP,fawgA,fawgD].flatten()  // Loading all decision makers
members = members.findAll()   

if (members == []) {members = null}
if (members){
  if (!(members?.sort() == fawg?.sort())) {  // Sort 
    if (fawg) {members = members?.minus(fawg)}  // Looking for removed decision makers
  fawgP?.removeAll(members)  // Removing removed DM
     fawgA?.removeAll(members)
        fawgD?.removeAll(members)
}}

if (fawgA == []) {fawgA = null}
fawgAcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fawgAcf), fawgA), changeHolder);  
if (fawgD == []) {fawgD = null}
fawgDcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fawgDcf), fawgD), changeHolder);   
if (fawgP == []) {fawgP = null}
fawgPcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fawgPcf), fawgP), changeHolder);     // zapísanie výsledkov späť do decision polí

//--- FIRST WAVE INDIVIDUALS CLEARANCE
members = []
members = [fawiP,fawiA,fawiD].flatten()
members = members.findAll()

if (members == []) {members = null}
if (members) {
  if (!(members?.sort() == fawi?.sort())) {
    if (fawi) {members = members?.minus(fawi)}
  fawiP?.removeAll(members)
     fawiA?.removeAll(members)
        fawiD?.removeAll(members)
}}

if (fawiA == []) {fawiA = null}
fawiAcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fawiAcf), fawiA), changeHolder);  
if (fawiD == []) {fawiD = null}
fawiDcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fawiDcf), fawiD), changeHolder);   
if (fawiP == []) {fawiP = null}
fawiPcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fawiPcf), fawiP), changeHolder);   

//--- SECOND WAVE INDIVIDUALS CLEARANCE
members = []
members = [sawiP,sawiA,sawiD].flatten()     
members = members.findAll()       

if (members == []) {members = null}
if (members){
  if (!(members?.sort() == sawi?.sort())) {   
    if (sawi) {members = members?.minus(sawi)}  
  sawiP?.removeAll(members)         
     sawiA?.removeAll(members)
        sawiD?.removeAll(members)

}}

if (sawiA == []) {sawiA = null}
sawiAcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sawiAcf), sawiA), changeHolder);   
if (sawiD == []) {sawiD = null}
sawiDcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sawiDcf), sawiD), changeHolder);  
if (sawiP == []) {sawiP = null}
sawiPcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sawiPcf), sawiP), changeHolder);  

//--- SECOND WAVE GROUP CLEARANCE
members = []
members = [sawgP,sawgA,sawgD].flatten()
members = members.findAll()

if (members == []) {members = null}
if (members){
  if (!(members?.sort() == sawg?.sort())) {
    if (sawg) {members = members?.minus(sawg)}
  sawgP?.removeAll(members)
     sawgA?.removeAll(members)
        sawgD?.removeAll(members)
}}

if (sawgA == []) {sawgA = null}
sawgAcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sawgAcf), sawgA), changeHolder);   
if (sawgD == []) {sawgD = null}
sawgDcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sawgDcf), sawgD), changeHolder);  
if (sawgP == []) {sawgP = null}
sawgPcf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sawgPcf), sawgP), changeHolder);  

Maybe @David Fischer ? Any ideas? I tried also JMWE copy field to field with append only new values, but did not get expected results. 

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 18, 2021

Hi @Tomáš Vrabec ,

first of all, if you're running this code inside a Scripted (Groovy) post-function, you should use JMWE's simplified API, especially when writing to the field (using the {{issue.setFieldValue}} method).

As for helping you, you'd need to post a minimal script that focuses on the problem at hand (identifying changes to the source field) instead of your long and complicated script... However, I don't actually see how you could get the previous value of your Insight custom field. Did you try using the issue history, and moving the post-function to the bottom of the list? Maybe Insight will write an entry in the issue history for its field, and you can thus see the change made by the user (between "from" and "to")? To access the issue history, you can use issue.getFieldHistory()

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events