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

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

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();

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.

 

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
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

465 views 7 5
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you