Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Auto populate the custom field with the Reporter on transition screen

Lakshmi S
Contributor
July 24, 2023

Hi Team,

Is it possible to automatically fill in the custom field (a single-user picker) with the Reporter's name when transitioning from status A to status B? When we move from A to B, a transition screen appears with the custom field already present, we want the Reporter's name to be automatically populated in that custom field.

1 answer

1 accepted

2 votes
Answer accepted
Hyrum Steffensen _Appfire_
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 24, 2023 edited

EDIT: Scriptrunner example updated based on further testing.


Hello Lakshmi,

You can set a default value for a single user picker using Scriptrunner Behaviors or Power Scripts Live Fields. I will detail both below.

Scriptrunner

To limit the screen to a particular transition, you could do something like:

// if (fieldScreen.name == "Approved Screen") {

def reporter = underlyingIssue.getReporter().name
getFieldByName("userPickerSingle").setFormValue(reporter)

// }

 For a full example with the necessary class imports, see:

https://library.adaptavist.com/entity/limit-behaviour-to-screen

To get the name of the transition print it out during a transition. See:

https://scriptrunner-docs.connect.adaptavist.com/jiracloud/script-console.html

Power Scripts

The main script would look something like the following. Change "trans_21" to the name of the transition and "singleUserPicker" to the name or id of the Single User Picker.

if (argv["screen"] == "trans_21") {
lfSet("singleUserPicker", reporter);
}

  To get the name of the transition, print out the value for argv["screen"] during the transition. For example:

printInFile("debug.txt", "Screen value: " + argv["screen"]);

See this documentation on using Live Fields:

https://appfire.atlassian.net/wiki/spaces/PSJ/pages/15488008/Live+Fields 

Let us know how it goes!

Regards,

Hyrum

Please note that I work for Appfire and support Power Scripts.

Lakshmi S
Contributor
July 24, 2023

Thank you @Hyrum Steffensen _Appfire_ for your quick response.

Did I miss anything here? I'm receiving an error in the script. I added as server script in the behavior.

reporter.PNG

Hyrum Steffensen _Appfire_
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 24, 2023


Sure thing, Lakshmi!

Try using "underlyingIssue" instead of "issue". I tested the following and it works.

 

import org.apache.log4j.Logger

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

log.error("fieldScreen.name: " + fieldScreen.name)

// if (fieldScreen.name == "Approved Screen") {

def reporter = underlyingIssue.getReporter().name

log.error("Reporter name: " + reporter)

getFieldByName("userPickerSingle").setFormValue(reporter)

// }

I also added some logging for debugging. These will show up in the Jira system logs. Remember to remove the lines with "log.error" before deploying the code to a production environment. The "error" logging level will spam your system and consume lots of system resources.

See this on using logging in Scriptrunner:

https://docs.adaptavist.com/sr4js/latest/best-practices/logging/advanced-logging

Hyrum

 

Lakshmi S
Contributor
July 25, 2023

Thank you @Hyrum Steffensen _Appfire_ .

It's working expected.

script.PNG

Hyrum Steffensen _Appfire_
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 25, 2023

Glad to help, Lakshmi!

Lakshmi S
Contributor
August 3, 2023 edited

Hi @Hyrum Steffensen _Appfire_ ,

Is it feasible to switch to a different user? The script transfers information from the "Reporter" to the "Owner" field. However, if I want to change to another user in the same field and not display the reporter on the same transition, I cannot update it. Is there a resolution to this issue?

Also, is it possible to only input the Reporter if the Owner field is blank? If there is already a value, it should just leave that value.

Hyrum Steffensen _Appfire_
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.
August 3, 2023

Hello Lakshmi,

If you wish to set the value of "Owner" based on the value of another field, make sure that field has been added to the Scriptrunner configuration. Then, move the server-side script to that field. You can then get the changes of that field by using the getFieldChanged() methods.

In the example below, I got the user input for "selectList", then changed the value for "userPicker" based on the option selected.

Note how I am getting the user input value using the following line:

def selectList = getFieldById(getFieldChanged())

Here is the complete script:

import org.apache.log4j.Logger
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

def selectList = getFieldById(getFieldChanged())
log.error("getFieldChanged(): " + selectList.getValue())

if (selectList.getValue() == "option1") {
def reporter = underlyingIssue.getReporter().name
getFieldByName("userPicker").setFormValue(reporter)
} else if (selectList.getValue() == "option2") {
getFieldByName("userPicker").setFormValue("charlie")
}

 

If you wish to set the "userPicker" (Owner) field only when it is empty, add the userPicker field in the Behaviours configuration, then do something like the following in the initializer script:

 

if (getFieldByName("userPicker").getValue() == null) {
  getFieldByName("userPicker").setFormValue("charlie")
}

Hope that helps!

Hyrum

 

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, team '25, conference, certifications, bootcamps, training experience, anaheim ca,

Want to make the most of Team ‘25?

Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.

Learn more
AUG Leaders

Upcoming Jira Events