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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,412
Community Members
 
Community Events
184
Community Groups

Clone issue using Web Item

I want to add a button (Web Item) that will clone the current issue and change the issue type.

How should that be done? Is the only way to create a REST endpoint and a Script Fragment or is there a better way?

1 answer

0 votes
Brittany Wispell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 14, 2018

Hey @Michael Bendtsen

You'll want to use the Script Fragment (Constrained create issue dialog) and a Behaviour.

See below image. See example code below for the behaviour. 

Screen Shot 2018-11-14 at 10.48.00 AM.png

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import groovy.util.logging.Log4j;
import org.apache.log4j.Level;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.customfields.option.Option
@Log4j
public class CreateIssueButton extends FieldBehaviours {
// Constants
private static final String CONTEXT = "SET KEY NAME HERE";
// Managers
private static IssueManager issueManager = ComponentAccessor.getIssueManager();
public run () {
// Check the behaviour context ID
if (behaviourContextId == CONTEXT) {
log.setLevel(Level.DEBUG);
log.debug("Constrained issue creation: Task triggered");

// Set the issue and project fields to read only
getFieldById("project-field").setReadOnly(true);
getFieldById("issuetype-field").setReadOnly(true);

// Get the issue that was created from
Issue contextIssue = issueManager.getIssueObject(getContextIssueId());

// Set any other fields here
getFieldById("summary").setFormValue("Cloned issue: ${contextIssue.summary}");
getFieldById("description").setFormValue("Cloned issue: ${contextIssue.summary}");
getFieldById("issuelinks-linktype").setFormValue("SET LINK NAME HERE").setReadOnly(true);
getFieldById("issuelinks-issues").setFormValue(contextIssue.key).setReadOnly(true);
}
}
}
Brittany Wispell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 14, 2018
//This copies specific fields to the new ticket
List copyCustomFields = ["Custom Field Name", "Custom Field Name", "Custom Field Name"]

List<CustomField> contextFields = customFieldManager.getCustomFieldObjects(contextIssue)
contextFields.each { CustomField cf ->
if (copyCustomFields && !copyCustomFields.contains(cf.name)) {
return
}
def contextValue = cf.getValue(contextIssue)?.toString()
if (!contextValue) {
return
}
getFieldById(cf.id).setFormValue(contextValue)
log.debug("contextValue: ${contextValue?.class} for type ${cf.name}")
if (contextValue instanceof ApplicationUser) {
getFieldById(cf.id).setFormValue(contextValue.username)
} else if (contextValue instanceof List && contextValue[0] instanceof Option) {
getFieldById(cf.id).setFormValue(contextValue*.optionId)
} else {
getFieldById(cf.id).setFormValue(contextValue)
}
}
}
}
}

Should the CONTEXT be the key of the Script Fragment?

Brittany Wispell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 14, 2018

Cannot get it to work. If I uses inline-script I guess I shouldn't create the class and method?

Kind of new to Script-runner ;)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events