Skip to content

Instantly share code, notes, and snippets.

@jechlin
Last active April 2, 2020 14:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jechlin/5380119 to your computer and use it in GitHub Desktop.
Save jechlin/5380119 to your computer and use it in GitHub Desktop.
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
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
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>

@Yediyd
Copy link

Yediyd commented Mar 31, 2020

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?

@Yediyd
Copy link

Yediyd commented Mar 31, 2020

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.

@Yediyd
Copy link

Yediyd commented Apr 2, 2020

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