Set back the previous value of group picker if it is set as null upon editing using listener

Alvin
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.
November 11, 2018

Hi All, 

How can I revert back the previous value of group picker if it is set to null upon editing using listener? I am currently experimenting this code but no luck

Scriptrunner Listener

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.crowd.embedded.api.Group

MutableIssue issueToUpdate = (MutableIssue) event.issue;

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Group Picker")
def modifiedValue = (List <Group>)event.issue.getCustomFieldValue(customField)

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == customField}
def changeHolder = new DefaultIssueChangeHolder()
for(Group group:modifiedValue){
log.warn "AA " +group.getName()
if (!modifiedValue) {
tgtField.updateValue(null, issue, new ModifiedValue((List <Group>)issue.getCustomFieldValue(customField), modifiedValue),changeHolder)
}
}
log.warn "BB " +tgtField

 

please help. thanks

2 answers

1 accepted

0 votes
Answer accepted
Alvin
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.
November 13, 2018

Hi @Neta Elyakim , I use changelog event, oldstring and newstring to capture the value of oldstring if the newvalue is equal to null. :)

1 vote
Neta Elyakim
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.
November 13, 2018

Try this:

Please note that you need to change the field id in the "customField" param.

Also, in the "lastFieldChange" param, use the field name as a string

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.event.type.EventDispatchOption

MutableIssue issue = event.issue as MutableIssue
def currentUser = event.user

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def issueManager = ComponentAccessor.getIssueManager()

def customField = customFieldManager.getCustomFieldObject(11001 as long)//Enter Group Picker id
def customFieldValue = issue.getCustomFieldValue(customField)

if(customFieldValue == null){

def lastFieldChange = changeHistoryManager.getChangeItemsForField(issue, "Group Picker")?.last()

String valuesToSet = lastFieldChange.fromString
List<String> valuesAsList = valuesToSet.substring(1,valuesToSet.size() -1).split(" ,") as List<String>
List<Group> groupsToSet = new ArrayList<Group>()

valuesAsList.each{
groupsToSet.add(groupManager.getGroup(it))
}

if(groupsToSet){
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), groupsToSet),new DefaultIssueChangeHolder())
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

 Let me know if it works for you.

Alvin
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.
November 13, 2018

Hi @Neta Elyakim , it does not get the support group back when I set it to null

Alvin
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.
November 13, 2018

I tried to change all "params"

def customField = customFieldManager.getCustomFieldObject(10472 as long)//Enter Group Picker id 

 and 

def customField = customFieldManager.getCustomFieldObject(customfield_10472 as long)//Enter Group Picker id 

and

def customField = customFieldManager.getCustomFieldObject("customfield_10472" as long)//Enter Group Picker id 

 and it does nothing

Neta Elyakim
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.
November 13, 2018

The first one should work for you :
https://docs.atlassian.com/software/jira/docs/api/7.2.1/com/atlassian/jira/issue/CustomFieldManager.html

def customField = customFieldManager.getCustomFieldObject(10472 as long)//Enter Group Picker id 

You can add logs to your script and see what is the problem.

Also, you can try to get the custom field form Script Console, return the field and if you see that you get a valid field object (you will see the field name), you will know that this line is working.

Alvin
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.
November 13, 2018

Screenshot from 2018-11-14 15_34_46.png

this is the log when I use log.warn(groupsToSet)

Alvin
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.
November 13, 2018
Neta Elyakim
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.
November 13, 2018

Where you locate the log in the script? and is your group picker field is multi or single?

Also, try to return each line (with the logs or from script console) and see where your problem is.

Alvin
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.
November 13, 2018

Hi @Neta Elyakim, I put it here

if(customFieldValue == null){

def lastFieldChange = changeHistoryManager.getChangeItemsForField(issue, "Group Picker")?.last()

String valuesToSet = lastFieldChange.fromString
List<String> valuesAsList = valuesToSet.substring(1,valuesToSet.size() -1).replaceAll(" ","").split(",") as List<String>
List<Group> groupsToSet = new ArrayList<Group>()

valuesAsList.each{
groupsToSet.add(groupManager.getGroup(it))
}
log.warn(groupsToSet)
if(groupsToSet){
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), groupsToSet),new DefaultIssueChangeHolder())
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.warn(groupsToset)
}
}

My field is a single group picker field

Neta Elyakim
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.
November 14, 2018

My code is for multi values, change the script in the "if(customFieldValue == null){}" to :

def lastFieldChange = changeHistoryManager.getChangeItemsForField(issue, "Group Picker")?.last()

String valuesToSet = lastFieldChange.fromString
String cleanValue = valuesToSet.substring(1,valuesToSet.size() -1)//.split(",") as List<String>
List<Group> groupsToSet = new ArrayList<Group>()
groupsToSet.add(groupManager.getGroup(cleanValue))

if(groupsToSet){
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), groupsToSet),new DefaultIssueChangeHolder())
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Alvin
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.
November 14, 2018

Hi @Neta Elyakim , I removed this line

if(customFieldValue == null){


}

 still it can't retain the value of the group picker field when I set it to null

Alvin
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.
November 14, 2018

Hi @Neta Elyakim , this is the warning

 

2018-11-14 08:15:40,858 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-11-14 08:15:40,858 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.util.NoSuchElementException: Cannot access last() element from an empty List
 at Script3534.run(Script3534.groovy:20)
Neta Elyakim
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.
November 14, 2018

Don't remove those lines:

if(customFieldValue == null){

}

Just change the script between those lines 

if(customFieldValue == null){
def lastFieldChange = changeHistoryManager.getChangeItemsForField(issue, "Group Picker")
if(lastFieldChange != null){
lastFieldChange = lastFieldChange.last()

String valuesToSet = lastFieldChange.fromString
String cleanValue = valuesToSet.substring(1,valuesToSet.size() -1)
List<Group> groupsToSet = new ArrayList<Group>()
groupsToSet.add(groupManager.getGroup(cleanValue))

if(groupsToSet){
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), groupsToSet),new DefaultIssueChangeHolder())
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}
Alvin
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.
November 14, 2018

Hi @Neta Elyakim , found these errors 

2018-11-14 08:37:28,664 WARN [runner.ScriptRunnerImpl]: com.atlassian.jira.issue.history.ChangeItemBean@733cb382[fieldType=custom,field=Support Group,from=<null>,fromString=[Sample Group Only],to=<null>,toString=,created=2018-11-14 08:37:28.407]
2018-11-14 08:37:28,664 WARN [runner.ScriptRunnerImpl]: [null]
2018-11-14 08:37:28,667 WARN [runner.ScriptRunnerImpl]: Support Group
Neta Elyakim
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.
November 14, 2018

Those are not errors, it's the log you print..

Alvin
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.
November 14, 2018

Hi @Neta Elyakim , yes it's the logs. by the way I have resolved this problem yesterday using changelog event. using oldstring value and newstring value, and compare them both. and if it is not equal to newstring, then retain oldstring using changeholder

Neta Elyakim
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.
November 15, 2018

The problem was with 

.replaceAll(" ","")

If you want to use the script, just remove it.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events