Skip to content

Instantly share code, notes, and snippets.

@jechlin jechlin/gist:5380119
Last active Mar 1, 2019

Embed
What would you like to do?
Display calculated custom fields on JIRA transition screens
<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>
@kcaglar

This comment has been minimized.

Copy link

kcaglar commented Feb 22, 2015

This code is very useful. But I encountered a small problem. When the script runs for the first time, scripted field is displayed on each workflow screen. I had add the following row after ajax call:
JIRA.unbind(JIRA.Events.NEW_CONTENT_ADDED, addCalculatedField);

@rblee19

This comment has been minimized.

Copy link

rblee19 commented Mar 1, 2019

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:

<script type="text/javascript">
  (function ($) {

		// --------------------------------------- MANDATORY CONFIG ---------------------------------------
		var fieldName = "Unresolved Issues" // display name - does not have to match the name of the field
		var fieldId = "customfield_18161" // 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.unbind(JIRA.Events.NEW_CONTENT_ADDED, addCalculatedField);
		}

		JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
			addCalculatedField(e, context);
		});

	})(AJS.$);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.