Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

275 views 8 7
Read article

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