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

Hello, 

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"){

display(invalidCustNum);

} else {

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

AJS.$.ajax({
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"

AJS.$.ajax({
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;
AJS.$(customFieldId).val(value);
}

</script>

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

</style>

I'd appreciate any suggestion or help with this. 

Thank you in advance.

Pon

 

 

 

 

0 answers

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,825 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot