Change assignee via javascript

grundic
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.
June 3, 2012

Hello!

I would like to know, is it possible to change assignee via javascript?

I don't want to use post-function, because user would like to see who is going to be assigned and change if he wants.

We are in migrate process from 4.4 to 5.0, but I suppose, that AssigneePicker has not been refatored too much.

And yes, I've searched 'answers' before posting this :]

Thanks in advance!

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
grundic
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.
June 7, 2012
var loggedInUser = JIRA.Meta.getLoggedInUser();
var assigneeSelect = $("#assignee", context);
assigneeSelect.trigger('set-selection-value', loggedInUser.name);

This is the most correct way to set assignee.

Carsten Beck-Astrup April 23, 2013

Hi, when I try this, I get an error regarding the context. I found a workaround for this by:

var assigneeSelect = document.getElementById('assignee');

After this change, I get an error regarding the trigger function which it says is not known. I use JIRA 5.2.8.

Does anyone know what I can do to set a user-id/name to the assignee in a popup resolve screen from a custom field (Responsible)?

0 votes
Mizan
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.
June 3, 2012

Are you trying to set assignee on the issue creation page based on some condition ?

I dont have a javascript solution but may i think behaviours plugin can help ,

You can add a behaviour to the assignee field , check for the condition and accordingly set the assignee .

You can refer these documentations

https://studio.plugins.atlassian.com/wiki/display/JBHV/Miscellaneous+Behaviours+Examples

https://studio.plugins.atlassian.com/wiki/display/JBHV/JIRA+Behaviours+Plugin#JIRABehavioursPlugin-Makeafieldreadonlyexceptforacertainrole

https://studio.plugins.atlassian.com/wiki/display/JBHV/JIRA+Behaviours+Plugin#JIRABehavioursPlugin-Setonefieldbasedonanotherfieldwithinformationprovidedbytheserver

grundic
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.
June 3, 2012

Thanks for answer!

but the problem is that JIRA.AssigneePicker is created using some javascript magic (I suppose via prototype inheritance). So it's not as easy as

AJS.$("#assignee-field").val("foobar")

There is some model, some descriptor and so on. I tryed to get some info via Firebug

JIRA.AssigneePicker.prototype.getDisplayVal()

but have an error

TypeError: this.options is undefined
[Break On This Error] 	

return descriptor[this.options.itemAttrDisplayed || "label"]();

So, your method doen't work for assignee filed :(

Mizan
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.
June 3, 2012

I am able to get and change the assignee using the following script

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

FormField assignee = getFieldById(fieldChanged)
Object componentFormValue = assignee.getFormValue()


log.debug("-------------------assignee---"+componentFormValue)

assignee.setFormValue("test1")

Hope you find it useful

grundic
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.
June 3, 2012

Okay, it's better now, but still doen't work.

I've got "Issues must be assigned." error (even if I only change 'summary' for example)

I've created user test1 and add him to jira-developers. I'm able to assign issues. Now I added new behaviour for assignee filed. When I create or edit any issue I've got such an error.

In log I see previous assigned user or 1 when creating issue.

I'm on Jira 5.0.4

grundic
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.
June 3, 2012

So, at the end of the day my collegue fond solution:

var value = "test1";
AJS.$("#assignee-field").val(value);
AJS.$("#assignee").find(":selected").data("descriptor").model()[0].value = value;

This code will change value in edit box, which is visible and set option in hided select list. After posting data to server you will get right that value!

One drawback of this solution, that is doesn't handles users's avatar. We don't need this, but if you do - just make ajax call to http://docs.atlassian.com/jira/REST/latest/#id164624 and parse response.

I hope, someone will find this solution helpfull. Also, this should work ok for every user picker (assignee, reporter or any custom field).

Raju KC
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 6, 2013

Does not work for me!!

Here is my code: I want to copy user in customfield_11320 to customfield_11124.

<script type="text/javascript">
AJS.$(document).ready(function() {
var value = AJS.$("#customfield_11320").val();
AJS.$("#customfield_11124").val(value);
});
</script>

JPB June 29, 2014

Hello Grigory, where did you put this code:

var value = "test1";
AJS.$("#assignee-field").val(value);
AJS.$("#assignee").find(":selected").data("descriptor").model()[0].value = value;

I need to do something similar, I'm using the bahaviours plugin to set a value to the assignee field depending of other field, but I can't get that working, so, where did you put your code to get it work? thanks

TAGS
AUG Leaders

Atlassian Community Events