Set Customfield with Javascript

Deleted user October 12, 2017

Hi All, 

I've been tasked to set a customfield with Javascript. 

Back story is that i want to query an API via javascript and then get the return values to set the customfields in the create screen so it "Auto-populates" certain customfield. 

Im currently having difficulties, just setting the customfield field with free text.

The current code i have is

<script type="text/javascript">
jQuery(document).ready(function($) {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
setDefaultValues();
});
setDefaultValues();

function setDefaultValues(){
setFieldValue();
}

function setFieldValue(){

$("#customfield_13600").val('Test');

 

});
</script>

 But it doesn't seem to set the customfield to "Test".

Any help is much appreciated.

Thanks, 

Pon

4 answers

1 accepted

1 vote
Answer accepted
Nadir MEZIANI
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 13, 2017

 

Hi @[deleted]

Could you try this , maybe it can help you.

<script type="text/javascript">

AJS.$("##customfield_13600").val("default value");

summuryField.hide();

</script>

Deleted user October 15, 2017

Hi @Nadir MEZIANI,

Thanks for your help!

Unfortunately this did not work :(

Nadir MEZIANI
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 16, 2017

Hi @[deleted]

Sorry , 

<script type="text/javascript">

AJS.$("#customfield_13600").val("default value");

</script>

could you share here screenshots?

Like freddy thuillier likes this
Deleted user October 17, 2017

Hi @Nadir MEZIANI,

Ahh its working with:

<script type="text/javascript">

AJS.$("#customfield_13600").val("default value");

</script>

Thank you so much for your help!

i really appreciate it.

Cheers, 

Pon

Deleted user October 17, 2017

hey @Nadir MEZIANI,

 

By any chance do you also know how to get custom field values via ajax? 

so like for example if i put in 123 value into a field, can i grab that value by any chance?

Many thanks for your help 

Deleted user October 17, 2017

Actually i got it :)

 

var custNo = document.getElementById("customfield_10000").value;

Nadir MEZIANI
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 17, 2017

hi @[deleted]

Or like this

<script type="text/javascript">

AJS.$("#customfield_13600").val();

</script>

, without put anythings in val () fuunction.

Aayush Mohanka May 14, 2020

Hi @[deleted] ,

Can you please help me ,where to add this code

<script type="text/javascript">

AJS.$("#customfield_13600").val("default value");

</script>
1 vote
Tayyab Bashir
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 16, 2017

Hi, 

$("#customfield_13600").val('Test');

The above won't work because the free text value is in form of an input and it's panel is inactive in idle mode (unless you double click and a text box appears) 
so the id, customfield_13600 does't exist yet. 

 

Use the following: 

AJS.$("#customfield_13600-val").text("Test Test Test")

0 votes
Neha Singh January 4, 2021

Hi Everyone,

I want to change the field alignment of Jira CF to center. For that I am using below code in the field description, but it is not working. Can someone please suggest me about it.

 

<script type="text/javascript">
jQuery(document).ready(function($) {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
setAlignment();
});
setAlignment();

function setAlignment(){
setFieldAlign();
}

function setFieldAlign(){
document.getElementById("customfield_18900").style.textAlign = "center";

 

});
</script>

Nadir MEZIANI
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.
February 3, 2021

H @Neha Singh 

If you want to align just the value , try this :

AJS.$('#customfield_18900'). parent().css('align-text','ecnter');

if you want to align label and value, try this:

AJS.$('#customfield_18900'). parent().css('margin','auto');

AJS.$('#customfield_18900'). parent().css('width','50%');

0 votes
Stephen W July 10, 2018

Hi Pon,

I'm just starting on Jira development projects. I've worked with JS for a few years, but have nil experience working with JS in Jira (Server).

I need to be able to do the same thing as you have discussed in this post - my edits need to be specific to tasks of a particular type.

How do you attach the javascript in the first place? Do you have any good beginners guide reading and simple examples of how to make this work?

Have you had to install any plugins from the marketplace in order to get this to work?

Thanks,

Steve

Nadir MEZIANI
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.
July 11, 2018

Hi

You could add javascrip scripts in description of field  , or in the banner or as a web resource in a plugin or using live field of power script for Jira

Stephen W July 12, 2018

Plugging JS into each field will become a maintenance nightmare - you just want all the code located in one place.

Banner insertion sounds good, but the Banners are used for all Task types and thus cannot be targeted, so this tends to negate this option as well.

So, what is left? Just Power Script, or have I missed something?

Nadir MEZIANI
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.
July 13, 2018

Yes maybe Plugging JS into each field will become a maintenance nightmare  mostly for begginer.

Using live field of powerscript is good but it's not free ,  and you missed the other alternative , whici is  developping a web resource plugin

nallu March 26, 2019

Hi @Nadir MEZIANI , I want to increase the character limit for particular custom field. Currently, It supports 255 characters and its a single line free text field. 

Can you please help me on this. 

Nadir MEZIANI
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 26, 2019

Hi @nallu ,

I think you could use an other custom field type which is multiple line text field.

nallu March 26, 2019

Hmmm.... But I would like to use single line text field itself @Nadir MEZIANI . if it is not at all possible, then only want to go for multiline text field. I tried java script but it doesn't work. 

<script type="text/javascript">

document.getElementById("customfield_xxxxxx").maxLength=500;

</script>

Nadir MEZIANI
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 27, 2019

@nallu, don't do it like this , you have a bug because jira will try to insert 500 chars in a culumn of database with 255 chars.

nallu March 27, 2019

ok thanks @Nadir MEZIANI . I will go with multi line field. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events