Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy: Setting Multiple Objects Insight Custom Field

Tomáš Vrabec
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.
October 7, 2020

Hi Community, 

I am trying to fill in Insight object(s) custom field, which is configured as multiple.

Here is the code:

if (members != null) 

{

for (item in members)

{

        def userInsightField = customFieldManager.getCustomFieldObject("customfield_11525")   // TSO members (Insight)

        Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade")

  def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass)

  def member = iqlFacade.findObjectsByIQLAndSchema(3,"\"User Jira Key\" = " + item.key) // Schema 3 = Customer Directory         

          if (issue.getCustomFieldValue(userInsightField) == null) 

          {userInsightField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(userInsightField), member), changeHolder)} 

          else 

          {userInsightField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(userInsightField), issue.getCustomFieldValue(userInsightField) + member), changeHolder)}

//return issue.getCustomFieldValue(userInsightField)

        }

In members, there is an ArrayList of DelegatingApplicationUser

In the return section, I am getting two objects

When executed in post function, only one object is in the field. 

Was search for solution for hours, but no success, please, help me out :-) 

2 answers

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 12, 2020 edited

hi @Tomáš Vrabec as @PD Sheehan suggested you have to use issue.setCustomFieldValue method otherwise it is still NULL when the code is in second, third... loop. It could be

def emails = ['alex.hrabal@metrostavfake.cz', 'frantisek.silny@cetinfake.cz']
for(def item in emails){
def member = iqlFacade.findObjectsByIQLAndSchema(1,"\"Email\" = " + item)
if (issue.getCustomFieldValue(userInsightField) == null) {
issue.setCustomFieldValue(userInsightField, member) // you have to invoke this, otherwise issue.getCustomFieldValue is still null
log.error("null:"+item)
userInsightField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(userInsightField), member), changeHolder)
} else {
log.error("not null:"+item)
def oldValue = issue.getCustomFieldValue(userInsightField)
def newValue = oldValue + member
issue.setCustomFieldValue(userInsightField, newValue)
userInsightField.updateValue(null, issue, new ModifiedValue(oldValue, newValue), changeHolder)
}
}

But I would try to optimize it and not to set the field with value by value but you should create collection of Insight objects and set it at once. In my case:

def emails = ['alex.hrabal@metrostavfake.cz', 'frantisek.silny@cetinfake.cz']
def insightObjects = []
for(def item in emails){
def member = iqlFacade.findObjectsByIQLAndSchema(1,"\"Email\" = " + item)
if(member){
member = member.get(0)
insightObjects.add(member)
}
}
def oldValue = issue.getCustomFieldValue(userInsightField)
def newValue = oldValue!=null ? new ArrayList(oldValue):[]
insightObjects.each{newValue.add(it)}

issue.setCustomFieldValue(userInsightField, newValue)
userInsightField.updateValue(null, issue, new ModifiedValue(oldValue, newValue), changeHolder)
Tomáš Vrabec
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.
October 14, 2020

Ahoj @Martin Bayer _MoroSystems_ s_r_o__ 

My final version looks like this:

if (members != null) 

{

    def userInsightField = customFieldManager.getCustomFieldObject("customfield_11525")   // TSO members (Insight)

      for(item in members)

      {

def member = iqlFacade.findObjectsByIQLAndSchema(3,"\"User Jira Key\" = " + item.key)

if(member)

          {

member = member.get(0)

insightObjects.add(member)

}

}

def oldValue = issue.getCustomFieldValue(userInsightField)

def newValue = oldValue!=null ? new ArrayList(oldValue):[]

insightObjects.each{newValue.add(it)}

issue.setCustomFieldValue(userInsightField, newValue)

userInsightField.updateValue(null, issue, new ModifiedValue(oldValue, newValue), changeHolder)

}

But yes, it does work! Thanks for the tip with oldValue and newValue, this is exactly what I was looking for! Kudos and accepted answer for you!

0 votes
PD Sheehan
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.
October 7, 2020

I'm not a fan of the customFieldObject.updateValue() method in general because I don't think it will appear in the issue history.

But in your specific case, I would guess that the value you are setting with this low-level api is being updated by the temporary issue object values (which are not updated after you update the custom field value) that the postfunction is holding in memory.

Instead, I would recommend you use the issue.setCustomFieldValue() or issueInputParameters.addCustomFieldValue() methods. Then the rest of the postfunction mechanism will take care of storing those values.

Another approach is to move your post function to the end of the post function sequence (after the store issue object function). 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events