JS - Javascript not working on first load create screen / change issue type


i've ran into an issue that i cannot seem to resolve. 

My javascript function only loads on the second try after opening the create screen. 

The following is my full JS code that i place under the description field:

<input class="callAPIBtn" type="button" id="apiBttn" value="Retrieve Info">

<p id="text"></p>

<script type="text/javascript">

AJS.$(document).ready(function() {
AJS.$("#apiBttn").click(function() {

var invalidCustNum = "<font color='red'>Invalid Customer ID, please try again</font>";
var cid = getCustomFieldValue(10000).trim();

if (cid.length != 9 || cid.substring(0, 3) != "100"){


} else {

var url = "http://RESTAPI/" + cid + "/";

url: url,
success: function(result){

// Key = customfield ID, Value = Value of JSON object
var customfields = {
10004: "firstName",
10005: "lastName",
10301: "emailAddress",
10674: "address",
10024: "mobilePhone"

for (cf in customfields) {
var id = "customfield_" + cf;

if (document.getElementById(id)) {
setCustomFieldValue(cf, result[customfields[cf]].trim().replace(" "," "));

//Second Webbus call to get Service ID
var servicesURL = "RESTAPI" + cid + "/services?status=active"

url: servicesURL,
success: function(result){

var text = "";

for (val in result) {

text += result[val]["accountNumber"] + " - " + result[val]["serviceIdentifier"] + " - " + result[val]["serviceType"].toUpperCase() + "<br/>";

display("<font color='green'>Success</font>" + "<br/><br/>" + text + "<br/>");


}, error: function(data) {

display("<font color='red'>Error! Please try again</font>");


function display(text) {
document.getElementById("text").innerHTML = text;

function getCustomFieldValue(id) {
var customField = "#customfield_" + id;
var fieldVal = AJS.$(customField).val();
return fieldVal;

function setCustomFieldValue(id, value) {
var customFieldId = "#customfield_" + id;


<style type="text/css" style="display: none">

.callAPIBtn {
font-family: verdana;
color: #1E286A !important;
font-size: 10px;
box-shadow: 1px 1px 1px #BEE2F9;
padding: 3px 12px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
border: 1px solid #3866A3;
background: #FFFFFF;

.callAPIBtn:hover {
color: #14396A !important;
background: #468CCF;
background: linear-gradient(top, #468CCF, #63B8EE);
background: -ms-linear-gradient(top, #468CCF, #63B8EE);
background: -webkit-gradient(linear, left top, left bottom, from(#468CCF), to(#63B8EE));
background: -moz-linear-gradient(top, #468CCF, #63B8EE);


I'd appreciate any suggestion or help with this. 

Thank you in advance.






0 answers

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

64 views 0 5
Read article

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