Forums

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

Add Project in the Custom Field Context using groovy/scriptrunner

Ranganathan Arumugham February 22, 2018

Hi,

We are creating some standard Task on project creation using the ScriptRunner Custom Listener on ProjectCreatedEvent.

In the same listener, we want to add the new project in one of the Custom Field Content.

I looked into other forums and it seems it's possible using FieldconfigSchemes and JiraContextNode classes, but not sure how to use it.

 

Any suggestion would be of great help!!!

 

Thanks!!!

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
Rinaldi James Michael
Contributor
June 2, 2023

(COMMENT DELETED)

Noel Dinger (Extern)
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 30, 2024

Why did this got deleted? Does anyone have a solution? 

0 votes
Rinaldi James Michael
Contributor
July 4, 2025

Deleted the previous answer since that didn't work out as required. Found code by Tishina that works better. I've modified it to my use case:

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Author/s: Tishina (Atlassian Community)
//Modified: Rinaldi Michael
//Created: August 2022
//Last Modified: 4th July 2025, 10:18 PM
//Reference:
//https://docs.atlassian.com/software/jira/docs/api/7.6.1/index.html?com/atlassian/jira/issue/customfields/CustomFieldUtils.html
//https://community.developer.atlassian.com/t/add-project-for-customfield-context-programmatically-via-java-solved/60776
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


import java.io.*
import java.lang.String
import java.lang.*
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*
import com.onresolve.scriptrunner.parameters.annotation.*
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.config.manager.FieldConfigManager
import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManagerImpl
import com.atlassian.jira.project.Project
import com.onresolve.scriptrunner.parameters.annotation.ProjectPicker
import com.atlassian.jira.issue.fields.config.FieldConfigScheme
import com.atlassian.jira.issue.customfields.*
import com.atlassian.jira.issue.context.*
import com.atlassian.jira.issue.context.JiraContextNode
import com.atlassian.jira.issue.customfields.CustomFieldUtils.*
import com.atlassian.jira.issue.fields.config.manager.*


//////////////////////////////////////////////////////////////////////////////////////////////////////
//Variable declarations
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def customFieldConfigManager = ComponentAccessor.getComponent(FieldConfigManager)
def customFieldConfigSchemeManagerImpl = ComponentAccessor.getComponent(FieldConfigSchemeManagerImpl)
def fieldConfigSchemeManager = ComponentAccessor.getComponent(FieldConfigSchemeManager)
def customFieldUtils = ComponentAccessor.getComponent(CustomFieldUtils)
def projectManagerVariable = ComponentAccessor.projectManager
def issueService = ComponentAccessor.issueService
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//////////////////////////////////////////////////////////////////////////////////////////////////////
//for script console input
@CustomFieldPicker(label = 'Custom Field', description = 'Pick a custom field', placeholder='Select custom field')
CustomField customField1

@NumberInput(label = 'Enter context number (enter if name not known/filled)', description = 'DO NOT PROVIDE CONTEXT NAME IF THIS VALUE IS USED. One custom field may have one or many contexts. Check the order in the custom fields configuration page and type in which context it is. If you want to modify the third context. Type in 3.')
Integer contextNumberInt
contextNumberInt-=1

@ShortTextInput(description='DO NOT PROVIDE CONTEXT NUMBER IF THIS VALUE IS USED. Provide the name of the custom field context that you would like to add the projects to',label='Custom Field Context Name (enter if number not known/filled)')
String contextName

@ShortTextInput(description = 'Enter a comma separated list of project keys (Not case sensitive)', label = 'Provide a comma list of project keys')
String string
String[] projects = string.split(',')

//////////////////////////////////////////////////////////////////////////////////////////////////////
//Fetch the custom field configuration scheme
FieldConfigScheme scheme
if(contextNumberInt!=null && contextName==null)
scheme = customField1.getConfigurationSchemes()[contextNumberInt]


//Get the Custom field context
FieldConfigScheme fieldContext
if(contextName!=null && contextNumberInt==null)
{
for (FieldConfigScheme fcs : customField1.getConfigurationSchemes())
if (contextName.equalsIgnoreCase(fcs.getName()))
fieldContext=fcs
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////
// List of projectId already added to context
List<Long> projIDs = scheme.getAssociatedProjectIds(); //VERIFIED work correct

//Create list of project ID's
for(int i=0;i<projects.size();i++)
{
projIDs.add(projectManagerVariable.getProjectByCurrentKey(projects[i]).getId().toLong())
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
//Output variable
String printtext=""
printtext+= "Custom Field - <b>${customField1}</b>"
printtext+= "<br>Context is <b>${scheme.name}</b><br><br>"


//////////////////////////////////////////////////////////////////////////////////////////////////////
//Add projects to the context

// list projects as JiraContextNode for updating context
List<JiraContextNode> addProjectContext = CustomFieldUtils.buildJiraIssueContexts(false, projIDs.toArray(new Long[projIDs.size()]), projectManagerVariable);

// in this method I update list of projects for context; NOW IT WORKS!
if(contextNumberInt!=null && contextName==null)
fieldConfigSchemeManager.updateFieldConfigScheme(scheme, addProjectContext, customField1)
else
fieldConfigSchemeManager.updateFieldConfigScheme(fieldContext, addProjectContext, customField1)


//////////////////////////////////////////////////////////////////////////////////////////////////////
//Print out the final list of projects in the custom field's context
printtext+="<br><br>*------------*<br><b>Project List</b>"
printtext+="<br>The list tends to be inaccurate. Which is why Jira always asks admins to reindex the instance after modifying a custom field. Run the script again with the same inputs to view the accurate list.<br>"
def allProjectsInTheContext = scheme.associatedProjectObjects
for(int p=0;p<allProjectsInTheContext.size();p++)
{
printtext+="<br>${p+1}. Project Name: <b>${allProjectsInTheContext[p].getName()}</b>, Project Key: <b>${allProjectsInTheContext[p].getKey()}</b>"
}


return printtext
TAGS
AUG Leaders

Atlassian Community Events