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
Hi @Neta Elyakim , I use changelog event, oldstring and newstring to capture the value of oldstring if the newvalue is equal to null. :)
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Neta Elyakim , it does not get the support group back when I set it to null
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this is the log when I use log.warn(groupsToSet)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Those are not errors, it's the log you print..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The problem was with
.replaceAll(" ","")
If you want to use the script, just remove it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.