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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

JIRA7 - Script Runner - Create new issue - Project Picker and User Picker custom fields

Hi,

 I have tried some methods and could not succeed, I need some help in creating the new issue using script runner script console. The use case is I have to create issues in project 'POWN' with a project picker and user picker values set - both are single selects.

Without these two custom fields I am able to create the issue in 'POWN' project. My code is

//https://community.atlassian.com/t5/Marketplace-Apps-questions/ScriptRunner-automation-rules-add-a-value-to-a-custom-field-list/qaq-p/868166
//https://community.atlassian.com/t5/Jira-questions/script-of-add-values-to-single-select-list-custom-field/qaq-p/872349
//https://community.atlassian.com/t5/Jira-questions/script-of-add-values-to-single-select-list-custom-field/qaq-p/872349


//https://community.atlassian.com/t5/Jira-questions/Create-Issue-in-project-depending-on-custom-field-ScriptRunner/qaq-p/58630
import org.apache.log4j.Level
import org.apache.log4j.Logger
def log = Logger.getLogger("com.acme.workflows")
log.setLevel(Level.DEBUG)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager


def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.getIssueFactory()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def customFieldManager =ComponentAccessor.getCustomFieldManager()
def userUtil = ComponentAccessor.getUserUtil()
def projectMgr = ComponentAccessor.getProjectManager()
def issueLinkTypeManager = ComponentAccessor.getComponentOfType(IssueLinkTypeManager.class)
def issueLinkType = issueLinkTypeManager.getIssueLinkTypesByName('Problem/Incident')[0];
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def issueService = ComponentAccessor.getIssueService();

def IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//log.info(user.id)

def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()
def productToOwn = projectManager.getProjectObjByKey("AGEIR")
//log.info(productToOwn)
def pkey = productToOwn.key.replaceAll("Project: ", "")
//log.info(pkey)


def PTOCustomField = customFieldManager.getCustomFieldObjectByName("Product to Own")


CustomFieldManager customFieldManager_EU = ComponentAccessor.getCustomFieldManager();
CustomField cf_EU = customFieldManager_EU.getCustomFieldObjectByName("Executive Owner");
def userManager_EU = ComponentAccessor.getUserManager()
// Use this user to set the user picker to
def user_EU = userManager_EU.getUserByName("srinivasanr")
log.info(user_EU.username)

log.debug ("Initialize inputparameters")
issueInputParameters.setProjectId(projectMgr.getProjectObjByKey("POWN").id) //JSD project
issueInputParameters.setIssueTypeId("12701")
issueInputParameters.setSummary("AGEIR Ownership")

issueInputParameters.setCustomFieldValue(PTOCustomField, productToOwn)
issueInputParameters.setCustomFieldValue(cf_EU, user_EU);


IssueService.CreateValidationResult validationResult = issueService.validateCreate(authenticationContext.getLoggedInUser(), issueInputParameters); //issueObject.getReporter()

if(!validationResult.isValid()) {
log.debug("Could not create issue: " + validationResult.getErrorCollection());
} else {
// Create new issue
log.debug ("Creating new issue")
IssueService.IssueResult createResult = issueService.create(authenticationContext.getLoggedInUser(), validationResult)
log.debug(createResult)
if (!createResult.isValid()) {
log.debug("Issue was not created!")
} else {
log.debug("Issue was created. Issue Id: " + createResult.getIssue().getKey())
}
}

 

 Both lines with  setCustomFieldValue are failing; 

For project picker the error is 

No signature of method: com.atlassian.jira.issue.IssueInputParametersImpl.setCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.ImmutableCustomField, com.atlassian.jira.project.ProjectImpl) values: [Product to Own, Project: AGEIR] Possible solutions: getCustomFieldValue(java.lang.Long), getCustomFieldValue(java.lang.String), addCustomFieldValue(java.lang.Long, [Ljava.lang.String;), addCustomFieldValue(java.lang.String, [Ljava.lang.String;)

 

For user picker the error is

 No signature of method: com.atlassian.jira.issue.IssueInputParametersImpl.setCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.ImmutableCustomField, com.atlassian.jira.user.DelegatingApplicationUser) values: [Executive Owner, srinivasanr(srinivasanr)] Possible solutions: getCustomFieldValue(java.lang.Long), getCustomFieldValue(java.lang.String), addCustomFieldValue(java.lang.Long, [Ljava.lang.String;), addCustomFieldValue(java.lang.String, [Ljava.lang.String;)

 

I have another curious issue, without these custom fields though the issue is created neither "Issue was created, Issue Id" nor "Issue was not created"  logs are displayed on the console

Can anyone help with this please? I need to be able create new issue with those custom fields. 

 

Thank you in advance

 

with warm regards

ramki

 

1 answer

1 accepted

0 votes
Answer accepted

The following code works; observe

  • first of all, it is addCustomFieldValue
  • that project picker id has to be converted to string
  • user has to be username
  • customfields passed should be with their ids
//https://community.atlassian.com/t5/Marketplace-Apps-questions/ScriptRunner-automation-rules-add-a-value-to-a-custom-field-list/qaq-p/868166
//https://community.atlassian.com/t5/Jira-questions/script-of-add-values-to-single-select-list-custom-field/qaq-p/872349
//https://community.atlassian.com/t5/Jira-questions/script-of-add-values-to-single-select-list-custom-field/qaq-p/872349


//https://community.atlassian.com/t5/Jira-questions/Create-Issue-in-project-depending-on-custom-field-ScriptRunner/qaq-p/58630

// IMPORTANT LINK DO NOT DELETE https://community.atlassian.com/t5/Marketplace-Apps-questions/Create-issue-with-IssueService/qaq-p/706336
import org.apache.log4j.Level
import org.apache.log4j.Logger
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def customFieldManager =ComponentAccessor.getCustomFieldManager()
def projectMgr = ComponentAccessor.getProjectManager()
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//reference issue
def issue = issueService.getIssue(user, "POWN-1").getIssue()
def IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
//log.info(user.id)

def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()
def productToOwn = projectManager.getProjectObjByKey("AGEIR")
//log.info(productToOwn.id)
def pkey = productToOwn.key.replaceAll("Project: ", "")
//log.info(pkey)
def PTOCustomField = customFieldManager.getCustomFieldObjectByName("Product to Own")
//log.info(PTOCustomField)


CustomFieldManager customFieldManager_EU = ComponentAccessor.getCustomFieldManager()
CustomField cf_EU = customFieldManager_EU.getCustomFieldObjectByName("Executive Owner")
def userManager_EU = ComponentAccessor.getUserManager()
// Use this user to set the user picker to
def user_EU = userManager_EU.getUserByName("srinivasanr")
//log.info(user_EU.username)

//https://community.atlassian.com/t5/Marketplace-Apps-questions/Post-script-function-multiple-user-picker-custom-field-update/qaq-p/352376

log.debug ("Initialize inputparameters")
issueInputParameters.setProjectId(projectMgr.getProjectObjByKey("POWN").id)
issueInputParameters.setIssueTypeId(issue.issueTypeId)
issueInputParameters.setSummary("AGEIR Ownership")

issueInputParameters.addCustomFieldValue(PTOCustomField.id, productToOwn.id.toString())
//issueInputParameters.skipScreenCheck()
issueInputParameters.addCustomFieldValue(cf_EU.id,user_EU.username)


IssueService.CreateValidationResult validationResult = issueService.validateCreate(authenticationContext.getLoggedInUser(), issueInputParameters);
//issueObject.getReporter()
def newIssue

if(!validationResult.isValid()) {
log.debug("Could not create issue: " + validationResult.getErrorCollection());
} else {
// Create new issue
log.debug ("Creating new issue")
def createResult = issueService.create(authenticationContext.getLoggedInUser(), validationResult)
newIssue = createResult.getIssue()
log.info(newIssue)

if (!createResult.isValid()) {
log.debug("Issue was not created!")
} else {
log.debug("Issue was created. Issue Id: " + createResult.getIssue().getKey())
}
}

logging to the server is working but to the console after the create is still not working for me

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Statuspage

New feature: Slack notifications for Statuspage

We're excited to announce the release of a long-requested feature on Statuspage. Now visitors to your status page can subscribe to get notified in Slack when you report an incident or maintenance. Th...

51 views 0 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you