Store all Environment Specific Variables
You may wish to write and test your ScriptRunner scripts on a test/development instance before migrating over to your production instance. Manually editing environment-specific variables in each custom script is time-consuming and prone to errors. We suggest you use names rather than IDs in scripts as the name of custom components do not typically change when migrating to a production instance.
Sometimes, there can be multiple components (such as custom fields) with the same name. When this is the case, we suggest searching for the field name in the context of the issue you have.
For example, in Jira you could run the following:
import static com.atlassian.jira.component.ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).findByName('Approvals')Did you know you can also use Dynamic Forms to create scripts with editable configuration parameters?
Storing Variables
If you can't use names as suggested above, then you need to abstract the IDs to a centralised place to avoid hard-coding them in multiple scripts.
Here we describe two ways of doing that - one using a simple java class that varies between environments, and the second one which reads configuration from a YAML file. Storing variables in the application home directory, or an environment-specific class, removes the need to edit custom scripts manually.
Groovy Class
The advantage of this method, besides the simplicity, is you won't need to cast the type to a String or other class.
The disadvantage is that this requires a different version of the Config class in each environment. If using a version control system to manage your script root, this method would involve some manual copying and could be a maintenance burden.
YAML File
One way of storing environment-specific variables is to create a class which will read a YAML file. Below is an example YAML file but this method works with any valid YAML file:
Approvals: customfield_12345
Long Custom Field Name: customfield_56789import com.acme.CachedFileConfig
CachedFileConfig.reload()