Set customn field to component lead

Frits van der Holst October 26, 2015

I have customn field that I would like to set to the component lead if chosen and if not to the project lead. I have used a script as post function to realize this on issue creation, but it does not do anything sad

 

This is the code:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.user.ApplicationUser;

// GOAL: assign all user ids of the current issue components' lead to the custom field 'Additional assignee'
Issue issue = issue; // Not really needed but it will save you some red underlining when creating the listener
CustomField additionalAssigneeField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Additional Assignee(s)")[0];
//for every component that it is linked to the current issue
for (int i = 0; i < issue.getComponentObjects().size() ; ++i)
{
    // 1-Get the issue's component's lead
    // 1.1- get the component of the current issue
    ApplicationUser componentLead = issue.getComponentObjects()[i].getComponentLead(); // returns an ApplicationUser
    // 1.2- get the lead id of the component
    String componentLeadId = componentLead.getKey();
    
    // 2-Assign it to the issue's custom field
    // 2.1- Prepare the ModifiedValue pair
    List oldValue = issue.getCustomFieldValue(additionalAssigneeField) as List;
	List newValue = oldValue;
	ModifiedValue modifiedValue = new ModifiedValue(oldValue, newValue);
	newValue.add(ComponentAccessor.getUserUtil().getUserByKey(componentLeadId));
	// 2.2- Create a ChangeHolder
    IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
	// 2.3- Update the value in database
	additionalAssigneeField.updateValue(null, issue, modifiedValue, changeHolder);
}
return true;

What did I do wrong here?

8 answers

0 votes
Jeremy Gaudet
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.
October 26, 2015

I would try using this:

MutableIssue mIssue = (MutableIssue)issue;
mIssue.setCustomFieldValue(modifiedValue);
Frits van der Holst October 26, 2015

Where would I use this and how?

Jeremy Gaudet
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.
October 27, 2015

You would add the first line, and use the second line in place of "additionalAssigneeField.updateValue(null, issue, modifiedValue, changeHolder);".

Frits van der Holst October 28, 2015

Tried this but nothing really happens....

0 votes
Bart Van Belle October 26, 2015

CustomField additionalAssigneeField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Additional Assignee(s)")[0];


can be replaced by:


def CustomFieldManager = ComponentManager.getInstance().getCustomFieldManager()

def additionalAssigneeField= CustomFieldManager.getCustomFieldObject(the ID of your additionalAssigneeField customfield)  

 

don't forget to add "import com.atlassian.jira.ComponentManager;" also at the top of your code

Frits van der Holst October 26, 2015

I tried this as well, but still nothing happens (additional assignee(s) is not filled in :( )

Frits van der Holst October 26, 2015

I also get a lot of errors/ warnings using these 2 lines

0 votes
Frits van der Holst October 26, 2015

I placed it on the correct line now and still does not work. I will try your suggestion. Can you tell me which lines to change?

2015-10-26 14_50_03-Transition_ Create - Jira - TASS International.png

 

0 votes
Bart Van Belle October 26, 2015

another thing you could try, is to define your Customfield "additionalAssigneeField" by its customf-field ID (you can see the ID if you go to your custom field and for instance click the edit button, if the URL you will see the ID of your custom field)

 

import com.atlassian.jira.ComponentManager;

def CustomFieldManager = ComponentManager.getInstance().getCustomFieldManager()

def additionalAssigneeField= CustomFieldManager.getCustomFieldObject(the ID of your additionalAssigneeField customfield)  

0 votes
Bart Van Belle October 26, 2015

What kind of "field" is your custom field ? 

0 votes
Bart Van Belle October 26, 2015

if you call it before the "set status", "re-index" steps, does this resolve the issue ? I definitely would call it before those 2 steps...

0 votes
Frits van der Holst October 26, 2015

I call it at the very end of the list (in your case step 5).

 

btw. I also checked it with a next workflow step, but nothing happens there as well

0 votes
Bart Van Belle October 26, 2015

Hi,

 

When do you call this postfunction ? You first have to create the issue and then in the 2nd step of the transition run the postfuction. See screenshot. Does this help ?

Regards,

 

BartpostFunction.jpg

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events