Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
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

Cloning field configuration scheme (and field configuration) using groovy

Attempting to clone a field configuration scheme but running into a sql injection error. Script:

//Create a project objects (Source & Target)
Project jiraProjectSourceObj = projectManager.getProjectObjByName("SD_TEMPLATE")
Project jiraProjectTargetObj = projectManager.getProjectObjByName("SD_TEST")

//Define issue type scheme properties
def newIssueTypeSchemeObj

 

//Return a issue type scheme object
def ewIssueTypeSchemeObj = issueTypeScheme.getConfigScheme(jiraProjectTargetObj)

//Get issue types list
def issueTypesForScheme = issueTypeScheme.getIssueTypesForScheme(newIssueTypeSchemeObj)
List issueTypesList = new ArrayList(issueTypesForScheme)

//
def fieldConfigScheme = issueTypeScheme.getConfigScheme(jiraProjectSourceObj)
def configs = fieldConfigScheme.getConfigs()

def fieldConfigSchemeBuilder = new FieldConfigScheme.Builder(fieldConfigScheme)
fieldConfigSchemeBuilder.setName("sd_name")
fieldConfigSchemeBuilder.setDescription("sd_desc")

fieldConfigScheme = fieldConfigSchemeBuilder.toFieldConfigScheme()

//Get parameters
def configurableField = fieldConfigScheme.getField()
def contexts = fieldConfigScheme.getContexts()
def fieldConfig = fieldConfigScheme.getOneAndOnlyConfig()

fieldConfigSchemeManager.createFieldConfigScheme(fieldConfigScheme, contexts, issueTypesList, configurableField)

This is the error I receive:

|| Error: com.atlassian.jira.exception.DataAccessException: org.ofbiz.core.entity.GenericEntityException: while inserting: [GenericEntity:FieldConfigScheme][name,sd_name][description,sd_desc][id,26300][fieldid,issuetype] (SQL Exception while executing the following:INSERT INTO fieldconfigscheme (ID, configname, DESCRIPTION, FIELDID, CUSTOMFIELD) VALUES (?, ?, ?, ?, ?) (ORA-00001: unique constraint (JIRAMERGE.PK_FIELDCONFIGSCHEME) violated ))

What am I missing here? Thanks!

 

1 answer

Hi there,

I think the main problem was some missing parts and syntax errors. Here is a cleaned version's code:

/**
* Atlassian Community answer
* <a href="https://community.atlassian.com/t5/Jira-Service-Desk-questions/Cloning-field-configuration-scheme-and-field-configuration-using/qaq-p/1449149" target="_blank">Cloning field configuration scheme (and field configuration) using groovy</a>
* @author Tamás Baglyas - https://github.com/tbaglyas
* @version 1.0
* @since 2020-08-05
*/
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.issue.fields.config.manager.IssueTypeSchemeManager;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme;
import com.atlassian.jira.issue.issuetype.IssueType;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme.Builder;
import com.atlassian.jira.issue.fields.ConfigurableField;
import com.atlassian.jira.issue.context.JiraContextNode;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManager;

ProjectManager projectManager = ComponentAccessor.getProjectManager();

//Create Source and Target project object
Project sourceProject = projectManager.getProjectObjByName("SD_TEMPLATE");
Project targetProject = projectManager.getProjectObjByName("SD_TEST");

//New issue types
IssueTypeSchemeManager issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager();
FieldConfigScheme targetFieldConfigScheme = issueTypeSchemeManager.getConfigScheme(targetProject);
List<IssueType> issueTypes = (List<IssueType>)issueTypeSchemeManager.getIssueTypesForScheme(targetFieldConfigScheme);

//New FieldConfigScheme
FieldConfigScheme sourceFieldConfigScheme = issueTypeSchemeManager.getConfigScheme(sourceProject)
FieldConfigScheme.Builder fieldConfigSchemeBuilder = new FieldConfigScheme.Builder(sourceFieldConfigScheme);
fieldConfigSchemeBuilder.setName("sd_name");
fieldConfigSchemeBuilder.setDescription("sd_desc");
FieldConfigScheme newConfigScheme = fieldConfigSchemeBuilder.toFieldConfigScheme();

//Get parameters
ConfigurableField field = newConfigScheme.getField();
List<JiraContextNode> contexts = newConfigScheme.getContexts();

FieldConfigSchemeManager fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager();

FieldConfigScheme newFieldConfigScheme = fieldConfigSchemeManager.createFieldConfigScheme(newConfigScheme, contexts, issueTypes, field);

Nope, Dupliate key Primary, SQL error.

 

Please fix this as i would really love to put it in a for loop and give every project in my Jira instance its own field configuration and its own scheme programmatically like this.

Like Saiman Saimannex likes this

Hello.
There is way to generate random  key but even in that case there is error contexts is empty 

fieldConfigSchemeBuilder.setId(null); this will generate new ID
 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events