Last active
April 2, 2020 14:49
-
-
Save jechlin/5380119 to your computer and use it in GitHub Desktop.
Display calculated custom fields on JIRA transition screens
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
(function ($) { | |
// --------------------------------------- MANDATORY CONFIG --------------------------------------- | |
var fieldName = "Scripted Field" // display name - does not have to match the name of the field | |
var fieldId = "customfield_14013" // field Id | |
// --------------------------------------- END MANDATORY CONFIG ----------------------------------- | |
function addCalculatedField(e, context) { | |
var $context = $(context); | |
// if you want you can limit this to certain actions by checking to see if this value is in a list of action IDs | |
if (! $("input[name='action']").val()) { | |
return; | |
} | |
// multiple handlers can be added if you do an action, then cancel repeatedly | |
if ($context.find("#scriptedfield_" + fieldId).length > 0) { | |
return; | |
} | |
var issueKey = $("meta[name='ajs-issue-key']").attr("content"); | |
if (! issueKey) { | |
issueKey = $("#key-val").attr("rel"); // transition screens in full page mode | |
} | |
var paddingTop = AJS.$("meta[name='ajs-build-number']").attr("content") < 6000 ? "1" : "5"; | |
var fieldGroupHtml = '<div class="field-group">' + | |
'<label for="' + fieldId + '">' + fieldName + '</label>' + | |
'<div style="padding-top: ' + paddingTop + 'px" id="scriptedfield_' + fieldId + '"> ' + | |
'<span class="aui-icon aui-icon-wait">Loading, please wait</span></div>' + | |
'</div> '; | |
// Modify this select if you want to change the positioning of the displayed field | |
$context.find("div.field-group:first").before(fieldGroupHtml); | |
$.ajax({ | |
type: "GET", | |
"contentType": "application/json", | |
url: AJS.params.baseURL + "/rest/api/2/issue/" + issueKey + "?expand=renderedFields&fields=" + fieldId, | |
success: function (data) { | |
if ("fields" in data && fieldId in data.fields) { | |
var fieldValue = data.fields[fieldId]; | |
$context.find("#scriptedfield_" + fieldId).empty().append(fieldValue); | |
} | |
else { | |
$context.find("#scriptedfield_" + fieldId).empty().append("ERROR - bad field ID"); | |
} | |
}, | |
error: function () { | |
$context.find("#scriptedfield_" + fieldId).empty().append("ERROR"); | |
} | |
}); | |
} | |
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) { | |
addCalculatedField(e, context); | |
}); | |
})(AJS.$); | |
</script> |
Running into the same issue as rblee19, I can't get the fields to go away on other transitions until a page refresh. Anyone figure this out?
Actually just got it working using just JIRA.unbind(JIRA.Events.NEW_CONTENT_ADDED);
Removing the function name in the unbind is working and so far isn't breaking anything else, more testing will tell.
Better solution, running unbind without calling the function to unbind was breaking other items on the page.
Had to name the function in the .bind so it wasn't giving it an anonymous function name and then could use .unbind to remove that function by name.
<script type="text/javascript">
if(AJS.$('#issue-workflow-transition-submit').val() =='Send Next Steps Email'){
(function ($) {
// --------------------------------------- MANDATORY CONFIG ---------------------------------------
var fieldName = "Scripted Field" // display name - does not have to match the name of the field
var fieldId = "customfield_14013" // field Id
// --------------------------------------- END MANDATORY CONFIG -----------------------------------
function addCalculatedField(e, context) {
var $context = $(context);
// multiple handlers can be added if you do an action, then cancel repeatedly
if ($context.find("#scriptedfield_" + fieldId).length > 0) {
return;
}
var issueKey = $("meta[name='ajs-issue-key']").attr("content");
if (! issueKey) {
issueKey = $("#key-val").attr("rel"); // transition screens in full page mode
}
var paddingTop = AJS.$("meta[name='ajs-build-number']").attr("content") < 6000 ? "1" : "5";
var fieldGroupHtml = '<div class="field-group sendNextStepsAttachments">' +
'<label for="' + fieldId + '">' + fieldName + '</label>' +
'<div style="padding-top: ' + paddingTop + 'px" id="scriptedfield_' + fieldId + '"> ' +
'<span class="aui-icon aui-icon-wait">Loading, please wait</span></div>' +
'</div> ';
// Modify this select if you want to change the positioning of the displayed field
$context.find("div.field-group:first").before(fieldGroupHtml);
$.ajax({
type: "GET",
"contentType": "application/json",
url: AJS.params.baseURL + "/rest/api/2/issue/" + issueKey + "?expand=renderedFields&fields=" + fieldId,
success: function (data) {
if ("fields" in data && fieldId in data.fields) {
var fieldValue = data.fields[fieldId];
$context.find("#scriptedfield_" + fieldId).empty().append(fieldValue);
}
else {
$context.find("#scriptedfield_" + fieldId).empty().append("ERROR - bad field ID");
}
},
error: function () {
$context.find("#scriptedfield_" + fieldId).empty().append("ERROR");
}
});
JIRA.unbind(JIRA.Events.NEW_CONTENT_ADDED, addCalculatedField)
}
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, addCalculatedField)
})
(AJS.$);
}
</script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having the same issue as kcaglar. I've added the unbind line, but my field continues to show in every transition screen. Are there any other workarounds? Here is my script: