Forums

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

Updated Field Behaviour

Edwin Ruiz
Contributor
October 24, 2018

I added the Updated field to the behaviour. Can't find a way to trigger it don't know if all the functions listed in the API quick ref still work.

 

My question is two parts.

When is the server-side script run or how can i make it run?

Are all the functions listed in the quick ref such as:

getFieldById(fieldId)
formField.setFormValue(value)

still work?

1 answer

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
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.
October 24, 2018

Hi Edwin,

Yes, those still work. What are you trying to accomplish with your script? Can you post your entire script here? The server side script will be ran once when the form is loaded and then every single time the field is changed.

Regards,

Josh

Edwin Ruiz
Contributor
October 24, 2018

So I currently have this code linked to the Epic Link field. It works as intended but not firing when i want it to, which is after the user has finished making changes (hit the Update button). 

What I am trying to accomplish is adding a label to (parent) epic the ticket is linked to. I currently have similar code in the workflow for when a ticket is transitioned but a workflow doesn't account for when a ticket is linked manually to an epic as that is not changing a ticket status.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.label.Label;
import com.atlassian.jira.exception.DataAccessException;

// get an issue manager to deal with issues (getting and setting fields)
def issueManager = ComponentAccessor.getIssueManager();
def userManager = ComponentAccessor.getUserManager();
def me = userManager.getUserByName("testuser")
def issue;
Set<Label> labelsSet = new HashSet<Label>();
// get value in epic link field
String epic = getFieldById("customfield_10006").getValue();
String issueKey, issuesString;
if(getActionName() == null && epic != "null"){

issueKey = epic.substring(epic.indexOf(":")+1)

try{
issue = (MutableIssue) issueManager.getIssueByCurrentKey(issueKey);
} catch (DataAccessException e){
return;
}

def linkManager = ComponentAccessor.getIssueLinkManager();
def listTickets = linkManager.getOutwardLinks(issue.getId())

def allDone = true
for (IssueLink link : listTickets) {
// we only care about tickets in epic
if (link.getIssueLinkType().getName() == "Epic-Story Link"){
// only 1 tickets needs to not be done for the epic to not be done
if(link.getDestinationObject().getStatus().getName() != "Done"){
allDone = false;
}
}
}

labelsSet.addAll(issue.getLabels());

Label epicDone = new Label(91185L, issue.getId(), "epic-done")

if (allDone){
labelsSet.add(new Label(91185L, issue.getId(), "epic-done"))
}else{
for (Label label: labelsSet) {
if (label.getLabel() == "epic-done") {
labelsSet.remove(label);
}
}

}

issue.setLabels(labelsSet)
issueManager.updateIssue(me, issue, EventDispatchOption.ISSUE_UPDATED, false)

}

 

Adding the code above to the Updated field server-side script does nothing

Joshua Yamdogo @ Adaptavist
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.
October 24, 2018

Hi Edwin,

It is possible I am misunderstanding, but I don't think you would want to use a behaviour in this scenario. For adding labels to a parent epic, that doesn't seem like something that needs to be done instantaneously based on what's currently on the Edit form. Further, behaviours won't work at all if someone inline edits the Epic link field from the View screen.

I think you setting up a Script Listener is what you will want. For example, you can set up a listener that listens for the IssueLinkCreatedEvent. Whenever someone edits an issue to create a new Epic link, the listener will get triggered. The script you have above would then run to add the labels to the Epic.

Does that make sense?

Regards,

Josh

Edwin Ruiz
Contributor
October 24, 2018

Hey Josh,

Looks like that is a better route to go through. I will work on getting it to work on that event. It fires precisely when i want it to.

Edwin Ruiz
Contributor
October 24, 2018

Got it to work! :) For anyone trying to learn (Thanks Josh for the hint):

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent;
import com.atlassian.jira.issue.label.Label;
import com.atlassian.jira.exception.DataAccessException;
import com.atlassian.jira.ComponentManager;

def issueManager = ComponentAccessor.getIssueManager(); // get issue manager to deal with issues
def userManager = ComponentAccessor.getUserManager(); // get user manager to deal with users
def customManager = ComponentAccessor.getCustomFieldManager();
def me = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // create application user (used later to update ticket)
def createLink = event as IssueLinkCreatedEvent;
MutableIssue epic = (MutableIssue) createLink.getIssueLink().getSourceObject(); // get current issue that triggered the event

String issueKey; // will be used to store issue key of epic
Set<Label> labelsSet = new HashSet<Label>();

if(epic != "null"){ // epic is not empty (shouldn't be since it is a issuelink created event)
def linkManager = ComponentAccessor.getIssueLinkManager(); // get linkmanager to get linked issues of epic
def listTickets = linkManager.getOutwardLinks(epic.getId()) // we are concerned about outward links from epic
def allDone = true

for (IssueLink link : listTickets) { // iterate through issue links
if (link.getIssueLinkType().getName() == "Epic-Story Link"){ // we only care about tickets in epic
if(link.getDestinationObject().getStatus().getName() != "Done"){ // only 1 tickets needs to not be done for the epic to not be done
allDone = false;
break;
}
}
}

labelsSet.addAll(epic.getLabels()); // add labels to hashset

if (allDone){
labelsSet.add(new Label(91185L, epic.getId(), "epic-done")) // create a label and add to set
}else{
for (Label label: labelsSet) { // iterate set
if (label.getLabel() == "epic-done") { // if "epic-done" label exists
labelsSet.remove(label); // remove from set
}
}

}
epic.setLabels(labelsSet) // set the label field for epic with the set
issueManager.updateIssue(me, epic, EventDispatchOption.ISSUE_UPDATED, false) // update issue (without this changes don't save)
}

 

Joshua Yamdogo @ Adaptavist
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.
October 25, 2018

Hi Edwin,

Glad to hear you got it working. Nice work!

Regards,

Josh

Suggest an answer

Log in or Sign up to answer