Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting current value of a Group Picker field and setting required field

Srinath Anand March 23, 2018

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

3 answers

1 vote
Srinath Anand April 3, 2018

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)

}

Prashant Mali
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.
April 4, 2018

I am getting static type checking error on below line

def oldValue = underlyingIssue.getCustomFieldValue(customField)*.name as Set

 

Thanks,

Prashant

Srinath Anand April 4, 2018

Ignore the static type checking error in the editor and try it out. It worked for me.

Prashant Mali
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.
April 4, 2018

Thanks Srinath

It is working for me too.

0 votes
Alexey Matveev
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.
March 23, 2018

Make sure that you attached this script to the "Group Field" custom field behaviour. Not to the initializing script.

Srinath Anand March 23, 2018

Yes, it is attached to the "Group Field".

Alexey Matveev
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.
March 23, 2018

It is strange. getValue takes the value from the screen. And if you choose another value, it will take it.

Srinath Anand March 23, 2018

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?

Alexey Matveev
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.
March 23, 2018

That is right. Your code would look like this

def commentField = getFieldById("comment")

if (oldValue != newValue) {

 commentField.setRequired(true)

} else {

 commentField.setRequired(false)

}
Srinath Anand March 24, 2018

Yes that is what I am trying to achieve but the oldValue and newValue are not returning the values I expect. Any suggestions?

Nic Brough -Adaptavist-
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.
March 24, 2018

What values were you expecting?  (I'd expect two group objects from them)

Srinath Anand March 24, 2018

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.

Nic Brough -Adaptavist-
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.
March 25, 2018

And what do they contain?

Srinath Anand March 25, 2018

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

Nic Brough -Adaptavist-
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.
March 25, 2018

That's what I expected - the current value is an group object.  Try adding .getName() to the code that gets it from the issue.

Prashant Mali
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.
April 3, 2018

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.

0 votes
Gezim Shehu [Communardo]
Community Champion
March 23, 2018

Is this being run as a listener?

Srinath Anand March 23, 2018

Behaviour Script

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events