Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update the field with the new value with a behavior

Jaime Basco
Contributor
January 18, 2023

Hi,

I am trying to generate a summary automatically with the value of a field.
The problem is that if I change the value of that field when creating the task, it does not update the summary field with the new value.
I have created a behavior that works only when I update the creation screen or when I update the issue already created.

Behavior:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getDisplayName()
def summary = getFieldById("summary")
def inf = getFieldById("customfield_15900").value
summary.setFormValue("${inf}" + ":" + " " + "${currentUser}")


How can I make the summary field to be updated with the new value that I put in the field from which I take that value?

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
PD Sheehan
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 18, 2023

Where did you put your behavior script?

It should be against the server-side script for the custom field that should trigger an update.

But be careful, you don't want future users that edit the issue to cause the summary to be refreshed to that user.

So either make sure your script only runs on create or if you want to allow the summary to be changed during future edits of the custom field, then you'll need to use the reporter details instead of the current user.

Option 1 - only run during creation:

import com.atlassian.jira.component.ComponentAccessor

if(underlyingIssue) return //underlying issue doesn't exist during creation, and we only want to run initially during creation
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def summary = getFieldById("summary")
def inf = getFieldById("customfield_15900").value
summary.setFormValue("$inf: $currentUser.displayName")

Option 2 - user currentUser during creation and reporter after:

import com.atlassian.jira.component.ComponentAccessor

//first assume the reporter is the current user (for issue creation)
def reporter = ComponentAccessor.jiraAuthenticationContext.loggedInUser
if(underlyingIssue) {
//if an issue exists, the reporter is whoever was set as the reporter field on the issue
reporter = underlyingIssue.reporter
}
def summary = getFieldById("summary")
def inf = getFieldById("customfield_15900").value
summary.setFormValue("$inf: $reporter.displayName")
Jaime Basco
Contributor
January 23, 2023

Hello Peter,

First of all thanks for your reply.

I have tried the option 1- only run during creation - in the behavior and I have the same problem.

When I put some CF option I need to refresh the page so that the summary changes.

I can NOT find any option to update the summary without refreshing the creation page.

Do you know where I can look for this error?

Attach the examples

refresh the page.pngSIN update.JPG

PD Sheehan
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 23, 2023

Can you share a screenshot of your behaviour configuration?

Jaime Basco
Contributor
January 24, 2023

Hi Peter;

Yes! attach the script, 

-----------------------------------------------------------------

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

if(underlyingIssue) return //el problema subyacente no existe durante la creación, y solo queremos ejecutarlo inicialmente durante la creación

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getDisplayName()
def summary = getFieldById("summary")
def inf = getFieldById("customfield_15900").value
//summary.setFormValue("$inf: $currentUser.displayName")
//def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getDisplayName()
summary.setFormValue("${inf}" + ":" + " " + "${currentUser}")

-----------------------------------------------------------------

I have tried to combine your answer with the behavior I just created because some option does not give the result we are looking for.

Example of summary --> value: currentuser

Thakyou very much for your help Peter!!

PD Sheehan
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 24, 2023

There is nothing wrong with the script, but what I asked for is a screenshot of the behaviour configuration page so that I can know for sure where the script is being called from.

I do have a question about the script...

Why this?

summary.setFormValue("${inf}" + ":" + " " + "${currentUser}")

And not this

summary.setFormValue("$inf: $currentUser")

Isn't the second one much easier to read?

Jaime Basco
Contributor
January 26, 2023

Hi Peter,
I think it doesn't matter the line, this way it shows me the result in the summary field as requested.
Do you think it is a problem to leave it like that?

Does it influence the problem that the summary field is not updated?

I attach the configuration
config behavior.JPG

 

Thanks for your help!

PD Sheehan
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 26, 2023

No that line has nothing to do with the issue with the summary.

I was just curious why you kept the more complex version. 

The problem is where your script is located.

Only scripts linked to fields that are changed are executed. Add the "informacion que deseo modificar" field to the behaviour configuration and then copy your scrirpt there.

You can delete the server-side script from summary field.

When that field is updated, the script will run and the summary will be updated.

Jaime Basco
Contributor
January 27, 2023

hello peter,

I was misinterpreting the behavior.
I have just changed the configuration to the corresponding field and it is now displayed correctly.

thank you very much for your help and patience.

best regards

TAGS
AUG Leaders

Atlassian Community Events