Clickable Link Disguised as a Power Action™ with Live Fields

Clickable Link Disguised as a Power Action™ with Live Fields

Prerequisites

Required apps


This recipe assumes :

Problem

It often happens that our customers would like to create a link on the issue that will send the user to a customized page (external systems anyone?). The link will, most probably, be generated based on the user, his/her permissions and other issue data.

Solution

Power Actions™ for Jira app enables you to add buttons on the screen. It is aimed to integrate them into the workflow, while we only need a link. However, we will use the buttons created by Power Actions™ for Jira app and change their behavior using Power Scripts™ for Jira's Live Fields. The idea is to break the connection to the Power Actions™ Engine and put a plain link into it using Live Fields by executing a piece of JavaScript when the issue page is loaded. We want to be able to use  lfSet (button, URL). To do this, we'll first need to teach Power Scripts™ for Jira how to handle the Power Actions™ buttons (don't worry, this example provides you with a reusable piece of JavaScript code to do that). We'll put this script in the initialization of the Live Fields along with the lfSet() call we want.

For the purpose of this recipe, we'll create a link to Google's homepage, but this can be anything you want (keep in mind, it can be generated conditionally using SIL™).

Step 1 - Create the JavaScript File

When the issue page is loaded, we'll execute a piece of JavaScript code that enables you to use the Power Actions™ button with Live Fields just like any other issue field. Remember that Live Fields know how to handle the description of the issue, for example, when you do a lfSet on it. The same way this will teach it how to use a specific Power Actions™ button when we do a lfSet(button, URL).

clickable-link.js
(function(){
	/* Descriptor used by Live Fields to know how to do lfSet */
	var BAlink = new jjupLF.ExtendedViewSelector("single-select", {
		view : {
			setValue : function(fieldId, url){
			/* the field ID will come in with the value of linkID */
			/* find the link inside the button */
			var link = AJS.$("#" + fieldId).find("a.toolbar-trigger");
			/* set the new url */
			link.attr("href", url);
			/* [optional] make it open in a new tab. You can skip this to open in the same window */
			link.attr("target", "_blank");
		}
	}});

	/* we'll call this for each PA button we want to make clickable */
	function makeClickableLink(baId, fieldName){
		/* generate a unique id for the link*/
		var linkID = "jjlf-clickable-" + baId;
		AJS.$("li.blitz-action[buttonid=" + baId + "]").attr("id", linkID); 
		/* clear the already existing onclick handler */
		AJS.$("#" + linkID).attr("onclick", "");
	
		/* register the fieldName with the Live Fields*/
		jjupLF.registerDescriptor(fieldName, linkID, "", linkID, BAlink);
	}

	/* FILL IN WITH YOUR DATA HERE*/

	makeClickableLink(10000,           /* 10000 is the id of the Power Action that we want to convert */
	                  "goToGoogle");   /* this is the name we will use in SIL to call lfSet with */


})();

You will only need to call makeClickableLink at the end of the script for each button you want to convert. The rest of the script can be used as it is. The function takes two parameters:

  • the button id. This is the id of the Power Actions™ button. You can retrieve it by looking for the selectedValue parameter in the URL of any of the screens that allow you to edit the SIL™ scripts behind the button. It should be at the very end of the address (for instance /secure/admin/EditBlitzCondition.jspa?[...]selectedValue=10000).
  • the field name. This is the field name which will be used with lfSet. Chose any unique name you want for this parameter. For this example, we'll use "goToGoogle".

Place this script in your silprograms folder.

Step 2 - Configure the Initial Script for Live Fields

All you have to do now, is to configure the initial script to run the JS and then set the URL you want.

initial-script.sil
lfExecuteJS("clickable-link.js");
string myURL = "http://www.google.com"; // calculate your URL here
lfSet("goToGoogle", myURL); 


Don't you love us ?

See also