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

set customer request type with script runner

Olivier Albertini February 23, 2015

My version of script runner is  3.0.9

 

When I create a task by an agent, I want to create another and link the tasks in JSD 2.2 project.

I need to change the value of customer request type.

When I use automation, I m able to edit the issue and change the customer request type. But I would like to use script runner ...

If I remove the edition of customfield for customer request type, the task is created. 

 

So something is wrong with :

[...]
def cfrt = customFieldManager.getCustomFieldObjects(newissue).find {it.name == 'Customer Request Type'}
[...]
newissue.setCustomFieldValue(cfrt, 'im/scanner-management-system')

 

Here my code

import com.atlassian.jira.user.UserUtils
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.atlassian.crowd.embedded.api.User
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.fields.CustomField
 
log = Category.getInstance("com.onresolve.jira.groovy.CreateDependentIssue")
 
// Configurable section
def projectName = "Dicom Management" // Name of project you want to create the issue in
//def CustomRequestTypeName = "customfield_10010"
//def CustomTerminalName = "customfield_11811"
def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
//CustomField customFieldRequestType = customFieldManager.getCustomFieldObject(CustomRequestTypeName)
//CustomField customFieldTerminal = customFieldManager.getCustomFieldObject(CustomTerminalName)
//def customFieldLocationVal = issue.getCustomFieldValue(customFieldLocation)
//def customFieldTerminalVal = issue.getCustomFieldValue(customFieldTerminal)

// for other customisations change the code below
issueMgr = ComponentManager.getInstance().getIssueManager()
projectMgr = ComponentManager.getInstance().getProjectManager()
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
User currentUserObj = UserUtils.getUser(currentUser);
def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true
issueFactory = ComponentManager.getInstance().getIssueFactory()
newissue = issueFactory.getIssue()
def cfrt = customFieldManager.getCustomFieldObjects(newissue).find {it.name == 'Customer Request Type'}
//newissue.setSummary (" PAIE - "+issue.summary+"- "+customFieldTerminalVal)
newissue.setSummary (" PAIE - "+issue.summary)
newissue.setProject (projectMgr.getProjectByName(projectName))
newissue.setIssueType (issue.getIssueType())
newissue.description = issue.description
newissue.assignee = UserUtils.getUser("oalbertini");
newissue.reporter = UserUtils.getUser("oalbertini");
newissue.setCustomFieldValue(cfrt, 'im/scanner-management-system')
//newissue.setCustomFieldValue(customFieldRequestType, "im/scanner-management-system")
 
params = ["issue":newissue]
subTask = issueMgr.createIssue(currentUserObj, params)
println subTask.get("key")
 
// get the current list of outwards depends on links to get the sequence number
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
def sequence = 0
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
    if ("Blocks" == link.issueLinkType.name) {
        sequence++;
    }
}
 
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
// TODO: Should check that 10000 is always the link id for Gantt: finish-start
linkMgr.createIssueLink (newissue.id, issue.id, 10000, sequence, currentUserObj)
ImportUtils.indexIssues = wasIndexing

 

I have the following error :

*************************************************************************************
2015-02-23 14:20:55,763 http-bio-8090-exec-13 ERROR oalbertini 860x478289x1 1h891ap  /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: IM-230, actionId: 851, file: <inline script>
java.lang.NullPointerException
	at com.atlassian.jira.issue.managers.DefaultCustomFieldManager.getCustomFieldObjects(DefaultCustomFieldManager.java:272)
	at com.atlassian.jira.issue.CustomFieldManager$getCustomFieldObjects.call(Unknown Source)
	at Script11.run(Script11.groovy:37)
2015-02-23 14:20:55,781 http-bio-8090-exec-13 ERROR oalbertini 860x478289x1 1h891ap  /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] *************************************************************************************
2015-02-23 14:20:55,781 http-bio-8090-exec-13 ERROR oalbertini 860x478289x1 1h891ap /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: IM-230, actionId: 851, file: <inline script>
com.atlassian.jira.exception.CreateException: An unknown exception occured executing Validator com.atlassian.jira.workflow.SkippableValidator@47485a23: root cause: java.lang.NullPointerException
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:695)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:618)
	at com.atlassian.jira.issue.IssueManager$createIssue.call(Unknown Source)
	at com.atlassian.jira.issue.IssueManager$createIssue.call(Unknown Source)
	at Script9.run(Script9.groovy:50)
Caused by: com.atlassian.jira.workflow.WorkflowException: An unknown exception occured executing Validator com.atlassian.jira.workflow.SkippableValidator@47485a23: root cause: java.lang.NullPointerException
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:909)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:684)
	... 4 more
Caused by: com.opensymphony.workflow.WorkflowException: An unknown exception occured executing Validator com.atlassian.jira.workflow.SkippableValidator@47485a23: root cause: java.lang.NullPointerException
	at com.opensymphony.workflow.AbstractWorkflow.verifyInputs(AbstractWorkflow.java:1523)
	at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:615)
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:879)
	... 5 more
Caused by: java.lang.NullPointerException
	at com.atlassian.jira.workflow.validator.AbstractPermissionValidator.hasUserPermission(AbstractPermissionValidator.java:49)
	at com.atlassian.jira.workflow.validator.PermissionValidator.validate(PermissionValidator.java:46)
	at com.atlassian.jira.workflow.SkippableValidator.validate(SkippableValidator.java:52)
	at com.opensymphony.workflow.AbstractWorkflow.verifyInputs(AbstractWorkflow.java:1512)
	... 8 more
2015-02-23 14:21:06,104 http-bio-8090-exec-5 WARN oalbertini 861x478353x1 1h891ap  /rest/analytics/1.0/publish/bulk [analytics.client.browser.PublisherResource] BrowserEvent property values should not be Collections or Maps. name=servicedesk.page.view.agent.session, properties={count=5, path=[queues, queues/issue, queues/issue, queues/issue, queues/issue], sdVersion=2.2.0}

 

someone can help me plz ? THX !

2 answers

1 vote
Rodolfo Bortolin July 23, 2015

For us it works better via SQL:

def stringvalue = sql.firstRow("SELECT concat(lower(p.pkey),'/',vform.key) as value FROM jiraissue i , project p, issuetype t ,  AO_54307E_VIEWPORT vport, AO_54307E_VIEWPORTFORM vform WHERE p.id = i.project 	AND i.issuetype = t.id	AND i.project = vport.project_id AND vport.id = vform.viewport_id AND	vform.issue_type_id = t.id AND i.id = $issue.id;");
	
	DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
	def serviceType = "customfield_10000"
	customFieldTarget = customFieldManager.getCustomFieldObject(serviceType);
	if(stringvalue.value != "null"){
		def requestType = customFieldTarget.getCustomFieldType().getSingularObjectFromString(stringvalue.value)
		customFieldTarget.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customFieldTarget), requestType),changeHolder);
	}

After that, reindex the issue.

 

Ivaylo Mihaylov June 26, 2018

I just want to mention that the first substring is not the project key but the portal key. The portal key matches the project key if the project key was never changed. Using AO_54307E_VIEWPORT.KEY is the correct solution.

Andrei Lipovchenko December 4, 2018

What about this?
issue.getProjectObject().getOriginalKey()

0 votes
Marina Veselić March 31, 2020

There isn't a better way of getting the key of the request type's name?? You have to search the database in order to get that? 

We have the problem of many issues comming form emails and we need to triage them based on some parameters and we need to set the correct Customer Request Type for each one. We do that with code because it isn't always strightforward. 

Does anyone know why this isn't as simple as ".getKeyFromName()" kind of function?

Is there an Atlassian issue where I can vote to extend the api so that there is a better way of getting (and setting) the CRT key?

Cheers,

Marina

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events