Hi All,
Here is my requirement -
I have a Group Picker field that has a value. I want to set the Comment field to required when someone changes the value.
My initial thought is to get the current value of the field and then compare it to the new value and then set Comment to required if true. So for example, if the field had the value "Foo" and someone edited it to "Bar" then set Comment to required. If the value didn't change then do not set Comment as required.
Here is the code I have currently -
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.*
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField = customFieldManager.getCustomFieldObjectByName("Group Field")
IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issue = (MutableIssue) underlyingIssue;
Issue originalIssue = issueManager.getIssueObject(issue.id)
def oldValue = originalIssue.getCustomFieldValue(customField)
def customFieldOnForm = getFieldByName("Group Field")
def newValue = customFieldOnForm.getValue()
log.error "[DEBUG] oldValue is :: " +oldValue
log.error "[DEBUG] newValue is :: " +newValue
Output:
/rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours][DEBUG] oldValue is :: [com.atlassian.crowd.embedded.impl.ImmutableGroup@9acac80d]
2018-03-22 14:25:55,096 http-nio-7999-exec-19 ERROR <> 865x332419x3 duah9u 139.49.147.10,10.73.139.2 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] [DEBUG] newValue is :: FOO
I would have expected oldValue to be FOO and newValue to be what I set it to from the Edit screen.
Any suggestions?
Thanks
This code worked for me -
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.*
import com.atlassian.jira.component.ComponentAccessor
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField = customFieldManager.getCustomFieldObjectByName("<Single Group Picker Field>")
IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issue = (MutableIssue) underlyingIssue;
Issue originalIssue = issueManager.getIssueObject(issue.id)
def customFieldOnForm = getFieldByName("<Single Group Picker Field>")
def oldValue = underlyingIssue.getCustomFieldValue(customField)*.name as Set
def newValue = customFieldOnForm.getValue().split(",")*.trim() as Set
def commentField = getFieldById("comment")
if (oldValue != newValue)
{
log.error "[INFO] oldValue " +oldValue +" is not equal to the newValue " +newValue +".Setting comment field to required."
commentField.setRequired(true)
}
I am getting static type checking error on below line
def oldValue = underlyingIssue.getCustomFieldValue(customField)*.name as Set
Thanks,
Prashant
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ignore the static type checking error in the editor and try it out. It worked for me.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Srinath
It is working for me too.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Make sure that you attached this script to the "Group Field" custom field behaviour. Not to the initializing script.
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.
It is strange. getValue takes the value from the screen. And if you choose another value, it will take it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So if I have a value of "Foo" on the screen getValue should return "Foo" right? If I change it to "Bar" then how do I get that value and compare it to "Foo" and make the Comment field required?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That is right. Your code would look like this
def commentField = getFieldById("comment")
if (oldValue != newValue) {
commentField.setRequired(true)
} else {
commentField.setRequired(false)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes that is what I am trying to achieve but the oldValue and newValue are not returning the values I expect. Any suggestions?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What values were you expecting? (I'd expect two group objects from them)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am expecting oldValue to hold the current value in the field and I want to compare it to some newValue that the user sets it to and then make the Comment field required.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And what do they contain?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Assuming FOO is what is on the field this is what I get -
/rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours][DEBUG] oldValue is :: [com.atlassian.crowd.embedded.impl.ImmutableGroup@9acac80d]
2018-03-22 14:25:55,096 http-nio-7999-exec-19 ERROR <> 865x332419x3 duah9u 139.49.147.10,10.73.139.2 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.j.groovy.user.FieldBehaviours] [DEBUG] newValue is :: FOO
My code-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.*
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField = customFieldManager.getCustomFieldObjectByName("Group Field")
IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issue = (MutableIssue) underlyingIssue;
Issue originalIssue = issueManager.getIssueObject(issue.id)
def oldValue = originalIssue.getCustomFieldValue(customField)
def customFieldOnForm = getFieldByName("Group Field")
def newValue = customFieldOnForm.getValue()
log.error "[DEBUG] oldValue is :: " +oldValue
log.error "[DEBUG] newValue is :: " +newValue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's what I expected - the current value is an group object. Try adding .getName() to the code that gets it from the issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nic,
I have a same issue. I got that current value is an group object. but where to add .getName()
Can you please explain me in detail.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is this being run as a listener?
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.