Persistent variables
Sometimes, when you're trying to keep track of something in Jira, you have to create a custom field then update this field, and any time that you make another update you have to look at the value and do something like that. That can be an exhausting and annoying thing about using custom fields for trying to keep track of a counter or some value.To help you work with such cases, starting with version 4.1.7 the Simple Issue Language™ (or SIL™) acquired a new feature, called persistent variables.
Persistent variables enable you to track something that you need without having you to use custom fields. And this is extremely powerful when we are talking about Confluence, as you don't even have a concept of a custom field here.
So, what is a persistent variable? In short, in the scope of the Page, a persistent variable is a value inherently linked to a Page. Outside the scope of the Page, the persistent variable becomes a global variable, accessible from all scripts. The persistent variables for Confluence work pretty much the same way as they do in Jira, except they work with the page ID in Confluence instead of an issue ID in Jira.
For you to get a better understanding on what it means and how it can simplify your SIL™ usage throughout your Confluence integration, see the following usage examples.
Usage in the scope of the Page
Persistent variables are introduced via the persistent keyword. In order to save space, we do not allow persistent and constant modifiers to be used on the same variable, so you can't have both. A persistent variable is always modifiable.
Let's look into the page counter. Using a script below you can initialize, return, and then increment it.
persistent number counter = 0;
return counter++;
So what's happening here? The truth is that the initialization of the variable happens only once. Subsequent executions will find the persistent variable initialized, will load that particular value, and start from there. The flag will be flipped, and the counter will be incremented with each executed transition on that page.
If you refresh the page, the counter will be incremented because the script is executed again and the persistent variable is updated. So, this persistent variable will be updated each time that the script is executed. It is the one which actually executes in the context of the page.
Note
When you create a copy of the page it will not keep the number.
The same if you restart your Confluence instance.
Only admins can see the page count.
Usage in the Global Scope
If you need to check State execution between a SIL™ script from one execution to another, you can do this using the persistent variables.
For example, you have the value of 3 and you'd like to change it, for instance, for value of 42 using Number page ID. In this case you can use the setPersistentVar to provide this page ID, which you've just retrieved, then set it to 42. Take for instance the following script, run from the SIL™ listener.
persistent number counter = 0;
logPrint("ERROR", "\n**********\" + (counter++) + "\n**********\n");
const number pageId = getPage("PP", "PSC Guide");
setPersistentVar(pageId, "counter", 42);
Helper routines
There are two helper routines available to ease your work with persistent variables.
getPersistentVar — Gets the specified persistent variable as a string.
setPersistentVar — Sets the specified persistent variable as a string.