KCF-Pro Custom Field Clear from Java Script

AndrewB November 25, 2015

I'm wondering how I can use JavaScript to clear a Kepler Custom Fields Pro custom field. 

 

Here's the use case:

I have an external application that has to create a certain issue type that happens to have 1 KCF-Pro custom field called Builds on the Create screen (Just recently changed the field from a JIRA field to KCF-Pro). I've just learned that KCF-Pro doesn't support the REST API yet. The good news is that the external application will use the same value every time for the Builds field. The Builds field has to be filled out by users.

 

Temp workaround:

Right now the Builds field is not required, a validator script requires users other than the external application user to fill in the field, and a post function script handles setting the field for the external application. This makes it so the external application doesn't have to do anything with the Builds field.

 

Want:

I would ideally like to make the field required. My thought was to make the field required, remove the validation script, remove the post function script, set a default value to what the external application uses, and use JavaScript to clear the default value. This would present a blank required field to users using the UI. The external application could still not care about the Builds field and just use the default value.

 

What I've Tried:

I've used JavaScript in both field configurations and the announcement banner to manipulate fields before. However, I wasn't able to clear the default value out of the Builds field. It's of type KCF - Autocomplete Multiple set to Issue Picker. When I put the JS in the field config field description on Builds, the JS appeared as an actual description on the create screen so I assumed that it doesn't support JS there. So I moved it to a different fields field config field description that is also on the create screen - doing this seemed to load the JS since it didn't appear as text on the create screen.


On field config field description of fellow create screen field:

<script type="text/javascript">
    createScreenModifications();
 
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
        createScreenModifications();
    });
 
    function createScreenModifications(){
        // For value I put the Issue ID of the default value
        AJS.$("#customfield_xxxx option[value='1234']").remove();
    }
</script>

 

In banner:

I tried this because it worked for me on a different custom field where I set the field to a specific value using the same syntax (I wasn't trying to clear that field though).

<script type="text/javascript">
    var fieldID = "customfield_xxxxx";
    document.getElementById(fieldID).value = ""; // Also tried null
</script>

 

I'm running 6.4.6 BTW. Please let me know if you need more info. Thanks in advance.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Alexandra Topoloaga
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.
November 26, 2015

Hi Andrew,

 

The simplest way to do this is by using Live Fields (if you also have JJUPIN plugin). The support for setting a custom fields to an empty value will be available since tomorrow (when we'll release a new version on both JJUPIN and KCF Pro).
Your code would look like this:

lfSet("customfield_XXXX", "");


However, if you want to try to set it via javascript you should know that this field  (both of the ones with autocomplete, actually) is a select2, so you should do it this way:

 

AJS.$("#customfield_XXXX").select2("val", "");

 

HTH,
Alexandra 

AndrewB December 1, 2015

Thank you, I didn't know the field type was a select2.

AndrewB December 10, 2015

AJS.$("#customfield_XXXX").select2("val", ""); did not clear the KCF - Autocomplete Multiple field for me (I did put my actual custom field ID in when I tried). I tried looking up some other examples to try other variants, but none cleared the field. Do you have another example, or have you tried this yourself?

AndrewB December 10, 2015

After trying more variants, I found that AJS.$("#customfield_XXXX").val('').change(); cleared the KCF - Autocomplete Multiple field for me.

Alexandra Topoloaga
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.
December 11, 2015

Hmm.. I have tested both of the solutions on edit/create screen, both of them worked the same (correctly). Anyway, sorry that my answer didn't help you that much, glad you found your own.

AndrewB December 11, 2015

Your answer did help me very much - I still needed to know that it was a select2. Your answer got me close enough to do a little more research and find the solution. Maybe it's a JIRA version, browser type, browser version, etc. thing. Thanks for the help!

Alexandra Topoloaga
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.
December 11, 2015

If you also need to reset the value for view screen, this small piece of code should do the trick: AJS.$("#customfield_xxxx-val").html('') :)

Justin Rush February 26, 2016

I have a related question. I'm trying to pre-populate a KCF - Autocomplete Multiple set to Issue Picker with a default issue using AJS as it is a hard constraint.

AJS.$("#customfield_XXXX").select2("val""");

Using the call above seems to be the best approach for me. I have an issue id and am confused about the format of "" in the select2("val""") portion of the call. I have a JIRA id of TST-1234

Alexandra Topoloaga
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 28, 2016

Answered here.

TAGS
AUG Leaders

Atlassian Community Events