Templates and prepopulation: variables

Pre-defined variables

JEditor supports the following pre-defined variables in Templates and Prepopulation:

  • {$datetime} - locale date and time
  • {$time-all}  - locale time (full)
  • {$date-cal} - locale date (full)
  • {$time} - locale time (short)
  • {$date} - locale time (short)
  • {$user} - current user's name
  • {$user-fullname} - current user's full name

Custom variables

It's also possible to add custom variables:

1. Go to JEditor Configuration > Custom Javascript
2. Add the following lines to the Javascript field (replace _MYTESTVAR_ with the name of your variable and callback function's code with your code that should return the value of the variable)

JEDITOR.customVariables = JEDITOR.customVariables || {};
JEDITOR.customVariables['_MYTESTVAR_'] = {
  name:'_MYTESTVAR_',
  callback: function () {
    return 'PRIORITY: ' + AJS.$('#priority-field').val();
  }
};

3. Save

This sample code will return the value of the Priority field. It will only work in a dialog where the Priority field is displayed. The code will be executed only once - when JEditor pre-populates a field or when a user inserts a template.

Sample callbacks for various Jira field types

Choice custom field

JEDITOR.customVariables = JEDITOR.customVariables || {};
JEDITOR.customVariables['$custom_choice'] = {
  name:'\\$custom_choice',
  callback: function () {
    var myCustomFieldId = '10000';
    var myField = document.querySelector('#customfield_' + myCustomFieldId);
    var selectedId = myField && myField.value;
    var output = '';
    if (JIRA.Dialog.current && myField && selectedId) {
      output = myField.querySelector('option[value="' + selectedId + '"]').innerText.trim();
    }
    else {
      output = document.querySelector('#customfield_' + myCustomFieldId + '-val')
&& document.querySelector('#customfield_' + myCustomFieldId + '-val').innerText.trim();
    }
    return output;
  }
};

The code above assumes the custom field's id is '10000'. You need to replace this with an actual id of your custom field. You may also want to change the name of the variable ($custom_choice).

Multi-line text custom field

JEDITOR.customVariables = JEDITOR.customVariables || {};
JEDITOR.customVariables['$custom_multi'] = {
  name:'\\$custom_multi',
  callback: function () {
    var myCustomFieldId = '10000';
    var myField = document.querySelector('#customfield_' + myCustomFieldId);
    var output = '';
    if (JIRA.Dialog.current && myField) {
      output = myField.value && myField.value.trim();
    }
    else {
      output = document.querySelector('#field-customfield_' + myCustomFieldId + ' div.verbose div.flooded')
&& document.querySelector('#field-customfield_' + myCustomFieldId + ' div.verbose div.flooded').innerHTML.trim();
    }
    return output;
  }
};

The code above assumes the custom field's id is 10000. You need to replace this with an actual id of your custom field. You may also want to change the name of the variable ($custom_multi).
The code also assumes that the multi-line text field contains data in plain text or HTML format. This code will not render wiki markup. Instead, it will insert wiki markup as is.

Summary system field

JEDITOR.customVariables = JEDITOR.customVariables || {};
JEDITOR.customVariables['$summary'] = {
  name:'\\$summary',
  callback: function () {
if (JIRA.Dialog.current) {
return document.querySelector('#summary').value;
}
else {
    return document.querySelector('#summary-val').innerText;
}
  }
};

Custom date format

JEDITOR.customVariables = JEDITOR.customVariables || {};
JEDITOR.customVariables['$customdate'] = {
  name:'\\$customdate',
  callback: function () {
    return moment().format('DD/MM/YYYY');
  }
};

Issue key

JEDITOR.customVariables = JEDITOR.customVariables || {};
JEDITOR.customVariables['$issuekey'] = {
  name:'\\$issuekey',
  callback: function () {
    return (!require('jeditor-helpers').isCreateDialog() && JIRA.Issue.getIssueKey()) || 'n/a';
  }
};

The code above will return the issue key of the currently open Jira issue unless a "create" Jira dialog (one of Create Issue/Create Sub-Task/Create Epic) is active.

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk