Forums

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

getFieldById("issuetype-field").setFormValue("Task") does't work on create issue screen

harainye
Contributor
November 22, 2022

Use behavior to set the issue type when you create a issue using ScriptRunner.

The id of the issuetype is not changed

But it doesn't work.

Can someone look at it for me!

Here is my code:

============

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.service.sprint.Sprint
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("order-numerical-task")
log.setLevel(Level.DEBUG)
def prop = ComponentAccessor.getApplicationProperties()
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

if ("order-numerical-task" == getBehaviourContextId()) {
def issueManager = ComponentAccessor.getIssueManager()
def fieldManager = ComponentAccessor.getCustomFieldManager()

def contextIssue = issueManager.getIssueObject(getContextIssueId())

// issuetype
//def issueTypeField = getFieldById(ISSUE_TYPE).setFormValue('数值').setReadOnly(true)

//// only change the field label but id is not changed
getFieldById("issuetype-field").setFormValue("Task").setReadOnly(true)

//getFieldById("issuetype").setFormValue(10301L)


// project
getFieldById("project-field").setFormValue(contextIssue.getProjectObject().name).setReadOnly(true)
getFieldById("project").setFormValue(contextIssue.getProjectObject().id.toString()).setReadOnly(true)
log.debug "=====issuetypeId="+getFieldById("issuetype")
// summary
getFieldById("summary").setFormValue(contextIssue.summary)
// description
getFieldById("description").setFormValue(contextIssue.description)

getFieldById("issuelinks-linktype").setFormValue("relates to").setReadOnly(true)
getFieldById("issuelinks-issues").setFormValue(contextIssue.key).setReadOnly(true)
getFieldById("priority").setFormValue(contextIssue.priorityId)
getFieldById("components").setRequired(true)
getFieldById("reporter").setFormValue(contextIssue.getReporter().name)
getFieldById("issuelinks-issues").setHidden(true)
getFieldById("assignee").setHidden(true)
log.debug "=====end="
}

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 28, 2022

Hi @harainye

The approach you are using appears to be incorrect.

Instead of using:-

getFieldById("issuetype-field").setFormValue("Task").setReadOnly(true)

As shown below, it would be best if you used the IssueFieldConstants to declare the system field. This approach will help to avoid any errors in the System Field name.

import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE
...
...

def issueType = getFieldById(ISSUE_TYPE)

Below is the complete sample working code for your reference:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.ISSUE_TYPE

@BaseScript FieldBehaviours behaviours
def issueType = getFieldById(ISSUE_TYPE)
issueType.setFormValue('Task')
issueType.readOnly = true

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Since you want this to load the moment the Create screen is displayed, it would be best to use the Behaviour Initialiser instead of the Server-Side Behaviour.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

 

Below is a test screenshot for your reference:-

test1.png

If you observe, when the create screen is loaded, the Issue Type is by default set to Task and is also set to Read-Only.

 

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer