Forums

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

Groovy script should run when assignee gets changed

Priyanka Nimbalkar May 12, 2020

Hi Team,

We are working on the case where whenever the ticket of this project is updated, let's say Field tower should be made  mandate whenever any changes made by JIRA user in Assignee field.

So whenever user changes assignee of the ticket , the field tower should be made mandatory, so that even if users forgets to update field tower and clicks update , a pop will arise saying provide value for mandatory field. In our case it's already mandate and tried to run this groovy script as well but doesn't seem to work :

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import org.apache.log4j.Logger
import org.apache.log4j.Level
//Get the field tower for update
def TowerValue = getFieldById("Tower");
//Grab Assignee in edit view
def AssigneeName = getFieldById(getFieldChanged());
//grab current Assignee from database
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField currentAssignee = customFieldManager.getCustomFieldObject("Assignee");
//see assignee was changed in edit view (note null safe operator [?] which allow tower to be null without throwing an exception)
if((AssigneeName ?.value) != currentAssignee) {
//clear Tower field
TowerValue.setFormValue(null);
//require Tower field so that user is forced to enter text
TowerValue.setRequired(true);
}
else {
TowerValue.setRequired(false);
}

Also could you help us guiding where this script should be running in behaviour : initialiser / under filed tower (server side - script)/ under Field Assignee (server side - script)

Regards,

Priyanka.

1 answer

0 votes
Lasse Langhorn
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.
May 12, 2020

Hi @Priyanka Nimbalkar

You should add Assignee to the list of Fields in your Behaviour setup.

I have made some pseudo Groovy code which should be triggered when the Assignee field is changed. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

def towerValue = getFieldById("Tower")
def assigneeField = getFieldById(getFieldChanged())
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField currentAssignee = customFieldManager.getCustomFieldObject("Assignee")
if((assigneeField?.value) != currentAssignee) {
// Show and set required
towerValue?.setHidden(false)
towerValue?.setRequired(true)
} else {
// Hide and unset required
towerValue?.setHidden(true)
towerValue?.setRequired(false)
}

Hope this will get you further.

Regards

Lasse Langhorn

Priyanka Nimbalkar May 14, 2020

Thanks for your reply, But this doesn't work .

So whenever i am trying to change assignee , there is no change in tower field , neither  the "if " condition is getting executed nor the "else" one.

Could you please help me to understand the difference between getFieldByName & getFieldById ? ( as this has some effect on code when i tried to change it for both fields ).

And which assignee are these statements representing : 

def assigneeField = getFieldById(getFieldChanged())

CustomField currentAssignee = customFieldManager.getCustomFieldObject("Assignee")

Thanks,

Priyanka.

Lasse Langhorn
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.
May 20, 2020

Hi,

The following code applies to the field that your Behavior code is meant to execute.

def assigneeField = getFieldById(getFieldChanged())

The following code makes the assumption that there is a custom field name Assignee but Assignee is a Jira system field. This is wrong, my bad.

CustomField currentAssignee = customFieldManager.getCustomFieldObject("Assignee")

What about the following code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

def currentAssignee = getFieldById("assignee")
def assigneeField = getFieldById(getFieldChanged())
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField towerValue = customFieldManager.getCustomFieldObject("Tower")
if((assigneeField?.value) != currentAssignee) {
// Show and set required
towerValue?.setHidden(false)
towerValue?.setRequired(true)
} else {
// Hide and unset required
towerValue?.setHidden(true)
towerValue?.setRequired(false)
}

You can read more about Behaviors here.

Regards

Lasse Langhorn

Suggest an answer

Log in or Sign up to answer