Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,363,756
Community Members
 
Community Events
168
Community Groups

How to write a Behavior to auto-populate the Summary of an issue based upon multiple field values

When a user creates or edits an issue, I'd like the summary value of that issue to be auto-populated as the user enters values for 3 fields on the create/edit forms.

The summary value should look like "Pod - Name - Role"

Create issue screen.png

As you can see below, I've written 4 behaviors mapped to this project and issue type.  I don't feel like this is an elegant solution at all and it doesn't work.  I'm hoping someone far better at this than me can point me on the correct path.

My scriptrunner behavior summary code is:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY

@BaseScript FieldBehaviours fieldBehaviours


def summary = getFieldById("summary")
summary.setDescription("Will be set auotmatically when user selects Pod, enters a Name and chooses a Role")

 

My scriptrunner behavior pod code is:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY

@BaseScript FieldBehaviours fieldBehaviours

def pod = getFieldByName("Pod").value
def name = getFieldByName("Name").value
def role = getFieldByName("Role").value

def summary1 = getFieldById("summary")

//summary1.setDescription("Will be set auotmatically when user selects Pod, enters a Name and chooses a Role")
summary1.setFormValue("${pod.} + " - " + ${name} + " - " + ${role}")

 

My scriptrunner behavior name code is:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY

@BaseScript FieldBehaviours fieldBehaviours

def pod2 = getFieldByName("Pod").value
def name2 = getFieldByName("Name").value
def role2 = getFieldByName("Role").value

def summary2 = getFieldById("summary")

//summary2.setDescription("Will be set auotmatically when user selects Pod, enters a Name and chooses a Role")
summary2.setFormValue("${pod2} + " - " + ${name2} + " - " + ${role2}")

 

My scriptrunner behavior role code is:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY

@BaseScript FieldBehaviours fieldBehaviours

def pod3 = getFieldByName("Pod").value
def name3 = getFieldByName("Name").value
def role3 = getFieldByName("Role").value

def summary3 = getFieldById("summary")

//summary3.setDescription("Will be set auotmatically when user selects Pod, enters a Name and chooses a Role")
summary3.setFormValue("${pod3} + " - " + ${name3} + " - " + ${role3}")

 

 

 

1 answer

1 accepted

1 vote
Answer accepted

Since each field update event should trigger a change in the summary, I think the best solution is to store your script in your script root and apply the same script field in each field.

The alternative, is to force users to fill in the fields in a certain order and trigger the update on the last one only. I think that's even less elegant

So what I recommend is:

Initializer:

getFieldById('summary').setReadOnly(true).setDescription("""<span class="greenText">Populated automatically from Pod, Name and Role</span>""")

Now, your Pod field looks like a scriptrunner issue picker field, so I'd recommend the following in a script file (use script editor to create this):

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
@BaseScript FieldBehaviours fieldBehaviours

def pod = getFieldByName("Pod").value ?: 'podTbd'
def name = getFieldByName("Name").value ?: 'nameTbd'
def role = getFieldByName("Role").value ?: 'roleTbd'
def podIssue = ComponentAccessor.issueManager.getIssueObject(pod as String)
def summary = getFieldById("summary")

summary.setFormValue("$podIssue.summary - $name - $role")

Add that script file to each of your fields with the method "run"

This is brilliant Peter!

I knew I was on the right track but you took it over the finish line.  I like how concise your solution is.  I can easily understand what's going on here.

Having this as 1 script file and applying this server-side script is convenient.

I see how you are getting the Pod field (which is a ScriptRunner issue picker field, correct) as a string and then getting the Issue Object from that.  That allows us to then grab the summary of that Pod object..which is the info we want.  Very clever.

I was struggling with this syntax too ("$podIssue.summary - $name - $role") which now I see how it works.  I can call the different variables in just 1 parenthesis.

Like Peter-Dave Sheehan likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events