Is there a way to get a customfield id in Service Desk?

Yiwei Gao May 11, 2017

I've written a custom field for Jira, and it uses some DOM manipulation to init the field as an AUI Select2 (https://docs.atlassian.com/aui/latest/docs/auiselect2.html). I would like to use this field in Service Desk, and it seems that the "JIRA" context unfortunately doesn't exist in Service Desk (I get `Uncaught ReferenceError: JIRA is not defined` when loading the js resources). 

Ok, not a huge deal, I don't really need the JIRA object, but I was wondering if there a way to programmatically detect my custom field id so I can do the necessary DOM manipulation to get feature parity as in Jira.

1 answer

0 votes
EddieW
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 11, 2017

You can still use AJS in your plugin to manipulate the Service Desk fields, and any AUI objects (like select2).

BUT Service Desk is for unknown reasons different that every other atlassian product in how you access AJS.

The trick is to NOT declare AJS as a dependency, but include any non-core AUI dependencies (i dont think select2 is core yet)

<!-- Service desk you jerk. Can't have AUi/AJS -  https://answers.atlassian.com/questions/41817061-->
    <web-resource name="Banner Web Resources (for Service Desk)" key="banner-resources-jsd">
        <dependency>com.atlassian.auiplugin:aui-progress-tracker</dependency>
.... any of your own js/css/images

<!-- context used by all JSD pages -->
        <context>customerportal</context>
    </web-resource>




And then use some JS to wait for the delayed loading of AJS in service desk portals

// JSD errors if you declare AJS dependency, but it is available, but may load after our code
var ajsAttempt=0;
function onFunctionAvailable() {
    if(ajsAttempt++ > 50){
        console.log("AJS is not available. Statuspage integration will not load.");
        return;
    }
    try{
        AJS.$.length
        console.log("AJS Methods Ready");

        //call function to manipulate dom here... i.e. 
       AJS.$("#select2-example").auiSelect2();

 }catch(err){
     console.log("AJS Methods not ready");
     setTimeout(function () {
             onFunctionAvailable();
             }, 50);
 }
}
onFunctionAvailable();
Yiwei Gao May 11, 2017

Hey, thanks for the quick reply! Do you know if there's a way to programmatically get the id of the custom field? The issue that's stumping me is in Jira my field element loads with an id that I set, but in Service Desk the id is just "customfield_15006".

Since I'm planning on deploying this across multiple instances, I can't hard-code in the id, so I was looking for ways to abstractly detect it.

 

EddieW
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 19, 2017

yes, you'll need to use a backend JIRA service for that translation.  There is a few methods to translate the pretty name from the system ID.

https://docs.atlassian.com/jira/7.2.0/com/atlassian/jira/issue/CustomFieldManager.html

So you may want to query the customfield_15006 (or approproate ID) in the backend first, and pass that ID to your front end JS.

Alternately you could use the REST api from your JS to look it up. https://docs.atlassian.com/jira/REST/server/#api/2/field-getFields

Suggest an answer

Log in or Sign up to answer