Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy: Setting Multiple Objects Insight Custom Field

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

hi @Tomáš Vrabec as @Peter-Dave 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)

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

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
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

4,567 views 12 8
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you