Unable to set the first user group name in the scripted field.

Muddassir Quazi January 28, 2022

Hi,

I want to fetch what was the first group assignee on the issue. Group assignee is populated in a single group picker custom field.

For this this code was executed but couldn't get the desired result.

What could be missing in this groovy script? Could you help over this?

The script is as below

import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def firstAssignee=changeHistoryManager.getChangeItemsForField(issue, "Group Assignee")

log.warn "firstAssignee = "+firstAssignee
//def firstNotNullAssignee = firstAssignee[0]?.from ?: firstAssignee[0]?.to

return firstAssignee
//issue.setAssigneeId(firstAssignee)

//def ApplicationUser = ComponentAccessor.getGroupManager().getGroup(firstNotNullAssignee.toString())
//return firstNotNullAssignee

//def usertest = ComponentAccessor.getGroupManager().getGroup()

firstgroupassignment.png

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2022

Hi

This code:

changeHistoryManager.getChangeItemsForField(issue, "Group Assignee")

will return a List of ChangeItem Beans.  That means all the change items for this "Group Assignee". 

https://docs.atlassian.com/software/jira/docs/api/8.13.15/com/atlassian/jira/issue/changehistory/ChangeHistoryManager.html#getChangeItemsForField-com.atlassian.jira.issue.Issue-java.lang.String-

This shows that the order of the list is from older to the newest, we can take the first item and know it's the first group assignee (note that if the group assignee was set when creating the issue, there will be no history for it). 

Here is a script that should work in all the cases:

import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def assigneeHistory=changeHistoryManager.getChangeItemsForField(issue, "Group Assignee")

def firstAssignee

if(assigneeHistory){
def firstAssigneeHistory = assigneeHistory.first()
if(firstAssigneeHistory.fromString == ""){
//assignee was change from nothing to first assignee
firstAssignee = firstAssigneeHistory.toString
log.info "firstAssignee[$issue.key]: issue first updated 'Group Assignee': $firstAssignee"
} else {
//assignee was change from initial value during create to another.
firstAssignee = firstAssigneeHistory.fromString
log.info "firstAssignee[$issue.key]: issue initially created with 'Group Assignee': $firstAssignee"
}
} else {
//there are no change history for the Group Assignee, check if there is a value currently
def groupAssigneeCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Group Assignee')[0]
def groupAssigneeVal = issue.getCustomFieldValue(groupAssigneeCf)
if(groupAssigneeVal){
firstAssignee = groupAssigneeVal.name
log.info "firstAssignee[$issue.key]: current 'Group Assignee' is first: $firstAssignee"
} else {
log.info "firstAssignee[$issue.key]: 'Group Assignee' was never populared"
}
}

return firstAssignee

This seemed to be in a scripted field.
I should share that I'm not a fan of using a scripted field to get and display static data from the Change History. Especially in a case like this where you want the first value. Getting the last value might be a better reason to use the script field since that might change every day. But the first, by definition, will only be set once. So paying the burden of a script that has to run every time you access that issue forever and always returning that save value seems wasteful to me. I'd use a listener to detect the change in the Group ASsignee field and set a regular custom field with that value the first time it is set and never have to touch it again.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 24, 2022

I think you have all the clues you need in my original answer.

You are using "getFromString()" on the change item. That's the value BEFORE the change. So by definition, it will be null when change from nothing to something.

So try to use getToString() instead.

Muddassir Quazi April 25, 2022

Hi Peter,

 

Thanks for the help!

Clue provided works to get the value.



def firstNotNullAssignee = firstAssignee[0]?.getFromString()?:firstAssignee[0]?.getToString()

return firstNotNullAssignee

 

Regards,

Muddassir Quazi

0 votes
Muddassir Quazi April 24, 2022

Hi Peter,

Thanks for the help with the script.

But the problem with the original script is it is giving a null value.

Whenever the field is empty at the first place it is giving a null value.

The logic that is required is supposed to be, give the first populated value for this field.

And that is what required. If it is null at the first place give first populated value then.

firstgroupassignment.pngfirstgroupassignment1.png

logs:

2022-04-24 15:29:01,422 WARN [runner.ScriptBindingsManager]: firstAssignee = [com.atlassian.jira.issue.history.ChangeItemBean@1a9c0311[fieldType=custom,field=Group Assignee,from=<null>,fromString=<null>,to=<null>,toString=[DLP_Africa],created=2022-04-19 14:34:51.952], com.atlassian.jira.issue.history.ChangeItemBean@42e82229[fieldType=custom,field=Group Assignee,from=<null>,fromString=[DLP_Africa],to=<null>,toString=[Apps _LMS],created=2022-04-19 14:36:00.394], com.atlassian.jira.issue.history.ChangeItemBean@6d7b89db[fieldType=custom,field=Group Assignee,from=<null>,fromString=[Apps _LMS],to=<null>,toString=[Infra_Backup],created=2022-04-19 14:37:17.81]]

 

What would be the required step here?

Regards,

Muddassir Quazi

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events