Populate summary field with static text + values from custom field

Stian Bentsen Sveen
Contributor
July 17, 2018

Hi,

My knowledge of scripting is very limited. I've been doing mostly configurations and automations in our JIRA based on pre-built functions or JQL.

 

I am looking to populate the summary field for a certain request type with some static text + values from two custom fields.

Its a form for "Create New Employee". I dont want the users input on the summary, I want it standardized. Today i've just hidden the summary and preset the value to "Create New Employee".

Since we are growing quite a bit, it can now be problematic to have a bunch of requests with the summary "Create New Employee without knowing which one is for which user.

Therefore I would like to also populate the summary with the custom field "Name" and custom field "Surname" as well as the static text so the summary after creation will be "Create New Employee - name + surname".

I've searched around, but most of the answers just states that you can use Scriptrunner / Behaviours for that, but no mention on how to do it.

I tried to understand it, but it just doesnt makes sense to me.


Can anyone help me with a groovy script for this so I can create a behaviour for this. It would also be incredibly helpful with some "help comments" on each step so that I can try to understand and learn why you're doing this or that, so maybe I can learn how to do this myself in the future.

Thanks!

3 answers

2 accepted

1 vote
Answer accepted
Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 17, 2018

Hi,

 

Though I disagree with the "categorization" by summary, the actual automatic summary is a good approach.

 

You can surely do it in a lot of ways, but most propably in a post function during create transition. You need to add a custom script post function into the desired workflow (custom script post function - part of Script Runner)

Now here's the tricky part:

  • If you have your field configuration configured that Summary is required., which I assume you have as that's the default basically, you would need to add the Post Function at order 1, above the Create Issue post function.
  • There's also the option that your field config does not care about the summary and you can put the post function last (i'm only mentioning this since most cases you would put the post function at the last order)

 

Now if you got to this part, the script would look something like this

(I will try to write in details, not shortening, so you can follow the logic)

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()



def myCustomField1 = customFieldManager.getCustomFieldObjectByName('Name of custom field 1')

def myCustomField2 = customFieldManager.getCustomFieldObjectByName('Name of custom field 2')

//if you have fields with same name, which btw bad practice, you can also use getCustomFieldOBjectById



//get values, you could also do it in 1 line

def value1 = issue.getCustomFieldValue(myCustomField1).toString()



//or

String value2 = issue.getCustomFieldValue(myCustomField2)




//set summary

def concatValue = "New Employee:" + value1 + value2

issue.setSummary(concatValue)





//done

 

 Br

Stian Bentsen Sveen
Contributor
July 17, 2018

Hi Gezim, 

Thank you so much! 

I'll look at this first thing tomorrow morning.

It's not as much categorization as it is an easy way of differentiating between multiple "New employee" tickets. They could just edit the request and include the name in the summary, but it would be even better to automate it.

If I can learn this that also mean I can probably do tons of other things with similar scripting, which is just what I'm after. :) 

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2018

@Mark Markovalso has a good point. Behaviors would be my choice also, since in your case you have already defined a default value for the field.

 

Though issue is that you would monitor 2 custom field for changes.

Also it would require 2 updates, that's why I suggest you remove even the default one and add a post function at the top of the order to populate it. This way it would show up within the create event, just as that was the original user's input.

 

Events/Updates are always worth saving/grouping.

 

Br

Stian Bentsen Sveen
Contributor
July 18, 2018

 @Gezim Shehu [Communardo] Thanks, I got it working with your script and with @Mark Markov input using behavior for default values as well.

Without using behavior to set a default value I got an error on creating because summary wasnt populated, even though I added the post function at the top of the order list (as you mentioned in the "tricky part").

I understand a bit more know and I guess using this in other contexts is just finding out which libraries I need to import and which commands / fields to use (easily explained ;) ).

Now I just need to figure out the "if" clause to make this only apply for request type "Create New Employee" so it doesnt set this summary for all request types using that workflow, hehe.

Thanks for the help, both of you!

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2018

Hi @Stian Bentsen Sveen,

 

I remember also having troubles with getting the Service Desk request names.

By default, using only normal methods to retrieve the value from Customer Request Type custom field, you would get the key, instead of the name.

Key looks something like this:

"rsd/2aed6e92-1695-4928-9926-8475e36d40ad"

 

Here's a little script that gets the name of the Customer Request Type, using the Service Desk API;

 

//service desk apis to get name instead of key value
import com.atlassian.servicedesk.api.requesttype.RequestTypeService
import com.atlassian.servicedesk.api.requesttype.RequestType
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

@PluginModule

RequestTypeService requestTypeService

//set Jira Robot as logged in user
def authContext = ComponentAccessor.getJiraAuthenticationContext()
def userKeyToSet = 'robot'
authContext.setLoggedInUser(ComponentAccessor.getUserManager().getUserByKey(userKeyToSet))
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


RequestType getRequestType = requestTypeService.getRequestTypeForIssue(currentUser, issue).right().get()
String requestTypeName = getRequestType.getName()
Stian Bentsen Sveen
Contributor
July 18, 2018

@Gezim Shehu [Communardo] Yeah, I remember having some issue with that a while ago. I just went into the DB and found the key and tested with the if clause and it works great!

 

Thank you so much for your assistance and the extra script for customer request type name is perfect as well!

Br,

Stian

Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2018

Np. Glad to help

Brian Manning August 28, 2019

Is there any way to do this outside of using Script Runner? For us that would be over $700 a month to utilize that tool. 

 

I basically want to do (CE-Eventname-Username) as the summary field. CE would be static text, and eventname and username are required input fields. 

 

HEEEELP!  :) 

 

Thanks everyone!

serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2020

hello @Brian Manning di you find the solution to your issue because I have the same needs for similar approach but for other field.

Did you find a way to do without using script runner for instance ?

regards 

Brian Manning January 22, 2020

No, unfortunately, this was a bit of a dead-end without having to spend a bunch of money for a one-use plugin.

serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2020

I manage to do that using Automation for Jira for my customer

Brian Manning January 22, 2020

would you mind sharing how you did this? 

serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2020

The way I test it for instance is that I have a service desk request when I need to create a new account for a user.

When the request is created , it create 2 sub task attached to the TSD request.
using automation I set the description field of the fisrt task by getting the user name using smart value syntax

2020-01-22_16-00-24.png 

In the mean time I add an Edit issue field function to edit the summary field of the issue which will add the user name in the same way

2020-01-22_16-05-43.png

Zdeněk August 4, 2022

Hello, thank you for the code. I just have one issue with that. I want to populate summary field based on Select List (cascading). When I run sucessfully this code, it returns the value in this format: Objednávka [null:Audit staff, 1:Travel Tickets]. Is it possible to get rid of the null, parenthesis and the number? I just need the text from the field. I spend like 4 hours looking for the solution but nothing worked for me. 

Could you please take a look at my code? 

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def myCustomField1 = customFieldManager.getCustomFieldObjectByName('Typ objednávky a předmětu')

def value1 = issue.getCustomFieldValue(myCustomField1).toString()

def concatValue = "Objednávka" + " " + value1

issue.setSummary(concatValue)
1 vote
Answer accepted
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2018

Hello @Stian Bentsen Sveen

Are you using service desk?

As i understand the user must fill customfields name, surname and this info must be added to summary field, Am I correct?

Stian Bentsen Sveen
Contributor
July 17, 2018

Hi Mark, 

Yes that's correct, they fill in those fields in the form (required fields) and if possible I'd like that information to be populated in the summary along with the static text when the issue is created.

 

Edit: and yes we use service desk

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 17, 2018

Behaviours work on front-end side. It means that when users fill fields, summary field on screen will change dynamically. And this is means that users can change the summary after it fills automatically. If you need to change summary without users notice that, will be prefer to use @Gezim Shehu method with some corrections.
Behaviours preferable to use for set defaults for fields, like in this example

https://scriptrunner.adaptavist.com/5.4.12/jira/recipes/behaviours/setting-default-fields.html

(also recommend you to check this article about how to create behavior from the start https://scriptrunner.adaptavist.com/5.4.12/jira/behaviours-overview.html)

And to set field defaults in service desk, you need to create behaviour with mapping to service desk like this

Снимок экрана 2018-07-18 в 0.36.37.png

Then click on fields and Create Initialiser with code like this

getFieldById("summary").setFormValue("New Employee")

This is will fill summary field with "New Employee" value and your user will not need to fill this field.

After this you can use @Gezim Shehu [Communardo] postfunction to add usernames to summary field.

Stian Bentsen Sveen
Contributor
July 18, 2018

Thanks @Mark Markov,

What we've done for each service request type is to hide the summary field with the value preset to the request type so the users dont have to fill in anything there, just the custom fields / description added to each form. This is because from experience, some users start writing the hole request in the summary field, incorrect info there etc (this is for internal IT support in our company).

summary.PNG

 

The only issue type they can choose summary for is incidents as there only one form for that since IT does all the categorizations since users usually dont know what the cause of the incident is.

Would it be better to change this and create behaviors for this instead that populates the summary field for each request type?

For the requests where we will try to use scripting to add additional values to the summary it doesnt make sense to set a preset value on the request form if we have scripting that will replace it anyway.

1 vote
Tuarn McInerney
Contributor
December 20, 2020

Hi

I wanted to update the summary field with the fields "first name" + "last name"

this is the script I ran in automation

 

"New User Access request for - {{issue.First Name}} {{issue.Last name}}"

 

New User Access request.jpg

Works a treat:)

Tuarn

Francisco Cardona
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 28, 2021

How can you include the start date in this example?

serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 2022

Simply in same way. Identify the field name on your issue and call it like issue.myfield.

Note :

In case of custom field your will have to provide the field ID name instead of field string

Like Ahmed Mekawy likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events